Robot Wifi Cam
Robot Wifi Cam
Robot Wifi Cam
//You can change the TrackFactorUp and TrackFactorDown to adjust the value of
tracking
//If the car deviate too far from the trail line,you can increase the value of
TrackFactorUp and reduce the value of TrackFactorDown
//If the car is shaking very badly, even turn around,you'd better reduce the value
of the TrackFactorUp and increase the value of rackFactorDown
//the default value is 0.5 and 0.7
/*********************************************************
#define TrackFactorUp 0.5
#define TrackFactorDown 0.7
**********************************************************/
//When powered on, the wifi module will start and you will see the blue led flash.
//At the same time the servo will reset to the center Angle you have setted.
//When the blue led stop flash and keep on light,which means the wifi start
successfully
//and you can control your car using our RobotCar APP.
#include <UCMotor.h>
#include <Servo.h>
#include <Wire.h>
#define TRIG_PIN A2
#define ECHO_PIN A3
byte serialIn = 0;
byte commandAvailable = false;
String strReceived = "";
//The current Angle of the two steering engines is used for retransmission
byte servoXPoint = 0;
byte servoYPoint = 0;
byte leftspeed = 0;
byte rightspeed = 0;
Servo servoX;
Servo servoY;
void setup()
{
pinMode(leftSensor, INPUT_PULLUP);
pinMode(middleSensor, INPUT_PULLUP);
pinMode(rightSensor, INPUT_PULLUP);
pinMode(buzzerPin, OUTPUT);
pinMode(ECHO_PIN, INPUT); //Set the connection pin output mode Echo pin
pinMode(TRIG_PIN, OUTPUT);//Set the connection pin output mode trog pin
servoX.attach(9);
servoY.attach(10);
Serial.begin(115200);
delay(5000);
servo_center();
MAX_SPEED_LEFT = SZ_SPEEDTHR;
MAX_SPEED_RIGHT = SZ_SPEEDTHR;
BEEP_OPEN ();
void loop()
{
getSerialLine();
if (commandAvailable) {
processCommand(strReceived);
strReceived = "";
commandAvailable = false;
}
}
void getSerialLine()
{
static int temp = 0;
while (serialIn != '\r')
{
if (!(Serial.available() > 0))
{
if (timeFlag) {
currentTime = micros();
timeFlag = false;
}
if ( micros() - currentTime >= 100000) {
timeFlag = true;
currentTime = micros();
if (detected_flag) {
temp = readPing();
if ( temp <= TURN_DIST && temp > 0 ) {
moveStop(); BEEP_INT();
} else {
if (!beepFlag)
digitalWrite(buzzerPin, LOW);
}
}
}
return;
}
serialIn = Serial.read();
if (serialIn != '\r') {
if (serialIn != '\n') {
char a = char(serialIn);
strReceived += a;
}
}
}
if (serialIn == '\r') {
commandAvailable = true;
serialIn = 0;
}
}
void processCommand(String input)
{
String command = getValue(input, ' ', 0);
static String SZ_speed, SZ_T, SZ_K;
byte iscommand = true;
int val;
if (command == "MD_up")
{
detected_flag = true;
int temp = readPing();
if ( temp <= TURN_DIST && temp > 0 ) {
moveStop(); BEEP_INT();
} else {
digitalWrite(buzzerPin, LOW);
moveForward();
}
if (trackFlag) {
trackStopFlag = true;
}
if (avoidFlag) {
avoidStopFlag = true;
}
}
leftMotor1.setSpeed(MAX_SPEED_LEFT);
rightMotor1.setSpeed(MAX_SPEED_RIGHT);
leftMotor2.setSpeed(MAX_SPEED_LEFT);
rightMotor2.setSpeed(MAX_SPEED_RIGHT);
}
}
else
{
iscommand = false;
}
if (iscommand) {
SendMessage("cmd:" + input);
}
}
String getValue(String data, char separator, int index)
{
int found = 0;
int strIndex[] = { 0, -1 };
int maxIndex = data.length() - 1;
void TrackturnLeft(void)
{
static int MAX_SPEED_LEFT_A, MAX_SPEED_RIGHT_A;
MAX_SPEED_RIGHT_A = MAX_SPEED_RIGHT > (255 - (int)(MAX_SPEED_RIGHT *
TrackFactorUp)) ? 255 : MAX_SPEED_RIGHT + (int)(MAX_SPEED_RIGHT * TrackFactorUp);
MAX_SPEED_LEFT_A = (MAX_SPEED_LEFT - (int)(MAX_SPEED_LEFT * TrackFactorDown)) <
0 ? 0 : MAX_SPEED_LEFT - (int)(MAX_SPEED_LEFT *TrackFactorDown);
motorSet = "LEFT";
leftMotor1.run(BACKWARD);
rightMotor1.run(FORWARD);
leftMotor2.run(BACKWARD);
rightMotor2.run(FORWARD);
leftMotor1.setSpeed(MAX_SPEED_LEFT_A);
rightMotor1.setSpeed(MAX_SPEED_RIGHT_A);
leftMotor2.setSpeed(MAX_SPEED_LEFT_A);
rightMotor2.setSpeed(MAX_SPEED_RIGHT_A);
}
void TrackturnRight(void)
{
static int MAX_SPEED_LEFT_A, MAX_SPEED_RIGHT_A;
MAX_SPEED_LEFT_A = MAX_SPEED_LEFT > (255 - (int)(MAX_SPEED_LEFT * TrackFactorUp))
? 255 : MAX_SPEED_LEFT + (int)(MAX_SPEED_LEFT * TrackFactorUp);
MAX_SPEED_RIGHT_A = (MAX_SPEED_RIGHT - (int)(MAX_SPEED_RIGHT * TrackFactorDown))
< 0 ? 0 : MAX_SPEED_RIGHT - (int)(MAX_SPEED_RIGHT * TrackFactorDown);
motorSet = "RIGHT";
leftMotor1.run(FORWARD);
rightMotor1.run(BACKWARD);
leftMotor2.run(FORWARD);
rightMotor2.run(BACKWARD);
leftMotor1.setSpeed(MAX_SPEED_LEFT_A);
rightMotor1.setSpeed(MAX_SPEED_RIGHT_A);
leftMotor2.setSpeed(MAX_SPEED_LEFT_A);
rightMotor2.setSpeed(MAX_SPEED_RIGHT_A);
}
void moveStop(void)
{
leftMotor1.run(5); rightMotor1.run(5);
leftMotor2.run(5); rightMotor2.run(5);
}
void moveTrack(void)
{
int temp = 0, num1 = 0, num2 = 0, num3 = 0;
strReceived = "";
commandAvailable = false;
while (1) {
if ((Serial.available() > 0))
{
serialIn = Serial.read();
if (serialIn != '\r') {
if (serialIn != '\n') {
char a = char(serialIn);
strReceived += a;
}
}
}
if (serialIn == '\r') {
commandAvailable = true;
serialIn = 0;
processCommand(strReceived);
strReceived = "";
commandAvailable = false;
}
if (trackStopFlag) {
trackStopFlag = false;
moveStop();
strReceived = "";
commandAvailable = false;
break;
}
num1 = digitalRead(leftSensor);
num2 = digitalRead(middleSensor);
num3 = digitalRead(rightSensor);
if ((num2 == 0) && (num1 == 0) && (num3 == 0)) {
moveStop(); continue;
} else if ( (num1 == 0) && num3 == 1) { //go to right
TrackturnLeft();
while (1) {
if ((Serial.available() > 0))
{
serialIn = Serial.read();
if (serialIn != '\r') {
if (serialIn != '\n') {
char a = char(serialIn);
strReceived += a;
}
}
}
if (serialIn == '\r') {
commandAvailable = true;
serialIn = 0;
processCommand(strReceived);
strReceived = "";
commandAvailable = false;
}
if (trackStopFlag) {
//trackStopFlag = false;
moveStop();
strReceived = "";
commandAvailable = false;
break;
}
num2 = digitalRead(middleSensor);
if (num2) {
TrackturnLeft(); continue;
} else
break;
}
} else if ((num3 == 0) && (num1 == 1)) { // go to left
TrackturnRight();
while (1) {
if (trackStopFlag) {
// trackStopFlag = false;
moveStop();
strReceived = "";
commandAvailable = false;
break;
}
num2 = digitalRead(middleSensor);
if (num2) {
TrackturnRight(); continue;
} else
break;
}
} else
{
moveForward();
}
}
}
void avoidance(void)
{
strReceived = "";
commandAvailable = false;
while (1) {
if ((Serial.available() > 0))
{
serialIn = Serial.read();
if (serialIn != '\r') {
if (serialIn != '\n') {
char a = char(serialIn);
strReceived += a;
}
}
}
if (serialIn == '\r') {
commandAvailable = true;
serialIn = 0;
processCommand(strReceived);
strReceived = "";
commandAvailable = false;
}
if (avoidStopFlag) {
avoidStopFlag = false;
moveStop();
strReceived = "";
commandAvailable = false;
break;
}
int S = readPing();
if (S <= TURN_DIST && S > 0 ) {
BEEP_INT();
turn();
} else
moveForward();
}
}
void turn() {
moveBackward();
delay(turnTime);
moveStop();
int x = random(1);
if (x = 0) {
turnRight();
}
else {
turnLeft();
}
delay(turnTime);
moveStop();
}
pinMode(ECHO_PIN, INPUT);
duration = pulseIn(ECHO_PIN, HIGH);