Friday, July 2, 2010

Report #2nd of July 2010

[b]*** NEWS :[/b] 3 more members have joined us :-) Two have been to Kick-start meetings before, but one(Ehab) is a new member :-)

We are already finished with programing the micro-controller(Arduino card) and right now we are working on the navigation program with Java. The program writes forth and read back from USB port.
The following code is developed in Arduino C language and is written for two motors which one(MOTOR#2) is connected to the front wheels controlling LEFTWARDS & RIGHTWARDS movement and the other(MOTOR#1) which controls the SPEED, and FORWARDS & BACKWARDS movement. The program can receive characters from USB port which according to those will enter different phases :

F - Moving Forward(Change the mode in Motor#1)
B - Moving Backward(Change the mode in Motor#1)
S - Moving Straight(Change the mode in Motor#2)
R -Moving Right(Change the mode in Motor#2 & does not change anything about Motor#1)
L - Moving Left(Change the mode in Motor#2  & does not change anything about Motor#1)
r - Resets everything & STOPS all the motors
V(speed)# - Sets the speed(PWM value) - Ex : V45# OBS : Be careful not to crash into the wall, driving with too high speed :-D We did it :-D


This ability to read & write from the USB port has made it easier to control the Arduino program running on the board with a Java program. When writing the Java navigation program we came to situations which we found out that serial communication is slow and even having one print command before setting a value can mean a lot in delay concept. See following example :
[code]---------------------------------------------- EXAMPLE
Code sample 1 :
      // reset, r    
    case 114:
      Serial.print( "Reseting the system");
      initFrontMotor();
      digitalWrite(13, LOW);
      digitalWrite(ENM1, LOW);
      digitalWrite(ENM2, LOW);
      break;
Code sample 2 :
      // reset, r    
    case 114:
      digitalWrite(13, LOW);
      digitalWrite(ENM1, LOW);
      digitalWrite(ENM2, LOW);
      Serial.print( "Reseting the system");
      initFrontMotor();
      break;
----------------------------------------------END OF EXAMPLE[/code]
Comparing these two pieces of our code shows that moving "a print method and a small function" made a significant difference in the delay we had stopping the test subject(Thanks to Ole we fixed this bug:-)). So be careful when you writing back & forth to USB port, there is a delay and [b]every single extra line[/b] can make your test subject to crash into something or fall off the table ;-)

[b]OUR ARDUINO CODE : [/b]
[code]

boolean motorEnable = false;



//OUTPUT PINS
byte IN1M1 = 2;//IN1 Motor1
byte IN2M1 = 4;//IN2 Motor1
byte IN1M2 = 11;//IN1 Motor2
byte IN2M2 = 12;//IN2 Motor2
byte ENM1 = 5;//enable signal Motor1
byte ENM2 = 10;//enable signal Motor2
byte PWMM1 = 3; //PWM Motor1
byte PWMM2 = 9; //PWM Motor2
byte ledPin =  13;    // LED connected to digital pin 13

//Analog
int sensorPin = 2;    // select the input pin for the potentiometer
double sensorValue = 0.00;  // variable to store the value coming from the sensor
double oldSensorValue;
int counter = 0;

void setup()
{
  Serial.begin(115200);

  //Declaring outputs
  pinMode(IN1M1, OUTPUT);  //  MOTOR1 IN1
  pinMode(IN2M1, OUTPUT);  //  MOTOR1 IN2
  pinMode(IN1M2, OUTPUT);  //  MOTOR2 IN1
  pinMode(IN2M2, OUTPUT);  //  MOTOR2 IN2
  pinMode(ENM1, OUTPUT);  //  MOTOR1 Enable
  pinMode(ENM2, OUTPUT);  //  MOTOR2 Enable
  pinMode(PWMM1, OUTPUT);  //  MOTOR 1 PWM
  pinMode(PWMM2, OUTPUT);  //  MOTOR 2 PWM
  pinMode(ledPin, OUTPUT); //  LED

  //INITILIZING
  digitalWrite(IN1M1, LOW);
  digitalWrite(IN2M1, LOW);
  digitalWrite(IN1M2, LOW);
  digitalWrite(IN2M2, LOW);
  digitalWrite(ENM1, LOW);
  digitalWrite(ENM2, LOW);
  digitalWrite(PWMM1, LOW);
  digitalWrite(PWMM2, LOW);
  digitalWrite(ledPin, LOW);
 initFrontMotor();
  Serial.println( "Starting ..." );
}

//********************************************************
//********************************************************
//***************** LOOP *********************************

void loop()
{
  //Serial.print("x");
  if (Serial.available() > 0 )
    comm();
  //micRead();
  //Serial.print( "We are here !!!" );
/*  digitalWrite(IN1M1, HIGH);
  digitalWrite(IN2M1, LOW);
  digitalWrite(IN1M2, HIGH);
  digitalWrite(IN2M2, LOW);
  digitalWrite(ENM1, HIGH);
  digitalWrite(ENM2, HIGH);
  //digitalWrite(PWMM1, HIGH);
  analogWrite(PWMM1, 150);
  //digitalWrite(PWMM2, HIGH);
  analogWrite(PWMM2, 80);
  digitalWrite(ledPin, HIGH);*/
}
//***************** LOOP *********************************
//********************************************************
//***************** COMM     *****************************

void comm()
{
  int lastPosition = 0;
  byte value = Serial.read();
  int counter = 1;
  int sum = 0;
  char temp;

    switch(value)
    {
      // reset, r    
    case 114:
      digitalWrite(13, LOW);
      digitalWrite(ENM1, LOW);
      digitalWrite(ENM2, LOW);
      Serial.print( "Reseting the system");
      initFrontMotor();
      break;
    
    case 86://Velocity, V pressed
       Serial.print( "Velocity set to :" );
       temp = Serial.read();
       while (temp != '#'){
         sum = (10 * sum) + (temp - 48);
         //counter = counter * 10;
         temp = Serial.read();
       }
       Serial.print(sum);
       analogWrite(PWMM1, sum);
       break;    
    
    case 70://Forward, F pressed
       Serial.println( "F pressed" );
       digitalWrite(IN1M1, LOW);
       digitalWrite(IN2M1, HIGH);
       digitalWrite(ENM1, HIGH);
       //analogWrite(PWMM1, 100);
       digitalWrite(ledPin, HIGH);
       break;
    
    case 66://Back, B pressed
       Serial.println( "B pressed" );    
       digitalWrite(IN1M1, HIGH);
       digitalWrite(IN2M1, LOW);
       digitalWrite(ENM1, HIGH);
       //analogWrite(PWMM1, 100);
       digitalWrite(ledPin, HIGH);
       break;
    
    case 83://Straight, S pressed
       Serial.println( "S pressed" );    
       if (lastPosition == 0)
       return;
       else if (lastPosition == 1){
         digitalWrite(IN1M2, HIGH);
         digitalWrite(IN2M2, LOW);
       }
       else if (lastPosition == 2){
          digitalWrite(IN1M2, HIGH);
          digitalWrite(IN2M2, LOW);
       }
       digitalWrite(ENM2, HIGH);
       analogWrite(PWMM2, 120);
       digitalWrite(ledPin, HIGH);
       delay(50);
       digitalWrite(ENM2, LOW);
       lastPosition = 0;    
       break;    
    
    case 82://Right, R pressed
       Serial.println( "R pressed" );
       lastPosition = 1;
       digitalWrite(IN1M2, HIGH);
       digitalWrite(IN2M2, LOW);
       digitalWrite(ENM2, HIGH);
       analogWrite(PWMM2, 200);  
       digitalWrite(ledPin, HIGH);
       delay(100);    
       digitalWrite(ENM2, LOW);    
       break;    
    
    case 76://Left, L pressed
       Serial.println( "L pressed" );
       lastPosition = 2;
       digitalWrite(IN1M2, LOW);
       digitalWrite(IN2M2, HIGH);
       digitalWrite(ENM2, HIGH);
       analogWrite(PWMM2, 200);
       digitalWrite(ledPin, HIGH);
       delay(100);
       digitalWrite(ENM2, LOW);    
       break;
    }
}

//***************** COMM     *****************************
//********************************************************
//****************** INITIAL FRONT MOTOR *****************
void initFrontMotor(){
       Serial.println( "Initializing" );
       digitalWrite(IN1M2, HIGH);
       digitalWrite(IN2M2, LOW);
       digitalWrite(ENM2, HIGH);
       analogWrite(PWMM2, 120);
       digitalWrite(ledPin, HIGH);
       delay(50);
       digitalWrite(IN1M2, HIGH);
       digitalWrite(IN2M2, LOW);
       analogWrite(PWMM2, 120);
       digitalWrite(ledPin, HIGH);
       delay(50);
       digitalWrite(ENM2, LOW);
}
[/code]
Members developing the code : Puya, Shahab B., Ramtin, Ehab, Shahab F. + Special thanks to Ole for help :-)
Up to now no problems that we could not solve and we have found our way with reverse engineering, asking others for help and creativity :-)

Soon we are finished developing the navigation program and will post it here ...

[b]*** REMEMBER THAT IT IS NEVER LATE TO JOIN US. WE CAN TEACH YOU WHAT WE HAVE BEEN THROUGH & WE ARE LEARNING BY TRYING, SO COME & JOIN. OUR GOAL IS TO TRAIN MEMBERS FOR FURTHER PROJECTS IN FALL 2010 ***[/b]

We also have had a workshop about 3D design in Solidworks & Arduino programing.

No comments:

Post a Comment