M
M
motcart2018-03-19 15:42:37
Arduino
motcart, 2018-03-19 15:42:37

How to pass data through the port monitor to this sketch?

There is a sketch for controlling 5 steppers (2 for x, y and 1 for z axis). There is an arduino nano, drivers for motors with a separate power supply unit, and the motors themselves. The iron part works.
Help me figure out how to transfer data to control motors through the port monitor in the Arduino IDE? More precisely, what data is the controller waiting for me to send to him to rotate the motors?

//velocity control added

byte MotorState[]={1,1,1};  //X Y Z A Ex1 Ex2
word Movement_of_Motor[]={0,0,0};
char Direction_of_Motor[]={'+','+','+'};
int MotorNumber;
byte NewMotorState;

int PinX[]={9,11,12,10};
int PinY[]={13,3,4,2};
int PinZ[]={8,6,7,5};
int i;
byte b[32];
byte a[18];
word DMAX;
word stepcounter;
word s[5];
float OP1;
float OP2;
word period=0;

// the setup routine runs once when you press reset:
void setup()
  {                
  for (i = 0; i < 4; i = i + 1) 
      {
      pinMode(PinX[i], OUTPUT); 
      pinMode(PinY[i], OUTPUT);  
      pinMode(PinZ[i], OUTPUT); 
      }  
// initialize the serial communication:
  Serial.begin(9600);
  }
void shiftMotorState(int MN){
    if (Direction_of_Motor[MN]=='+'){
        switch (MotorState[MN]) {
            case 1:
                NewMotorState=3;
                break;
            case 3:
                NewMotorState=2;
                break;
            case 2:
                NewMotorState=6;
                break;    
            case 6:
                NewMotorState=4;
                break;
            case 4:
                NewMotorState=12;
                break;
            case 12:
                NewMotorState=8;
                break;
            case 8:
                NewMotorState=9;
                break;    
            default:
                NewMotorState=1;
            }
    }
     else{
        switch (MotorState[MN]) {
            case 2:
                NewMotorState=3;
                break;    
            case 6:
                NewMotorState=2;
                break;  
            case 4:
                NewMotorState=6;
                break;
            case 12:
                NewMotorState=4;
                break;
            case 8:
                NewMotorState=12; 
                break;   
            case 9:
                NewMotorState=8;
                break;
            case 1:
                NewMotorState=9;
                break;
            default:
                NewMotorState=1;
                break;
                }}
    MotorState[MN]=NewMotorState;
}
void setHardwareState()
{
  for (i = 0; i < 4; i = i + 1) 
    {
    digitalWrite(PinX[i], bitRead(MotorState[0],i));  
    digitalWrite(PinY[i], bitRead(MotorState[1],i)); 
    digitalWrite(PinZ[i], bitRead(MotorState[2],i));  
    }  
}
void disableHardware()
{
  for (i = 0; i < 4; i = i + 1) 
    {
    digitalWrite(PinX[i], LOW);  
    digitalWrite(PinY[i], LOW); 
    digitalWrite(PinZ[i], LOW);  
    }  
}

// the loop routine runs over and over again forever:
void loop() {
  disableHardware();
  if (Serial.available() > 31) {       
       for (i = 0; i < 32; i = i + 1) 
       {
       b[i] = Serial.read();
       }
       Direction_of_Motor[0]=b[8];
       Direction_of_Motor[1]=b[11];
       Direction_of_Motor[2]=b[14];
       Movement_of_Motor[0]=b[9]*256;
       Movement_of_Motor[1]=b[12]*256;
       Movement_of_Motor[2]=b[15]*256;
       Movement_of_Motor[0]=Movement_of_Motor[0]+b[10];
       Movement_of_Motor[1]=Movement_of_Motor[1]+b[13];
       Movement_of_Motor[2]=Movement_of_Motor[2]+b[16];
       period=b[30]*256;
       period=period+b[31];
       DMAX=0;
       for (MotorNumber = 0; MotorNumber < 3; MotorNumber = MotorNumber + 1)
            {
            s[MotorNumber]=0;
            if (Movement_of_Motor[MotorNumber]>DMAX) 
                {  
                DMAX=Movement_of_Motor[MotorNumber];
                }
            }
       for (stepcounter = 1; stepcounter <= DMAX; stepcounter = stepcounter + 1)
             {
             for (MotorNumber = 0; MotorNumber < 3; MotorNumber = MotorNumber + 1)
                  {                                        
                  OP1=(float)s[MotorNumber]*(float)DMAX;
                  OP2=(float)stepcounter*(float)Movement_of_Motor[MotorNumber];
                  if (OP1<=OP2 && Movement_of_Motor[MotorNumber]>0)
                     {  //если s[]*DMAX[] <= stepcounter*DX[]                   
                      s[MotorNumber]=s[MotorNumber]+1;                  
                      shiftMotorState(MotorNumber);
                      }
                  }
             setHardwareState();
             for (i = 0; i < 2; i = i + 1) 
             {
             delayMicroseconds(period);  
             delayMicroseconds(period);  
             delayMicroseconds(period);  
             delayMicroseconds(period);  
             delayMicroseconds(period);  
             delayMicroseconds(period);  
             delayMicroseconds(period);  
             delayMicroseconds(period);  
             delayMicroseconds(period);  
             delayMicroseconds(period); 
              } 
             }     
       a[0] = 'D';         
       a[1] = 'o';
       a[2] = 'n';
       a[3] = 'e';     
  
       a[17] = a[0] ^ a[1] ^ a[2] ^ a[3] ^ a[4] ^ a[5] ^ a[6] ^ a[7] ^ a[8] ^ a[9] ^ a[10] ^ a[11] ^ a[12] ^ a[13] ^ a[14] ^ a[15] ^ a[16] ;
       for (i = 0; i < 18; i = i + 1) 
       {
       Serial.write(a[i]);
       }
        }
}

Answer the question

In order to leave comments, you need to log in

1 answer(s)
O
Ocelot, 2018-03-20
@motcart

for (i = 0; i < 32; i = i + 1)
{
b[i] = Serial.read();
}
Direction_of_Motor[0]=b[8];
Direction_of_Motor[1]=b[11];
Direction_of_Motor[2]=b[14];
Movement_of_Motor[0]=b[9]*256;
Movement_of_Motor[1]=b[12]*256;
Movement_of_Motor[2]=b[15]*256;
Movement_of_Motor[0]=Movement_of_Motor[0]+b[10];
Movement_of_Motor[1]=Movement_of_Motor[1]+b[13];
Movement_of_Motor[2]=Movement_of_Motor[2]+b[16];
period=b[30]*256;
period=period+b[31];

Takes 32 bytes, numbering from 0 to 31.
Byte 8 - direction of the X axis ('+' or '-'). Bytes 9 (highest part) and 10 (lowest part) - move along the X axis.
Bytes 11, 12, 13 - the same for the Y axis.
Bytes 14, 15, 16 - the same for the Z axis.
Bytes 30 (highest part) and 31 (younger part) - the period between steps. Period unit == 20 µs, sort of.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question