Skip to content

Instantly share code, notes, and snippets.

@tominated
Last active January 1, 2016 23:19
Show Gist options
  • Save tominated/8216236 to your computer and use it in GitHub Desktop.
Save tominated/8216236 to your computer and use it in GitHub Desktop.
Basic motor control using an RC receiver with a Baby Orangutan robot controller
#include <pololu/orangutan.h>
#define RIGHT_STICK_V 0
#define RIGHT_STICK_V_MIN 3000
#define RIGHT_STICK_V_MAX 4500
#define LEFT_STICK_V 1
#define LEFT_STICK_V_MIN 3000
#define LEFT_STICK_V_MAX 4500
#define RIGHT_STICK_H 2
#define RIGHT_STICK_H_MIN 2675
#define RIGHT_STICK_H_MAX 4350
// Re-maps a number from one range to another.
// Taken from http://arduino.cc/en/reference/map
long map(long x, long in_min, long in_max, long out_min, long out_max) {
return (x - in_min) * (out_max - out_min) / (in_max - in_min) + out_min;
}
// Constrains a number to be within a range
long constrain(long x, long min, long max) {
if (x < min) return min;
if (x > max) return max;
return x;
}
int main() {
// Set PD2, PD4 & PD7 as PWM inputs for the RC receiver
pulse_in_start((unsigned char[]) {IO_D2, IO_D4, IO_D7}, 3);
unsigned long pulse_width;
int speed;
while(1) {
// Get the pulse width for the left stick vertical position and make
// sure it's within the specified range
pulse_width = get_last_high_pulse(LEFT_STICK_V);
pulse_width = constrain(pulse_width, LEFT_STICK_V_MIN, LEFT_STICK_V_MAX);
// Map the pulse width to the speed value (between -255 and 255)
speed = map(pulse_width, LEFT_STICK_V_MIN, LEFT_STICK_V_MAX, -255, 255);
// Set the first motor speed
set_m1_speed(speed);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment