joi, 29 ianuarie 2015

Fun with Arduino RC control

Well ... it's a fine evening to play with some Arduino coding. I've just finished to put toghether a Zumo shield and a RC receiver ... it looks promising so far.

The test code was taken from Pololu website just to check if everything works, but I intend to recode the little robot with some heavy complex code similar to MultiWiiCopter one.

Untill then ... wish you happy coding :).



THE TEST CODE

#include <ZumoMotors.h>

#define THROTTLE_PIN 4 // throttle channel from RC receiver
#define STEERING_PIN 5 // steering channel from RC receiver
#define LED_PIN 13 // user LED pin

#define MAX_SPEED 400 // max motor speed
#define PULSE_WIDTH_DEADBAND 25 // pulse width difference from 1500 us (microseconds) to ignore (to compensate for control centering offset)
#define PULSE_WIDTH_RANGE 350 // pulse width difference from 1500 us to be treated as full scale input (for example, a value of 350 means
// any pulse width <= 1150 us or >= 1850 us is considered full scale)


void setup()
{
pinMode(LED_PIN, OUTPUT);

// uncomment one or both of the following lines if your motors' directions need to be flipped
//motors.flipLeftMotor(true);
//motors.flipRightMotor(true);
}

void loop()
{
int throttle = pulseIn(THROTTLE_PIN, HIGH);
int steering = pulseIn(STEERING_PIN, HIGH);

int left_speed, right_speed;

if (throttle > 0 && steering > 0)
{
// both RC signals are good; turn on LED
digitalWrite(LED_PIN, HIGH);

// RC signals encode information in pulse width centered on 1500 us (microseconds); subtract 1500 to get a value centered on 0
throttle -= 1500;
steering -= 1500;

// apply deadband
if (abs(throttle) <= PULSE_WIDTH_DEADBAND)
throttle = 0;
if (abs(steering) <= PULSE_WIDTH_DEADBAND)
steering = 0;

// mix throttle and steering inputs to obtain left & right motor speeds
left_speed = ((long)throttle * MAX_SPEED / PULSE_WIDTH_RANGE) - ((long)steering * MAX_SPEED / PULSE_WIDTH_RANGE);
right_speed = ((long)throttle * MAX_SPEED / PULSE_WIDTH_RANGE) + ((long)steering * MAX_SPEED / PULSE_WIDTH_RANGE);

// cap speeds to max
left_speed = min(max(left_speed, -MAX_SPEED), MAX_SPEED);
right_speed = min(max(right_speed, -MAX_SPEED), MAX_SPEED);
}
else
{
// at least one RC signal is not good; turn off LED and stop motors
digitalWrite(LED_PIN, LOW);

left_speed = 0;
right_speed = 0;
}

ZumoMotors::setSpeeds(left_speed, right_speed);
}


Niciun comentariu:

Trimiteți un comentariu