Sorry for the massive code block, but I'm on the way to work, and Im on a phone at the moment. This is the exact code we are using. If you think something I could change would make it ping able faster, please recommend it to me:
#include <Ethernet.h>
#include <SPI.h>
byte mac[] = {0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED}; //The media access control address for the ethernet shield
byte ip[] = {10, 0, 1, 2}; //the IP address for the ethernet shield
#define PORT (1337) //The port the ethernet recieves packets on
#define DEBUG true //Output running data to computer serial terminal
//Packet Definitions
#define PACKET_movement (1)
//Avoid pins 5 and 6 for PWM as they have a known issues with PWM.
//While using the ethernet shield, be wary of pins 10 and 4:
////Note that because the W5100 (ethernet shield) and SD card share the SPI bus, only one can be active
////at a time. If you are using both peripherals in your program, this should be taken care of by the
////corresponding libraries. If you're not using one of the peripherals in your program, however,
////you'll need to explicitly deselect it. To do this with the SD card, set pin 4 as an output and
////write a high to it. For the W5100, set digital pin 10 as a high output.
#define ETHERNET_SHIELD_DISABLE (10) //DONT TOUCH THIS PIN!!!! Set this pin HIGH to disable the ethernet shield.
#define SD_CARD_DISABLE (4) //Set this pin HIGH to disable the SD card
#define PIN_MOTOR_1_PWM_LEFT (2)
#define PIN_MOTOR_1_PWM_RIGHT (3)
#define PIN_MOTOR_1_ENABLE (5)
#define PIN_MOTOR_2_PWM_LEFT (8)
#define PIN_MOTOR_2_PWM_RIGHT (9)
#define PIN_MOTOR_2_ENABLE (7)
#define PIN_MOTOR_3_PWM_LEFT (11)
#define PIN_MOTOR_3_PWM_RIGHT (12)
#define PIN_MOTOR_3_ENABLE (13)
//Movement Variables
#define MAX_MOVEMENT_VALUE (127) //Max value for X, Y, or Rotation sent from client computer
#define MAX_X_PWM (60)
#define MAX_Y_PWM (255 / 4) //Max Speed is 0, and not moving is 255
#define MAX_ROTATION_PWM (30)
#define motor1Bias (1)
#define motor2Bias (1)
#define motor3Bias (1)
EthernetServer server = EthernetServer(PORT);
void setup() {
Ethernet.begin(mac, ip); //Initialize the ethernet device
server.begin(); //Start listening for clients on the Ethernet
if (DEBUG) {
Serial.begin(9600); //Start serial communication to computer
Serial.println("Serial has begun");
}
//Setup Pins
// pinMode(SD_CARD_DISABLE, OUTPUT); //Must set this PIN high since we aren't using the SD Card and it
// digitalWrite(SD_CARD_DISABLE, HIGH); //shares a serial port with the Ethernet Shield.
pinMode(PIN_MOTOR_1_PWM_LEFT, OUTPUT);
pinMode(PIN_MOTOR_1_PWM_RIGHT, OUTPUT);
pinMode(PIN_MOTOR_1_ENABLE, OUTPUT);
pinMode(PIN_MOTOR_2_PWM_LEFT, OUTPUT);
pinMode(PIN_MOTOR_2_PWM_RIGHT, OUTPUT);
pinMode(PIN_MOTOR_2_ENABLE, OUTPUT);
pinMode(PIN_MOTOR_3_PWM_LEFT, OUTPUT);
pinMode(PIN_MOTOR_3_PWM_RIGHT, OUTPUT);
pinMode(PIN_MOTOR_3_ENABLE, OUTPUT);
//Enable all H-Bridges
digitalWrite(PIN_MOTOR_1_ENABLE, LOW);
digitalWrite(PIN_MOTOR_2_ENABLE, LOW);
digitalWrite(PIN_MOTOR_3_ENABLE, LOW);
//Outputing a solid 1 to the H-Bridges turns wheel off
analogWrite(PIN_MOTOR_1_PWM_LEFT, 255);
analogWrite(PIN_MOTOR_1_PWM_RIGHT, 255);
analogWrite(PIN_MOTOR_2_PWM_LEFT, 255);
analogWrite(PIN_MOTOR_2_PWM_RIGHT, 255);
analogWrite(PIN_MOTOR_3_PWM_LEFT, 255);
analogWrite(PIN_MOTOR_3_PWM_RIGHT, 255);
//################################################################
//Just for testing purposes. Normally should be commented out.
//################################################################
digitalWrite(PIN_MOTOR_1_ENABLE, HIGH);
digitalWrite(PIN_MOTOR_2_ENABLE, HIGH);
digitalWrite(PIN_MOTOR_3_ENABLE, HIGH);
analogWrite(PIN_MOTOR_1_PWM_LEFT, 200);
analogWrite(PIN_MOTOR_1_PWM_RIGHT, 255);
analogWrite(PIN_MOTOR_2_PWM_LEFT, 200);
analogWrite(PIN_MOTOR_2_PWM_RIGHT, 255);
analogWrite(PIN_MOTOR_3_PWM_LEFT, 200);
analogWrite(PIN_MOTOR_3_PWM_RIGHT, 255);
//################################################################
}
void loop() {
EthernetClient client = server.available();
if (client) { //Is there a client connected?
if (client.connected()) { //If we have a connection
if (client.available()) {
int packetType = client.read();
handlePacket(client, packetType);
}
}
}
}
void handlePacket(EthernetClient client, int packetType) {
switch (packetType) {
case PACKET_movement:
HandlePacket_movement(client);
break;
}
}
void HandlePacket_movement(EthernetClient client) {
//prog_char is a signed 8 bit int.
//we are recieving a values from -127 to +127
prog_char xFromServer = client.read();
prog_char yFromServer = client.read();
prog_char rotationFromServer = client.read();
//Make values relative to set MAX values so as not to overheat H-Bridges
int X = (MAX_X_PWM * xFromServer) / MAX_MOVEMENT_VALUE;
int Y = (MAX_Y_PWM * yFromServer) / MAX_MOVEMENT_VALUE;
int rotation = (MAX_ROTATION_PWM * rotationFromServer) / MAX_MOVEMENT_VALUE;
int totalMotor1 = 0;
int totalMotor2 = 0;
int totalMotor3 = 0;
//X Calculations
totalMotor1 += X * 1.2;
totalMotor2 += -X;
totalMotor3 += -X;
//Y Calculations
//Motors 2 and 3 are used for going in the Y direction
totalMotor2 += Y;
totalMotor3 += -Y;
//Rotation Calculations
//All motors are used for rotation
totalMotor1 += rotation;
totalMotor2 += rotation;
totalMotor3 += rotation;
//Set wheel biases
totalMotor1 = totalMotor1 * motor1Bias;
totalMotor2 = totalMotor2 * motor2Bias;
totalMotor3 = totalMotor3 * motor3Bias;
//Motor 1
serial_print("Motor 1: ");
serial_println(totalMotor1);
if (totalMotor1 > 0) {
totalMotor1 = 255 - totalMotor1; //The H-Bridges we use have an active low PWM so lets take that into account
digitalWrite(PIN_MOTOR_1_ENABLE, HIGH);
analogWrite(PIN_MOTOR_1_PWM_RIGHT, totalMotor1);
analogWrite(PIN_MOTOR_1_PWM_LEFT, 255);
} else if (totalMotor1 < 0) {
totalMotor1 = 255 + totalMotor1;
digitalWrite(PIN_MOTOR_1_ENABLE, HIGH);
analogWrite(PIN_MOTOR_1_PWM_RIGHT, 255);
analogWrite(PIN_MOTOR_1_PWM_LEFT, totalMotor1);
} else {
analogWrite(PIN_MOTOR_1_PWM_RIGHT, 255);
analogWrite(PIN_MOTOR_1_PWM_LEFT, 255);
digitalWrite(PIN_MOTOR_1_ENABLE, LOW);
}
//Motor 2
serial_print("Motor 2: ");
serial_println(totalMotor2);
if (totalMotor2 > 0) {
totalMotor2 = 255 - totalMotor2;
digitalWrite(PIN_MOTOR_2_ENABLE, HIGH);
analogWrite(PIN_MOTOR_2_PWM_RIGHT, totalMotor2);
analogWrite(PIN_MOTOR_2_PWM_LEFT, 255);
} else if (totalMotor2 < 0) {
totalMotor2 = 255 + totalMotor2;
digitalWrite(PIN_MOTOR_2_ENABLE, HIGH);
analogWrite(PIN_MOTOR_2_PWM_RIGHT, 255);
analogWrite(PIN_MOTOR_2_PWM_LEFT, totalMotor2);
} else {
digitalWrite(PIN_MOTOR_2_ENABLE, LOW);
analogWrite(PIN_MOTOR_2_PWM_RIGHT, 255);
analogWrite(PIN_MOTOR_2_PWM_LEFT, 255);
}
//Motor 3
serial_print("Motor 3: ");
serial_println(totalMotor3);
if (totalMotor3 > 0) {
totalMotor3 = 255 - totalMotor3;
digitalWrite(PIN_MOTOR_3_ENABLE, HIGH);
analogWrite(PIN_MOTOR_3_PWM_RIGHT, totalMotor3);
analogWrite(PIN_MOTOR_3_PWM_LEFT, 255);
} else if (totalMotor3 < 0) {
totalMotor3 = 255 + totalMotor3;
digitalWrite(PIN_MOTOR_3_ENABLE, HIGH);
analogWrite(PIN_MOTOR_3_PWM_RIGHT, 255);
analogWrite(PIN_MOTOR_3_PWM_LEFT, totalMotor3);
} else {
digitalWrite(PIN_MOTOR_3_ENABLE, LOW);
analogWrite(PIN_MOTOR_3_PWM_RIGHT, 255);
analogWrite(PIN_MOTOR_3_PWM_LEFT, 255);
}
}
void serial_println(char string[]) {
if (DEBUG) {
Serial.println(string);
}
}
void serial_println(int num) {
if (DEBUG) {
Serial.println(num);
}
}
void serial_print(char string[]) {
if (DEBUG) {
Serial.print(string);
}
}
void serial_print(int num) {
if (DEBUG) {
Serial.print(num);
}
}