Go Down

Topic: Mecanum Drive (Adafruit Motor Shield) (Read 3681 times) previous topic - next topic

Rob K

I have started a little backwards on this and have designed the wheels before starting on my actual sketch.

3d-printable_mecanum_wheel Well it is a set, not just one wheel.

I plan to control this using 2 analog joysticks for all of the movement.

Question is how do I control motor direction on the Adafruit motor shield using a analog joystick? I have seen examples for the motor shield on using it for PIR robots, but more of a motor forward motor backwards. I want speed control.

KyleK

What kind of analog joystick? a game port one? something knicked from an arcade machine? An analog joystick is just two 100k ohm potentiometers, one for X axis and the other for the Y axis. Well, the value might not be 100k depending on where the stick is intended to be used.

I think you can read the position of the joystick via 2 of the analog ports. Just need to calibrate the values for center and the extremes.

Rob K

#2
Apr 18, 2010, 10:15 am Last Edit: Apr 18, 2010, 10:20 am by Rob_K Reason: 1
Just analog joysticks pulled from a old PS1 controller. 2.5K or so.


http://www.flickr.com/photos/20102150@N06/4494809794/in/set-72157623717071761/


Rob K

#3
Apr 20, 2010, 03:50 am Last Edit: Apr 20, 2010, 09:22 am by Rob_K Reason: 1
This is what I have working so far.

   
Code: [Select]
#include <AFMotor.h>  // imported AFMotor library

   AF_DCMotor motor(3);  

   
   int SPEED = 0;
   int val = 0;

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

     motor.run(RELEASE);
     delay(50);

     
     }
   void moveForward() {
     motor.run(FORWARD);
       motor.setSpeed(SPEED);
       Serial.print(" ");
       Serial.print( SPEED);
   }

   void moveBackward() {
     motor.run(BACKWARD);
       motor.setSpeed(SPEED);
       Serial.print(" ");
       Serial.print( SPEED);

   }

   void loop() {
     int val = analogRead(1);
       if (val > 540) {
       // move faster the higher the value from the potentiometer
       SPEED = 2048 - 1024 * val / 512 + 1;
       moveForward();
       
     } else if (val < 480) {
       // move faster the lower the value from the potentiometer
       SPEED = 1024 * val / 512 + 1;
       moveBackward();
       
     } else {
       motor.run(RELEASE);
     }


My values are off. Mid point between forward and middle and backward and middle are the high speeds. modifying it for 2 joysticks makes it very buggy, but usable.

Rob K

I got more help from the Adafruit forms.

Video http://www.flickr.com/photos/20102150@N06/4538224965/?processed=1&cb=1271792057427

Basic tank steering.

Code: [Select]

   #include <AFMotor.h>  // imported AFMotor library

   AF_DCMotor motor(3);  
   AF_DCMotor motor1(4);      
   
   void setup() {
     
   }  

   void loop() {
     int speed = analogRead(1);
     speed = map(speed, 0, 1023, -255, 255);
 if (speed < 0)
   {
     motor.run(BACKWARD);
     motor.setSpeed(abs(speed));  
   }
 if (speed > 0)
   {
     motor.run(FORWARD);
     motor.setSpeed(speed);
   }
   int speed2 = analogRead(0);
     speed2 = map(speed2, 0, 1023, -255, 255);
 if (speed2 < 0)
   {
     motor1.run(BACKWARD);
     motor1.setSpeed(abs(speed2));  
   }
 if (speed2 > 0)
   {
     motor1.run(FORWARD);
     motor1.setSpeed(speed2);
   }
   
 }  


Rob K

#5
May 04, 2010, 08:36 pm Last Edit: May 04, 2010, 08:43 pm by Rob_K Reason: 1
Here is a video of the side-to-side testing.

[media]http://www.youtube.com/watch?v=riWGafaGTV0[/media]
[size=10]I was the camera man, another guy with no experience was controlling it.[/size]

And the code for that part.

Code: [Select]

   #include <AFMotor.h>  // imported AFMotor library

   AF_DCMotor motor1(1);  
   AF_DCMotor motor2(2);    
   AF_DCMotor motor3(3);  
   AF_DCMotor motor4(4);      
   
   void setup() {
     
   }  

   void loop() {
     int speed = analogRead(2);
     speed = map(speed, 0, 1023, -255, 255);
 if (speed < 0)
   {
     motor2.run(FORWARD);
     motor2.setSpeed(abs(speed));  
     motor3.run(BACKWARD);
     motor3.setSpeed(abs(speed));  
   }
 if (speed > 0)
   {
     motor2.run(BACKWARD);
     motor2.setSpeed(speed);
     motor3.run(FORWARD);
     motor3.setSpeed(speed);
   }
   int speed2 = analogRead(3);
     speed2 = map(speed2,  0, 1023, -255, 255);
 if (speed2 < 0)
   {
     motor1.run(BACKWARD);
     motor1.setSpeed(abs(speed2));
     motor4.run(FORWARD);
     motor4.setSpeed(abs(speed2));  
   }
 if (speed2 > 0)
   {
     motor1.run(FORWARD);
     motor1.setSpeed(speed2);
     motor4.run(BACKWARD);
     motor4.setSpeed(speed2);
   }
   
 }

   

Go Up