View Single Post
  #6   Spotlight this post!  
Unread 12-07-2013, 18:44
cad321 cad321 is offline
Jack of all trades, Master of none
AKA: Brian Wagg
FRC #2386 (Trojans)
Team Role: Alumni
 
Join Date: Jan 2013
Rookie Year: 2012
Location: Burlington, Ontario
Posts: 348
cad321 is just really nicecad321 is just really nicecad321 is just really nicecad321 is just really nice
Re: Arduino Mecanum drive using PWM?

I am planning on taking the pwm signals from my spectrum ar6210 and mixing them with the arduino using the following code to create a robot with a mecanum drive. For the motors I plan on using some continuous rotation servos and I am planning on buying some small mecanum wheels such as the 4in vex wheels.

Code:
#include <Servo.h>

Servo FLS; //front left servo
Servo FRS; //front right servo...
Servo RLS;
Servo RRS;
int Forward = 1; //Inputs from RC receiver
int Strafe = 2;  
int Rotate = 3;  
unsigned long ForwardDuration; //To store the duration of the pulses
unsigned long StrafeDuration;    //coming from the receiver
unsigned long RotateDuration;
unsigned long FL; //Front Left Value
unsigned long FR; //Front Right Value...
unsigned long RL;
unsigned long RR;
unsigned long timeout = 20000;

void setup()
{
  FLS.attach(10);
  FRS.attach(11);
  RLS.attach(12);
  RRS.attach(13);
  pinMode(Forward, INPUT);
  pinMode(Strafe, INPUT);
  pinMode(Rotate, INPUT);
}

void loop()
{
  ForwardDuration = pulseIn(Forward, HIGH, timeout); //read receiver inputs
  StrafeDuration = pulseIn(Strafe, HIGH, timeout);
  RotateDuration = pulseIn(Rotate, HIGH, timeout);
  FL = ForwardDuration + StrafeDuration + RotateDuration;
  FR = ForwardDuration - StrafeDuration - RotateDuration;
  RL = ForwardDuration - StrafeDuration + RotateDuration;
  RR = ForwardDuration + StrafeDuration - RotateDuration;
  if(FL < 1000){
    FL = 1000;
  }else if(FL > 2000){
    FL = 2000;
  }if(FR < 1000){
    FR = 1000;
  }else if(FR > 2000){
    FR = 2000;
  }if(RL < 1000){
    RL = 1000;
  }else if(RL > 2000){
    RL = 2000;
  }if(RR < 1000){
    RR = 1000;
  }else if(RR > 2000){
    RR = 2000;
  }
  FLS.writeMicroseconds(FL);
  FRS.writeMicroseconds(FR);
  RLS.writeMicroseconds(RL);
  RRS.writeMicroseconds(RR);  
}
I am unsure at the moment if this code will work yet as I do not have a bot to test with at the moment, but hopefully I will be able to collect all of the resources I need this summer.