Added more comments to MegaRobogaiaPololu sketch

This commit is contained in:
Patrick Goebel 2012-12-16 18:53:49 -08:00
parent a679f6a437
commit aadcd53f50
2 changed files with 41 additions and 7 deletions

View File

@ -0,0 +1,4 @@
NPP_GetValue is called
NPP_GetValue is called
NPP_GetValue is called
NPP_GetValue is called

View File

@ -64,6 +64,7 @@
/* Sensor functions */ /* Sensor functions */
#include "sensors.h" #include "sensors.h"
/* Include servo support if required */
#ifdef USE_SERVOS #ifdef USE_SERVOS
#include <Servo.h> #include <Servo.h>
#include "servos.h" #include "servos.h"
@ -87,7 +88,11 @@
/* Run the PID loop at 30 times per second */ /* Run the PID loop at 30 times per second */
#define PID_RATE 30 // Hz #define PID_RATE 30 // Hz
/* Convert the rate into an interval */
const int PID_INTERVAL = 1000 / PID_RATE; const int PID_INTERVAL = 1000 / PID_RATE;
/* Track the next time we make a PID calculation */
unsigned long nextPID = PID_INTERVAL; unsigned long nextPID = PID_INTERVAL;
/* Stop the robot if it hasn't received a movement command /* Stop the robot if it hasn't received a movement command
@ -95,24 +100,30 @@
#define AUTO_STOP_INTERVAL 2000 #define AUTO_STOP_INTERVAL 2000
long lastMotorCommand = AUTO_STOP_INTERVAL; long lastMotorCommand = AUTO_STOP_INTERVAL;
// Wrap the encoder reading function /* Wrap the encoder reading function */
long readEncoder(int i) { long readEncoder(int i) {
if (i == LEFT) return encoders.YAxisGetCount(); if (i == LEFT) return encoders.YAxisGetCount();
else return encoders.XAxisGetCount(); else return encoders.XAxisGetCount();
} }
// Wrap the encoder reset function /* Wrap the encoder reset function */
void resetEncoder(int i) { void resetEncoder(int i) {
if (i == LEFT) return encoders.YAxisReset(); if (i == LEFT) return encoders.YAxisReset();
else return encoders.XAxisReset(); else return encoders.XAxisReset();
} }
/* Wrap the encoder reset function */
void resetEncoders() { void resetEncoders() {
resetEncoder(LEFT); resetEncoder(LEFT);
resetEncoder(RIGHT); resetEncoder(RIGHT);
} }
/* Wrap the motor driver initialization */
void initMotorController() {
drive.init();
}
// Wrap the drive motor set speed function /* Wrap the drive motor set speed function */
void setMotorSpeed(int i, int spd) { void setMotorSpeed(int i, int spd) {
if (i == LEFT) drive.setM1Speed(spd); if (i == LEFT) drive.setM1Speed(spd);
else drive.setM2Speed(spd); else drive.setM2Speed(spd);
@ -125,16 +136,27 @@
} }
#endif #endif
/* Variable initialization */
// A pair of varibles to help parse serial commands (thanks Fergs)
int mode = 0; int mode = 0;
int index = 0; int index = 0;
// Variable hold an input character
char chr; char chr;
// Variable to hold the current single-character command
char cmd; char cmd;
// Character arrays to hold the first and second arguments
char argv1[16]; char argv1[16];
char argv2[16]; char argv2[16];
// The arguments converted to integers
long arg1; long arg1;
long arg2; long arg2;
// Clear the current command parameters /* Clear the current command parameters */
void resetCommand() { void resetCommand() {
cmd = NULL; cmd = NULL;
memset(argv1, 0, sizeof(argv1)); memset(argv1, 0, sizeof(argv1));
@ -145,7 +167,7 @@ void resetCommand() {
index = 0; index = 0;
} }
// Run a command /* Run a command. Commands are defined in commands.h */
int runCommand() { int runCommand() {
int i = 0; int i = 0;
char *p = argv1; char *p = argv1;
@ -233,13 +255,16 @@ int runCommand() {
} }
} }
/* Setup function--runs once at startup. */
void setup() { void setup() {
Serial.begin(BAUDRATE); Serial.begin(BAUDRATE);
// Initialize the motor controller if used */
#ifdef USE_BASE #ifdef USE_BASE
drive.init(); initMotorController();
#endif #endif
/* Attach servos if used */
#ifdef USE_SERVOS #ifdef USE_SERVOS
int i; int i;
for (i = 0; i < N_SERVOS; i++) { for (i = 0; i < N_SERVOS; i++) {
@ -248,6 +273,10 @@ void setup() {
#endif #endif
} }
/* Enter the main loop. Read and parse input from the serial port
and run any valid commands. Run a PID calculation at the target
intervals and check for auto-stop conditions.
*/
void loop() { void loop() {
while (Serial.available() > 0) { while (Serial.available() > 0) {
@ -285,13 +314,14 @@ void loop() {
} }
} }
// If we using base control, run a PID loop at the appropriate intervals // If we using base control, run a PID calculation at the appropriate intervals
#ifdef USE_BASE #ifdef USE_BASE
if (millis() > nextPID) { if (millis() > nextPID) {
updatePID(); updatePID();
nextPID += PID_INTERVAL; nextPID += PID_INTERVAL;
} }
// Check to see if we have exceeded the auto-stop interval
if ((millis() - lastMotorCommand) > AUTO_STOP_INTERVAL) {; if ((millis() - lastMotorCommand) > AUTO_STOP_INTERVAL) {;
setMotorSpeeds(0, 0); setMotorSpeeds(0, 0);
moving = 0; moving = 0;