View Single Post
  #12   Spotlight this post!  
Unread 21-04-2013, 12:31
dtengineering's Avatar
dtengineering dtengineering is offline
Teaching Teachers to Teach Tech
AKA: Jason Brett
no team (British Columbia FRC teams)
Team Role: Mentor
 
Join Date: Jan 2005
Rookie Year: 2004
Location: Vancouver, BC
Posts: 1,831
dtengineering has a reputation beyond reputedtengineering has a reputation beyond reputedtengineering has a reputation beyond reputedtengineering has a reputation beyond reputedtengineering has a reputation beyond reputedtengineering has a reputation beyond reputedtengineering has a reputation beyond reputedtengineering has a reputation beyond reputedtengineering has a reputation beyond reputedtengineering has a reputation beyond reputedtengineering has a reputation beyond repute
Re: Controlling robot with arduino

You can do this cheaply and easily... you just have to think outside the cRio/FRC "box" a bit. I currently have a small "fleet" of vehicles running using Victor 888 controllers and CIM motors attached to Arduino Unos. It works great, and is quite easy to do... I've etched custom shields to attach the speed control pins and joystick (or bluetooth) connections, but you could do this easily enough using a breadboard, and then solder it up onto some perfboard/protoboard. I did have to recalibrate the Victors, as the Arduino was giving me slightly longer pulses than expected, but that is really easy to do... just push the "calibration" button on the Victor and move the joystick full circle to give "max" and "min" signals to the Victor, then let go of the stick.

Here's some code for controlling Victors on Pin8 and Pin9 using "joystick" mode, from a joystick connected to A2 and A3. The code will also print the values to your serial monitor. The downside is that it requires a joystick to be wired to your uno/robot at all times. (Which, in the application we have, where the driver is sitting on the vehicle... works great!)

Code:
#include <Servo.h> //include the Servo library for controlling the Victor speed controls

Servo motor_l;     //left motor servo object will be attached to pin 8
Servo motor_r;     //right motor servo object will be attached to pin 9
const int joy1_x = 2;   //the first joystick's x axis will be on analog input A2
int joy1_x_val;            // the value that we read from joystick 1's x axis
const int joy1_y = 3;   //the first joystick's y axis will be on analog input A3
int joy1_y_val;            // the value that we read from joystick 1's y axis
const int joy1_sw = 3;   //the first joystick's switch will be on digital input 3
const int led = 13;      //an led indicator to show whats going on (there should be one on the arduino board)
int left_motor;      //the value between 0 (full reverse) and 180 (full forward) to send to the left motor
int right_motor;      // the value to send to the right motor (90=stop)

void setup () {
  motor_l.attach(8); //attach the servo object to the correct digital output pins
  motor_r.attach(9);
  
  Serial.begin(9600);
  
  pinMode (led,OUTPUT);
  pinMode (joy1_sw,INPUT_PULLUP);
  digitalWrite (led,HIGH);
  delay (250);
}

void loop () {
  joy1_x_val=map (analogRead(joy1_x),0,1023,0,180); //read the joystick's analog value (0-1023)
  joy1_y_val=map (analogRead(joy1_y),0,1023,0,180); //and map it to the servo range of (0-180)
  left_motor= (joy1_y_val - joy1_x_val)+90;          // then mix the signals for "one stick drive"
  right_motor= (joy1_y_val + joy1_x_val)-90;
  motor_l.write(left_motor);                        //assign the resulting values to the servo pins
  motor_r.write(right_motor);
  
  Serial.print("Joy1x:");
  Serial.print(joy1_x_val);
  Serial.print("\t");
  Serial.print("Joy1y:");
  Serial.print(joy1_y_val);
  Serial.print("\t");
  Serial.print("Left Motor:");
  Serial.print(left_motor);
  Serial.print("\t");
  Serial.print("Right Motor:");
  Serial.println(right_motor);
  
  digitalWrite (led,digitalRead(joy1_sw));
  
  delay (10);
  
}
But this is the code I really like... purchase a bluetooth serial dongle, and connect it to pin0 and pin1 on the Arduino. (Note that you'll have to remove the dongle when programming the Arduino.) The download BlueBots to your Android phone. Pair your phone to the dongle, run BlueBots and you've got wireless control of your robot... from your phone.

By the way, you can get BlueBots Pro for $1.65... definitely worth it to support the developer considering that you'll replace about $1,000 of robot control equipment with a $30 Arduino, $10 bluetooth dongle and an Android phone that you may already own.

Code:
#include <Servo.h>

Servo motorL;
Servo motorR;
int motorLspeed;
int motorRspeed;

int joyX=0;
int joyY=0;
int slide1=0;
int slide2=0;
int checksum=0;
int double_check=0;


unsigned long last_time=millis();
void setup () {
  Serial.begin(9600);
  motorL.attach(8);
  motorR.attach(9);
}
// Read the data packed from Bluebot's joystick mode
void loop () {
  if (Serial.available()>0) {
    double_check=0;
    if (Serial.read()==125) { //125 indicates a joystick packet
      joyX=Serial.read(); // type mismatch issues occur
        if (joyX>128) joyX=joyX-256; //so make sure we get the negatives correct
      joyY=Serial.read();
        if (joyY>128) joyY=joyY-256;
      slide2=Serial.read();
      slide1=Serial.read();
      checksum=Serial.read(); //see Bluebots help file for checksum
      double_check=(125+joyX+joyY+slide1+slide2)%256;
      last_time=millis();
    }
  }
 // Timeout routine to stop motors if signal is lost for 500ms
 // also stops the motors if the checksum indicates lost data
 // This section would be better if I used 2x oversampling and
 // only killed the motors if two checksums in sequence were flawed
 // This section can be commented out, but it does play a safety role
 // in the event that Bluetooth communication is lost
  if ((millis()-last_time)>500||(checksum!=double_check)){
   joyX=0;
   joyY=0;
   slide2=62;
   slide1=62;
   if (millis()-last_time>500)Serial.println("Time Out");
   if (checksum!=double_check)Serial.println("Checksum Error");
  }
  
  // Map and send motor outputs
  motorLspeed=joyY-joyX;
  motorLspeed=map(motorLspeed,-124,124,0,180);
  motorLspeed=constrain(motorLspeed,0,180);
  
  motorRspeed=joyY+joyX;
  motorRspeed=map(motorRspeed,-124,124,0,180);
  motorRspeed=constrain(motorRspeed,0,180);
  
  motorL.write(motorLspeed);
  motorR.write(motorRspeed);
  
  delay(10);
  
/* Debugging print routine
  Serial.print("X");
  Serial.print(joyX);
  Serial.print(" Y");
  Serial.print(joyY);
  Serial.print(" S1 ");
  Serial.print(slide1);
  Serial.print(" S2 ");
  Serial.print(slide2);
  Serial.print(" Chk ");
  Serial.print(checksum); 
  Serial.print(" DblChk ");
  Serial.print((double_check));
  Serial.print(" MotL ");
  Serial.print(motorLspeed);
  Serial.print(" MotR ");
  Serial.println(motorRspeed);
  delay(100);
//*/  
}
And remember to keep your fingers away from the motors and chains/belts when experimenting!

Jason

Last edited by dtengineering : 21-04-2013 at 12:41.