top of page
Categories
Taesu Yim

Arduino code (move only)

Below is the Arduino code for the movement of the OROHA V2.


// ex) v004 (o) / v4 (x)
// ex) b0.9 (o) / b0.95(x)

int motor1Pin1 = 7;  // cw/ccw
int motor1Pin2 = 8;  // start 
int motor1Pin3 = 9;  // stop
int motor1Pin4 = 10; // INT_SPEED
int PWM1Pin = 11; // speed in
int motor2Pin1 = 20; // cw/ccw
int motor2Pin2 = 19; // start
int motor2Pin3 = 18; // stop
int motor2Pin4 = 6; // INT_SPEED
int PWM2Pin = 5; // spped in
int state[4];
int flag=0;       
int stateStop=0;
int PWMduty=30;
float turnAngle = 0.2; 
void setup() {
    // sets the pins as outputs:
    pinMode(motor1Pin1, OUTPUT);
    pinMode(motor1Pin2, OUTPUT);
    pinMode(motor1Pin3, OUTPUT);
    pinMode(motor1Pin4, OUTPUT);
    pinMode(PWM1Pin, OUTPUT);
    pinMode(motor2Pin1, OUTPUT);
    pinMode(motor2Pin2, OUTPUT);
    pinMode(motor2Pin3, OUTPUT);
    pinMode(motor2Pin4, OUTPUT);
    pinMode(PWM2Pin, OUTPUT);
    analogWrite(PWM1Pin, PWMduty);
    analogWrite(PWM2Pin, PWMduty);
    Serial.begin(9600);
}

void loop() {
    // if some date is sent, reads it and saves in state
    if(Serial.available() > 0)
     {     
       for(int i=0;i<4;i++){
       state[i]=Serial.read();   
      }
      flag=0;
    }   

    // if the state is 'w', robot moves forward
    if (state[0] == 'w') {
        digitalWrite(motor1Pin1, HIGH);
        digitalWrite(motor1Pin2, LOW); 
        digitalWrite(motor1Pin3, HIGH);
        digitalWrite(motor1Pin4, HIGH); 
        digitalWrite(motor2Pin1, LOW);
        digitalWrite(motor2Pin2, LOW);
        digitalWrite(motor2Pin3, HIGH);
        digitalWrite(motor2Pin4, HIGH);
        analogWrite(PWM1Pin, PWMduty);
        analogWrite(PWM2Pin, PWMduty);
        if(flag == 0){
          Serial.println("Forward");
          flag=1;
        }
    }
    
    // if the state is 'd', zero-radius truncate to the right
    else if (state[0] == 'd') {
        digitalWrite(motor1Pin1, LOW);
        digitalWrite(motor1Pin2, LOW); 
        digitalWrite(motor1Pin3, HIGH);
        digitalWrite(motor1Pin4, HIGH); 
        digitalWrite(motor2Pin1, LOW);
        digitalWrite(motor2Pin2, LOW);
        digitalWrite(motor2Pin3, HIGH);
        digitalWrite(motor2Pin4, HIGH);
        analogWrite(PWM1Pin, PWMduty);
        analogWrite(PWM2Pin, PWMduty);
        if(flag == 0){
          Serial.println("Right");
          flag=1;
        }
    }

    // if the state is 's', robot stops(brakes)
    else if (state[0] == 's' || stateStop == 1) {
        digitalWrite(motor1Pin1, HIGH);
        digitalWrite(motor1Pin2, HIGH); 
        digitalWrite(motor1Pin3, LOW);
        digitalWrite(motor1Pin4, HIGH); 
        digitalWrite(motor2Pin1, HIGH);
        digitalWrite(motor2Pin2, HIGH);
        digitalWrite(motor2Pin3, LOW);
        digitalWrite(motor2Pin4, HIGH);
        if(flag == 0){
          Serial.println("STOP");
          flag=1;
        }
        stateStop=0;
    }

    // if the state is 'a', zero-radius truncate to the left
    else if (state[0] == 'a') {
        digitalWrite(motor1Pin1, HIGH);
        digitalWrite(motor1Pin2, LOW); 
        digitalWrite(motor1Pin3, HIGH);
        digitalWrite(motor1Pin4, HIGH); 
        digitalWrite(motor2Pin1, HIGH);
        digitalWrite(motor2Pin2, LOW);
        digitalWrite(motor2Pin3, HIGH);
        digitalWrite(motor2Pin4, HIGH);
        analogWrite(PWM1Pin, PWMduty);
        analogWrite(PWM2Pin, PWMduty);
        if(flag == 0){
          Serial.println("Left");
          flag=1;
        }
    }

    // if the state is 'x', move backward
    else if (state[0] == 'x') {
        digitalWrite(motor1Pin1, LOW);
        digitalWrite(motor1Pin2, LOW); 
        digitalWrite(motor1Pin3, HIGH);
        digitalWrite(motor1Pin4, HIGH); 
        digitalWrite(motor2Pin1, HIGH);
        digitalWrite(motor2Pin2, LOW);
        digitalWrite(motor2Pin3, HIGH);
        digitalWrite(motor2Pin4, HIGH);
        analogWrite(PWM1Pin, PWMduty);
        analogWrite(PWM2Pin, PWMduty);
        if(flag == 0){
          Serial.println("Backward");
          flag=1;
        }
    }
   
    // if the state is 'q', robot turns left
    // The radius of rotation changes depending on the value of trunAngle

    else if (state[0] == 'q') {
        digitalWrite(motor1Pin1, HIGH);
        digitalWrite(motor1Pin2, LOW); 
        digitalWrite(motor1Pin3, HIGH);
        digitalWrite(motor1Pin4, HIGH);
        digitalWrite(motor2Pin1, LOW);
        digitalWrite(motor2Pin2, LOW);
        digitalWrite(motor2Pin3, HIGH);
        digitalWrite(motor2Pin4, HIGH);
        analogWrite(PWM1Pin, PWMduty);
        analogWrite(PWM2Pin, (PWMduty)*turnAngle);

        if(flag == 0){
          Serial.println("Forward Left");
          flag=1;
        }
    }

    // if the state is 'e', robot turns right
    // The radius of rotation changes depending on the value of trunAngle
    else if (state[0] == 'e') {
        digitalWrite(motor1Pin1, HIGH);
        digitalWrite(motor1Pin2, LOW); 
        digitalWrite(motor1Pin3, HIGH);
        digitalWrite(motor1Pin4, HIGH); 
        digitalWrite(motor2Pin1, LOW);
        digitalWrite(motor2Pin2, LOW);
        digitalWrite(motor2Pin3, HIGH);
        digitalWrite(motor2Pin4, HIGH);
        analogWrite(PWM1Pin, PWMduty*turnAngle);
        analogWrite(PWM2Pin, PWMduty);
        if(flag == 0){
          Serial.println("Forward Right");
          flag=1;
        }
    }
    // if the state is 'z', move backward left
    // The radius of rotation changes depending on the value of trunAngle

    else if (state[0] == 'z') {
        digitalWrite(motor1Pin1, LOW);
        digitalWrite(motor1Pin2, LOW); 
        digitalWrite(motor1Pin3, HIGH);
        digitalWrite(motor1Pin4, HIGH); 
        digitalWrite(motor2Pin1, HIGH);
        digitalWrite(motor2Pin2, LOW);
        digitalWrite(motor2Pin3, HIGH);
        digitalWrite(motor2Pin4, HIGH);
        analogWrite(PWM1Pin, PWMduty*turnAngle);
        analogWrite(PWM2Pin, PWMduty);
        if(flag == 0){
          Serial.println("Back Left");
          flag=1;
        }
    }

    // if the state is 'c', move backward right
    // The radius of rotation changes depending on the value of trunAngle
    else if (state[0] == 'c') {
        digitalWrite(motor1Pin1, LOW);
        digitalWrite(motor1Pin2, LOW);
        digitalWrite(motor1Pin3, HIGH);
        digitalWrite(motor1Pin4, HIGH); 
        digitalWrite(motor2Pin1, HIGH);
        digitalWrite(motor2Pin2, LOW);
        digitalWrite(motor2Pin3, HIGH);
        digitalWrite(motor2Pin4, HIGH);
        analogWrite(PWM1Pin, PWMduty);
        analogWrite(PWM2Pin, (PWMduty)*turnAngle);

        if(flag == 0){
          Serial.println("Back Right");
          flag=1;
        }
    }

    else if (state[0] == 'v') {
        char a[3];
        String str;
        a[0] = state[1];
        a[1] = state[2];
        a[2] = state[3];
        str = a;
        PWMduty =str.toInt();
        if(flag == 0){
          Serial.println("PWM change");
          flag=1;
        }
    }

    else if (state[0] == 'b') {
        char a[3];
        String str;
        a[0] = state[1];
        a[1] = state[2];
        a[2] = state[3];
        str = a;      
        turnAngle=str.toFloat(); 
        if(flag == 0){
          Serial.println("Angle Change");
          flag=1;
        }
    }    
}

The code is written based on the circuit connections in the previous post. Also, the code is not intended to operate alone but to receive signals from another program (or controller) to control the motor. It controls the motor according to the messages it receives from the other program via serial communication.


The corresponding movements for each message are,


w: Forward

s: Stop

d: Turn zero radius to the right

a: Turn zero radius to the left

x: Backward

q: Turn left (turning radius follows set value)

e: Turn right (turning radius follows the set value)

z: Turn left to the rear (turning radius follows the set value)

c: Turn right to the rear (turning radius follows the set value)

vxxx: xxx is an integer, whose value will be the Arduino PWM setpoint (rotation speed). Ex: v070 is PWM duty 70

bxxx: xxx is an integer, where the value will be the speed ratio of the left/right wheels when the robot is turning. Ex: b020 means that when turning left, the speed of the left wheel is 20% of the right wheel)


I have attached a Windows program that uses the keyboard to send the above message. In the case of the LattePanda Delta, since Windows is available and the Arduino Leonardo is always connected to the 'COM4' port, installing this program and the LabVIEW runtime engine in the LattePanda Delta Windows will allow you to control the robot with the keyboard.


This program was developed with LabVIEW 2021 SP1 (32-bit) and requires the same or a higher version of the runtime engine.





< UI of piloting programs>


5 views0 comments

Comments