Show Posts
Pages: [1]
1  Topics / Robotics / Re: Raspberry Pi / Arduino Motor Control on: November 15, 2012, 02:40:33 pm
Here is my code:

Code:
int pwm_a = 3;  //PWM control for motor outputs 1 and 2 is on digital pin 3
int pwm_b = 11;  //PWM control for motor outputs 3 and 4 is on digital pin 11

int dir_a = 12;  //direction control for motor outputs 1 and 2 is on digital pin 12
int dir_b = 13;  //direction control for motor outputs 3 and 4 is on digital pin 13

int forward = 4;
int backward = 5;
int left = 6;
int right = 7;

int f_val, b_val, l_val, r_val = 0;

int fast = 255;  // motor speed 100%
int med = 175;   // motor speed 75%
int slow = 100;  // motor speed 39%
int stopped = 0; // motor stopped 0%


void moveForward(int speed)
{

   analogWrite(pwm_a, speed); // set motor speed
   analogWrite(pwm_b, speed);   // set motor speed

   digitalWrite(dir_a, HIGH);  //Set motor direction, 1 low, 2 high
   digitalWrite(dir_b, LOW);  //Set motor direction, 3 high, 4 low

}


void moveBackward(int speed)
{

   analogWrite(pwm_a, speed); // set motor speed
   analogWrite(pwm_b, speed);   // set motor speed

   digitalWrite(dir_a, LOW);  //Set motor direction, 1 low, 2 high
   digitalWrite(dir_b, HIGH);  //Set motor direction, 3 high, 4 low

}


void turnRight(int speed)
{

   analogWrite(pwm_a, speed); // set motor speed
   analogWrite(pwm_b, speed);   // set motor speed

   digitalWrite(dir_a, HIGH);  //Set motor direction, 1 low, 2 high
   digitalWrite(dir_b, HIGH);  //Set motor direction, 3 high, 4 low

}


void turnLeft(int speed)
{

   analogWrite(pwm_a, speed); // set motor speed
   analogWrite(pwm_b, speed);   // set motor speed

   digitalWrite(dir_a, LOW);  //Set motor direction, 1 low, 2 high
   digitalWrite(dir_b, LOW);  //Set motor direction, 3 high, 4 low

}


void stopRobot()
{
    analogWrite(pwm_a, stopped);   // set motor speed
    analogWrite(pwm_b, stopped);   // set motor speed
}



void setup()
{
  Serial.begin(9600);
 
  //Set control pins to be outputs

  pinMode(pwm_a, OUTPUT); 
  pinMode(pwm_b, OUTPUT);
  pinMode(dir_a, OUTPUT);
  pinMode(dir_b, OUTPUT); 

  pinMode(forward, INPUT);
  pinMode(backward, INPUT);
  pinMode(left, INPUT);
  pinMode(right, INPUT);
 
  stopRobot();
}


void loop()
{
  f_val = digitalRead(forward);
  b_val = digitalRead(backward);
  l_val = digitalRead(left);
  r_val = digitalRead(right);
  if (f_val == HIGH)
  {
    moveForward(slow);
  }
  if (b_val == HIGH)
  {
    moveBackward(slow);
  }
  if (l_val == HIGH)
  {
    turnLeft(slow);
  }
  if (r_val == HIGH)
  {
    turnRight(slow);
  }
 
}

As I mentioned before, i'm using http://code.google.com/p/webiopi/ to switch the GPIO (output) pins of my Raspberry on and off which active the INPUT pins on my Arduino.

My Raspberry is currently powered using a 2A 5v USB supply and my Arduino is powered seperately through my motor sheild with 8 AA batteries.

Thanks, Jason.
2  Topics / Robotics / Re: Raspberry Pi / Arduino Motor Control on: November 14, 2012, 04:25:39 am
Thanks PaulS.  The current goes from my Raspberry to my Arduino (to active a motor), with the LED stopping it going backwards, just in case I have my input/output the wrong way around.  It works but is subject to interferance, which can active the motors randomly.  Would reducing the current help? Or do I need a pull down/up resistor?
Thanks, Jason.
3  Topics / Robotics / Re: Raspberry Pi / Arduino Motor Control on: November 13, 2012, 04:00:17 am
Ive got an led for two reasons. One, so that i know when a signal is sent over the line and two, to stop a reverse signal just incase my arduino outputs a signal to my pi. You mentioned a limiting resistor. Do i need this inline in line with the led? My electronics knowledge is not great, but i assumed because the pi outputs 3v and the arduino can receive 3v or 5v this should be ok.
Thanks,
Jason
4  Topics / Robotics / Raspberry Pi / Arduino Motor Control on: November 12, 2012, 12:15:07 pm
Im creating a robot using a raspberry pi for the main processing and arduino with shield for motor control. Ive got them connected but there is interferance and im not sure how to resolve it.
My raspberry pi and arduino are connected to each using the ground pins. Then ive connected one gpio from the pi to a digital input pin on my arduino with an led in between. Ive coded my arduino to move forward when the input pin is high. Which is done using Webiopi through the raspberry pi.
As i mentioned it does work but the input pin on the arduino can be triggered really easily.
I think my circuit is too simplistic.
Can any one help?
Thanks, jason.
Pages: [1]