The rover is programmed in C++ like my other projects.
The program runs on the Raspberry Pi and can be started remotely from an SSH terminal on another computer/tablet/phone (I use my iPad) to drive the rover remotely.
The program is very simple and just checks for keyboard input on the arrow keys and turns on and off the 4 control outputs to drive the 2 motors forwards or backwards as required.
You can download a zip file containing the code here.
The program requires pigpio (for GPIO) and ncurses (for terminal I/O) libraries/packages to be installed for it to work.
See the example in my Robot Arm project for instructions on setting these up.
Unzip the code zip file into a folder on your Raspberry Pi.
From the terminal go into the folder and type sh build.sh
to compile the program, then sudo ./drive
to run it.
The code zip file contains the following files:
#include <iostream>
#include "screen.h"
#include "output.h"
// Main program
int main() {
// Catch any errors while the program is running
try {
// Setup screen for I/O
Screen io = Screen();
// Setup the motor outputs
Output leftForward = Output(17);
Output leftReverse = Output(27);
Output rightForward = Output(22);
Output rightReverse = Output(10);
// Check for keys pressed until ESC key
int key = 0;
while (key != KEY_ESC) {
// Get keyboard input
key = getch();
// Check which key is pressed and turn on the correct motors
if (key == KEY_UP) {
io.printLine("forwards");
leftForward.on();
leftReverse.off();
rightForward.on();
rightReverse.off();
}
else if (key == KEY_DOWN) {
io.printLine("reverse");
leftForward.off();
leftReverse.on();
rightForward.off();
rightReverse.on();
}
else if (key == KEY_LEFT) {
io.printLine("left");
leftForward.off();
leftReverse.on();
rightForward.on();
rightReverse.off();;
}
else if (key == KEY_RIGHT) {
io.printLine("right");
leftForward.on();
leftReverse.off();
rightForward.off();
rightReverse.on();
}
else {
// Stop, turn all the motors off
leftForward.off();
leftReverse.off();
rightForward.off();
rightReverse.off();
}
// Sleep for a short time before the next update
io.sleep(0.1);
}
}
catch (const exception& e) {
// Error or program aborted
cerr << "Error: " << e.what() << "\n";
}
return 0;
}