With a lot of help from some great people, I've managed to get my Android phone to control an old FIRST robotics chassis. The Android connects to modem on the robot into which my Ethernet shield equipped Arduino Mega 2560 is connected. Code written by Eric Barch (ttjcrew.com), another FIRST mentor, has been downloaded to the Arduino. Code (with a few of my modifications) is as follows:
/************************************
Arduino Based Robot Control System v0.1
by Eric Barch (ttjcrew.com)
************************************/
#include <SPI.h>
#include <Ethernet.h>
#include <Udp.h>
#include <Servo.h>
//Set the MAC address, static IP, gateway, and subnet of the network
byte mac[] = { 0x90, 0xA2, 0xDA, 0x00, 0x50, 0xAD };
byte ip[] = { 10, 3, 46, 22 };
//UDP Stuff
const int PORT = 4444; //Port of incoming UDP data
const int PACKET_SIZE = 4; //Size of the joystick data packet
byte joystick_data[PACKET_SIZE]; //Byte array for incoming data - [0] = leftY, [1] = leftX, [2] = rightY, [3] = rightX
int wtf = 0;
const int led = 3;
//Robot specific stuff
boolean lastState = false; //Keeps track of when we go between enabled/disabled or vice versa
unsigned long lastUpdate = 0; //Keeps track of the last time (ms) we received data
//Define robot outputs
int pwm01 = 5; //Digital Pin 5
int pwm02 = 6; //Digital Pin 6
int pwm03 = 7; //Digital Pin 7
int pwm04 = 8; //Digital Pin 8
//Speed Controller/Servo Objects
Servo leftDriveFront;
Servo leftDriveRear;
Servo rightDriveFront;
Servo rightDriveRear;
void setup() {
Serial.begin(9600); //Setup serial comms for debugging
// Start Ethernet and UDP
Ethernet.begin(mac,ip);
Udp.begin(PORT);
Serial.println("Robot control system initialized.");
}
void loop() {
xferdata();
//Only allow robot to be enabled if we've received data in the last 100ms and robot is set to enabled
if (((millis() - lastUpdate) <= 100) && (millis() > 500)) //Robot is disabled for first 500ms of runtime
enabled();
else
disabled();
wtf = wtf++;
/* This function's sole purpose is to receive data and shove it into the joystick_data byte array */
void xferdata()
{
if (Udp.available()) {
Udp.readPacket(joystick_data,PACKET_SIZE);
lastUpdate = millis();
}
}
void enabled()
{
//If we were last disabled, we need to attach the PWM outputs
if (lastState == false)
{
leftDriveFront.attach(pwm01);
leftDriveRear.attach(pwm02);
rightDriveFront.attach(pwm03);
rightDriveRear.attach(pwm04);
}
//Output the left/right drive PWMs based on joystick input (0-180)
leftDriveFront.write(map((long)joystick_data[0], 0, 255, 0, 180));
leftDriveRear.write(map((long)joystick_data[0], 0, 255, 0, 180));
rightDriveFront.write(map((long)joystick_data[2], 0, 255,180, 0));
rightDriveRear.write(map((long)joystick_data[2], 0, 255, 180, 0));
analogWrite(led, map((long)joystick_data[0], 0, 255, 0, 180));
//We are enabled
lastState = true;
}
void disabled()
{
//Robot is disabled, detach PWM outputs
leftDriveFront.detach();
leftDriveRear.detach();
rightDriveFront.detach();
rightDriveRear.detach();
//We are disabled
lastState = false;
}
Here's the conundrum....if you leave out my line of wtf=wtf++ in the void loop ---- nothing happens on my system. I do have communication since I can see the transmit and receive lines blink and have put messages in to insure I'm getting to the right places. Keeping everything else the same, if I comment out that line, reload and run -- no motor movement. Uncomment, reload run --- robot runs --very quickly in fact.
I'm not sure what that one line accomplishes, except perhaps some stack overload when it hits the integer limit.
Can any one tell me why the addition of that one statement (delays or other non-incrementing statements don't work) makes things go?
I'm happy I've gotten Eric's program to run on my robot, but I'd like to know what the glitch is.
Thanks,
Doc