No Description

Robot.ino 3.7KB

    #include<SPI.h> #include<Ethernet.h> #include<avr/wdt.h> //Left motor int leftMotorL_PWM = 45; int leftMotorR_PWM = 44; int leftMotorL_EN = 30; int leftMotorR_EN = 31; //Right motor int rightMotorL_PWM = 9; int rightMotorR_PWM = 6; int rightMotorL_EN = 8; int rightMotorR_EN = 7; int lightPin = 22; int noCommand = 0; byte mac[] = {0x90,0xA2,0xDA,0x0E,0xCF,0xEA}; IPAddress IP(10,13,0,1); IPAddress DNS(8,8,8,8); IPAddress gateway(10,0,0,1); IPAddress netmask(255,0,0,0); EthernetServer server(97); //Servo upDown; //Servo leftRight; //Camera controls int camX = 90; int camY = 90; int delayTime = 250; //In ms void setup() { Serial.begin(9600); Ethernet.begin(mac, IP, DNS, gateway, netmask); //Left motor init pinMode(leftMotorL_PWM, OUTPUT); pinMode(leftMotorR_PWM, OUTPUT); pinMode(leftMotorL_EN, OUTPUT); pinMode(leftMotorR_EN, OUTPUT); //Right motor init pinMode(rightMotorL_PWM, OUTPUT); pinMode(rightMotorR_PWM, OUTPUT); pinMode(rightMotorL_EN, OUTPUT); pinMode(rightMotorR_EN, OUTPUT); //Light pinMode(lightPin, OUTPUT); digitalWrite(lightPin, HIGH); } void loop() { EthernetClient client = server.available(); if (client) { while (client.connected()) { if (client.available()) { noCommand = 0; char c = client.read(); if (c == 'R') //Right motor { int rightSpeed = client.parseInt(); if(rightSpeed > 92) rightMotor(true, map(rightSpeed, 90, 180, 0, 255)); else if(rightSpeed < 88) rightMotor(false, map(rightSpeed, 90, 0, 0, 255)); else rightMotor(true,0); } else if (c == 'L') //Left motor { int leftSpeed = client.parseInt(); if(leftSpeed > 92) leftMotor(true, map(leftSpeed, 90, 180, 0, 255)); else if(leftSpeed < 88) leftMotor(false, map(leftSpeed, 90, 0, 0, 255)); else leftMotor(true,0); } else if (c == 'T') //Light digitalWrite(lightPin, !digitalRead(lightPin)); //Toggles light } else { delay(50); noCommand++; if(noCommand > 5) //If no command is received for (5*50ms), cut the power to the motors and turn off the light { //Protects against losing connection and having the robot drive away leftMotor(true,0); rightMotor(true,0); digitalWrite(lightPin, HIGH); } if(noCommand > 50) //If no command is received for (50*50ms), assume the connection is lost break; //Terminate client } } leftMotor(true,0); rightMotor(true,0); digitalWrite(lightPin, HIGH); } } void leftMotor(bool forward, int Mspeed) //speed is PWM so 0-255 { if(Mspeed == 0) { digitalWrite(leftMotorL_EN, LOW); digitalWrite(leftMotorR_EN, LOW); analogWrite(leftMotorR_PWM, 0); analogWrite(leftMotorL_PWM, 0); return; } else { digitalWrite(leftMotorR_EN, HIGH); digitalWrite(leftMotorL_EN, HIGH); } if(forward) { analogWrite(leftMotorL_PWM, 0); analogWrite(leftMotorR_PWM, Mspeed); } else { analogWrite(leftMotorR_PWM, 0); analogWrite(leftMotorL_PWM, Mspeed); } } void rightMotor(bool forward, int Mspeed) //speed is PWM so 0-255 { if(Mspeed == 0) { digitalWrite(rightMotorL_EN, LOW); digitalWrite(rightMotorR_EN, LOW); analogWrite(rightMotorR_PWM, 0); analogWrite(rightMotorL_PWM, 0); return; } else { digitalWrite(rightMotorR_EN, HIGH); digitalWrite(rightMotorL_EN, HIGH); } if(forward) { analogWrite(rightMotorR_PWM, 0); analogWrite(rightMotorL_PWM, Mspeed); } else { analogWrite(rightMotorL_PWM, 0); analogWrite(rightMotorR_PWM, Mspeed); } }