Mecanum Drive (Adafruit Motor Shield)

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.

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.

Just analog joysticks pulled from a old PS1 controller. 2.5K or so.


Imgur

This is what I have working so far.

#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.

I got more help from the Adafruit forms.

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

Basic tank steering.

    #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);
    }
    
  }

Here is a video of the side-to-side testing.

I was the camera man, another guy with no experience was controlling it.

And the code for that part.

    #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);
    }
    
  }