Hello,
I want to assign different position to stepper motors from Processing.
I can do this very well with the library accelstepper.
But with the teensy stepper library, I have to add this at the end of the void loop
Morever it block my animation in Processing.
For now I just to play with 3 motor at different random position without the while and the delay because It blocks my animation in Processing
I put my entire program below
I want to assign different position to stepper motors from Processing.
I can do this very well with the library accelstepper.
But with the teensy stepper library, I have to add this at the end of the void loop
Code:
while(controller_1.isRunning() || controller_2.isRunning() || controller_3.isRunning()){ // wait until both controllers finished their movements
delay( 0);
Morever it block my animation in Processing.
For now I just to play with 3 motor at different random position without the while and the delay because It blocks my animation in Processing
I put my entire program below
Code:
#include "TeensyStep.h"
//# define SerialUSB Serial1
StepControl controller_1;
StepControl controller_2;
StepControl controller_3;
Stepper s1(3,2), s2(5,4), s3(7,6);
int i; //Test la loop
// Receive with start- and end-markers combined with parsing
const byte numChars = 200;
char receivedChars[numChars];
char tempChars[numChars]; // temporary array for use when parsing
// variables to hold the parsed data
char messageFromPC[numChars] = {0}; //or {5} doesn't change anything
int integerFromPC0 = 0;
int integerFromPC1 = 0;
int integerFromPC2 = 0;
int integerFromPC3 = 0;
int integerFromPC4 = 0;
int integerFromPC5 = 0;
int integerFromPC6 = 0;
int integerFromPC7 = 0;
int integerFromPC8 = 0;
int integerFromPC9 = 0;
int PC0= 0;
int PC1= 0;
int PC2= 0;
int PC3= 0;
int PC4= 0;
int PC5= 0;
int PC6= 0;
int PC7= 0;
int PC8= 0;
int PC9= 0;
int PCTer0= 0;
int PCTer1= 0;
int PCTer2= 0;
int PCTer3= 0;
int PCTer4= 0;
int PCTer5= 0;
int PCTer6= 0;
int PCTer7= 0;
int PCTer8= 0;
int PCTer9= 0;
int LoworderTrigger = 0;
int orderTrigger = 0;
int orderCohesion = 0;
float floatFromPC = 0.0; // no flat are used for the moment
boolean newData = false;
//============
const int ledPin = LED_BUILTIN;// the number of the LED pin
//StepControl controller;
void setup()
{
Serial.begin ( 500000);
Serial1.begin (500000); // receive datas from Processing with the "Programming Port"
while (!Serial);
Serial.println("TEST" );
s1
.setMaxSpeed(3200) // steps/s
.setAcceleration(1000); // steps/s^2
s2
.setMaxSpeed(3200) // steps/s
.setAcceleration(1000); // steps/s^2
s3
.setMaxSpeed(3200) // steps/s
.setAcceleration(1000); // steps/s^2
}
void loop()
{
recvWithStartEndMarkers(); // receive 33 datas from Processing 30 times/sec.
if (newData == true) {
strcpy(tempChars, receivedChars);
// this temporary copy is necessary to protect the original data
// because strtok() used in parseData() replaces the commas with \0
parseData(); // split the 33 data into its parts
i= random (0, 6400);
// showPosition(); // These datas are used to control positions of 10 motors. Print them on the native port
// showPhazisScaled(); // Print datas on the native port
// showTrigBangWithRevolution(); // Print datas on the native port
newData = false;
}
// Serial.print (i);
// Serial1.print (i);
// s1.setTargetAbs(PC0);
// s2.setTargetAbs(PC0 );
// s3.setTargetAbs(PC0);
s1.setTargetAbs(i*2);
s2.setTargetAbs(i );
s3.setTargetAbs(-i*2);
controller_1.moveAsync (s1 ); controller_2.moveAsync ( s2 ); controller_3.moveAsync (s3 );
while(controller_1.isRunning() || controller_2.isRunning() || controller_3.isRunning()){ // wait until both controllers finished their movements
delay( 0);
}
}
void recvWithStartEndMarkers() { // receive 33 datas from Processing 30 times/sec. They are marked like that in Processing <12, -98, 34,... ,-340>
static boolean recvInProgress = false;
static byte ndx = 0;
char startMarker = '<';
char endMarker = '>';
char rc;
while (Serial.available() > 0 && newData == false) {
rc = Serial.read();
if (recvInProgress == true) {
if (rc != endMarker) {
receivedChars[ndx] = rc;
ndx++;
if (ndx >= numChars) {
ndx = numChars - 1;
}
}
else {
receivedChars[ndx] = '\0'; // terminate the string
recvInProgress = false;
ndx = 0;
newData = true;
}
}
else if (rc == startMarker) {
recvInProgress = true;
}
}
}
//============
void parseData() { // split the 33 data into its parts
char * strtokIndx; // this is used by strtok() as an index
strtokIndx = strtok(tempChars,","); // get the first part - the string
integerFromPC0 = atoi(strtokIndx); // convert this part to an integer
strtokIndx = strtok(NULL, ","); // this continues where the previous call left off
integerFromPC1 = atoi(strtokIndx); // convert this part to an integer
strtokIndx = strtok(NULL, ","); // this continues where the previous call left off
integerFromPC2 = atoi(strtokIndx); // convert this part to an integer
strtokIndx = strtok(NULL, ","); // this continues where the previous call left off
integerFromPC3 = atoi(strtokIndx); // convert this part to an integer
strtokIndx = strtok(NULL, ","); // this continues where the previous call left off
integerFromPC4 = atoi(strtokIndx); // convert this part to an integer
strtokIndx = strtok(NULL, ","); // this continues where the previous call left off
integerFromPC5 = atoi(strtokIndx); // convert this part to an integer
strtokIndx = strtok(NULL, ","); // this continues where the previous call left off
integerFromPC6 = atoi(strtokIndx); // convert this part to an integer
strtokIndx = strtok(NULL, ","); // this continues where the previous call left off
integerFromPC7 = atoi(strtokIndx); // convert this part to an integer
strtokIndx = strtok(NULL, ","); // this continues where the previous call left off
integerFromPC8 = atoi(strtokIndx); // convert this part to an integer
strtokIndx = strtok(NULL, ","); // this continues where the previous call left off
integerFromPC9 = atoi(strtokIndx); // convert this part to an integer
strtokIndx = strtok(NULL, ","); // this continues where the previous call left off
PC0 = atoi(strtokIndx); // convert this part to an integer
strtokIndx = strtok(NULL, ","); // this continues where the previous call left off
PC1 = atoi(strtokIndx); // convert this part to an integer
strtokIndx = strtok(NULL, ","); // this continues where the previous call left off
PC2 = atoi(strtokIndx); // convert this part to an integer
strtokIndx = strtok(NULL, ","); // this continues where the previous call left off
PC3 = atoi(strtokIndx); // convert this part to an integer
strtokIndx = strtok(NULL, ","); // this continues where the previous call left off
PC4 = atoi(strtokIndx); // convert this part to an integer
strtokIndx = strtok(NULL, ","); // this continues where the previous call left off
PC5 = atoi(strtokIndx); // convert this part to an integer
strtokIndx = strtok(NULL, ","); // this continues where the previous call left off
PC6 = atoi(strtokIndx); // convert this part to an integer
strtokIndx = strtok(NULL, ","); // this continues where the previous call left off
PC7 = atoi(strtokIndx); // convert this part to an integer
strtokIndx = strtok(NULL, ","); // this continues where the previous call left off
PC8 = atoi(strtokIndx); // convert this part to an integer
strtokIndx = strtok(NULL, ","); // this continues where the previous call left off
PC9 = atoi(strtokIndx); // convert this part to an integer
strtokIndx = strtok(NULL, ","); // this continues where the previous call left off
PCTer0 = atoi(strtokIndx); // convert this part to an integer
strtokIndx = strtok(NULL, ","); // this continues where the previous call left off
PCTer1 = atoi(strtokIndx); // convert this part to an integer
strtokIndx = strtok(NULL, ","); // this continues where the previous call left off
PCTer2 = atoi(strtokIndx); // convert this part to an integer
strtokIndx = strtok(NULL, ","); // this continues where the previous call left off
PCTer3 = atoi(strtokIndx); // convert this part to an integer
strtokIndx = strtok(NULL, ","); // this continues where the previous call left off
PCTer4 = atoi(strtokIndx); // convert this part to an integer
strtokIndx = strtok(NULL, ","); // this continues where the previous call left off
PCTer5 = atoi(strtokIndx); // convert this part to an integer
strtokIndx = strtok(NULL, ","); // this continues where the previous call left off
PCTer6 = atoi(strtokIndx); // convert this part to an integer
strtokIndx = strtok(NULL, ","); // this continues where the previous call left off
PCTer7 = atoi(strtokIndx); // convert this part to an integer
strtokIndx = strtok(NULL, ","); // this continues where the previous call left off
PCTer8 = atoi(strtokIndx); // convert this part to an integer
strtokIndx = strtok(NULL, ","); // this continues where the previous call left off
PCTer9 = atoi(strtokIndx); // convert this part to an integer
strtokIndx = strtok(NULL, ","); // this continues where the previous call left off
LoworderTrigger = atoi(strtokIndx); // convert this part to an integer
strtokIndx = strtok(NULL, ","); // this continues where the previous call left off
orderTrigger = atoi(strtokIndx); // convert this part to an integer
strtokIndx = strtok(NULL, ","); // this continues where the previous call left off
orderCohesion = atoi(strtokIndx); // convert this part to an integer
}
//============
void showPosition() {
SerialUSB.print ("K ");SerialUSB.println(PC0);
SerialUSB.print ("L ");SerialUSB.println(PC1);
SerialUSB.print ("M ");SerialUSB.println(PC2);
SerialUSB.print ("N ");SerialUSB.println(PC3);
SerialUSB.print ("O ");SerialUSB.println(PC4);
SerialUSB.print ("P ");SerialUSB.println(PC5);
SerialUSB.print ("Q ");SerialUSB.println(PC6);
SerialUSB.print ("R ");SerialUSB.println(PC7);
SerialUSB.print ("S ");SerialUSB.println(PC8);
SerialUSB.print ("T ");SerialUSB.println(PC9);
}
void showPhazisScaled() {
SerialUSB.print ("A "); SerialUSB.println (integerFromPC0);
SerialUSB.print ("B "); SerialUSB.println (integerFromPC1);
SerialUSB.print ("C "); SerialUSB.println (integerFromPC2);
SerialUSB.print ("D "); SerialUSB.println (integerFromPC3);
SerialUSB.print ("E "); SerialUSB.println (integerFromPC4);
SerialUSB.print ("F "); SerialUSB.println (integerFromPC5);
SerialUSB.print ("G "); SerialUSB.println (integerFromPC6);
SerialUSB.print ("H "); SerialUSB.println (integerFromPC7);
SerialUSB.print ("I "); SerialUSB.println (integerFromPC8);
SerialUSB.print ("J "); SerialUSB.println (integerFromPC9);
}
void showTrigBangWithRevolution() {
SerialUSB.print ("U ");SerialUSB.println(PCTer0);
SerialUSB.print ("V ");SerialUSB.println(PCTer1);
SerialUSB.print ("W ");SerialUSB.println(PCTer2);
SerialUSB.print ("X ");SerialUSB.println(PCTer3);
SerialUSB.print ("Y ");SerialUSB.println(PCTer4);
SerialUSB.print ("Z ");SerialUSB.println(PCTer5);
SerialUSB.print ("a ");SerialUSB.println(PCTer6);
SerialUSB.print ("b ");SerialUSB.println(PCTer7);
SerialUSB.print ("c ");SerialUSB.println(PCTer8);
SerialUSB.print ("d ");SerialUSB.println(PCTer9);
SerialUSB.print ("e ");SerialUSB.println(LoworderTrigger);
SerialUSB.print ("f ");SerialUSB.println(orderTrigger);
SerialUSB.print ("g ");SerialUSB.println(orderCohesion);
}