Last active
November 17, 2017 22:31
-
-
Save scruss-elmwood/f442fc8ca1d4cfba58afb3fc11e2f36d to your computer and use it in GitHub Desktop.
Red Rover Trundle Plot - tape a marker to your Red Rover and it draws a flower-like figure
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
Red Rover Trundle Plot - tape a marker to your Red Rover | |
and it draws a flower-like figure | |
Plot **should** stay within 450 × 600 mm (US Arch C) pad | |
and likely will fit ISO A2 pads, but **test first** | |
and don't blame us for any stainage … | |
Rover motors appear on Arduino as two servos on pins 5 & 6 | |
scruss - 2017-11 - for Elmwood Electronics, elmwood.to | |
Product page: | |
https://elmwoodelectronics.ca/products/red-rover-robot-chassis-kit-with-dc-motors-and-motor-controller | |
Blog post with more information: | |
https://elmwoodelectronics.ca/blogs/news/red-rover-red-rover-well-send-it-right-over | |
*/ | |
#include <Servo.h> | |
Servo lefty, righty; | |
/* | |
drive - helper function to map motor speed (-100…100%) to | |
Servo.write() input range (0…180°) | |
Input: | |
< 0 : reverse | |
0 : stop | |
> 0 : forward | |
Output: | |
constrained to valid Servo.write() input range | |
Red Rover controller has effective deadband/ stop band | |
of 87…93°, but requesting very low speeds may not | |
result in expected motion | |
Uses integer maths, so expect rounding/truncation. | |
*/ | |
int drive(int speed) { | |
if (speed < 0) { | |
// reverse: -100…0% → 0…87° | |
// inputs *and* outputs need to be constrained to valid range | |
// as map() does no range checking | |
return constrain(map(constrain(speed, -100, 0), -100, 0, 0, 87), | |
0, 87); | |
} | |
if (speed > 0) { | |
// forward: 0…100% → 93…180° | |
return constrain(map(constrain(speed, 0, 100), 0, 100, 93, 180), | |
93, 180); | |
} | |
// default: stop | |
return 90; | |
} | |
void setboth(int left, int right) { | |
// helper to set both left & right motors between -100…+100% speed | |
lefty.write(drive(left)); | |
righty.write(drive(right)); | |
} | |
void setup() { | |
lefty.attach(5, 1000, 2000); // left servo: pin 5 | |
righty.attach(6, 1000, 2000); // right servo: pin 6 | |
setboth(0, 0); // initial state is stopped | |
} | |
void loop() { | |
// veer right forwards by setting left motor a bit faster (75%) | |
// than the right motor (60%) | |
setboth(75, 60); | |
delay(750); // trundle merrily onwards for ¾s | |
// now stop abruptly | |
setboth(0, 0); | |
delay(250); // give it ¼s settling time | |
// veer left backwards: right now faster than left | |
setboth(-60, -75); | |
delay(750); // same backwards trundle time as forwards, | |
// else Red Rover spirals off into shrubbery | |
// stop abruptly again and settle | |
setboth(0, 0); | |
delay(250); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment