Professional Documents
Culture Documents
h>
#include <Servo.h>
#include "UCNEC.h"
typedef struct MeModule
{
int device;
int port;
int slot;
int pin;
int index;
float values[3];
} MeModule;
union {
byte byteVal[4];
float floatVal;
long longVal;
} val;
union {
byte byteVal[8];
double doubleVal;
} valDouble;
union {
byte byteVal[2];
short shortVal;
} valShort;
#define GET 1
#define RUN 2
#define RESET 4
#define START 5
unsigned char prevc = 0;
double lastTime = 0.0;
double currentTime = 0.0;
uint8_t keyPressed = 0;
uint8_t command_index = 0;
uint32_t irValue = 0;
#define TURN_DIST 40
#define TRIG_PIN A2
#define ECHO_PIN A3
#define SERVO_PIN 10
Servo neckControllerServoMotor;
UCNEC myIR(2);
void setup() {
pinMode(ECHO_PIN, INPUT); //Set the connection pin output mode Echo pin
pinMode(TRIG_PIN, OUTPUT);//Set the connection pin output mode trog pin
neckControllerServoMotor.attach(SERVO_PIN);
neckControllerServoMotor.write(90);
delay(2000);
neckControllerServoMotor.detach();
delay(100);
myIR.begin();
Serial.begin(115200);
Serial.print("Version: ");
Serial.println(mVersion);
}
void loop() {
currentTime = millis() / 1000.0 - lastTime;
readSerial();
if (isAvailable) {
unsigned char c = serialRead & 0xff;
if (c == 0x55 && isStart == false) {
if (prevc == 0xff) {
index = 1;
isStart = true; isSmartMode = false;
}
} else {
prevc = c;
if (isStart) {
if (index == 2) {
dataLen = c;
} else if (index > 2) {
dataLen--;
}
writeBuffer(index, c);
}
}
index++;
if (index > 51) {
index = 0;
isStart = false;
}
if (isStart && dataLen == 0 && index > 3) {
isStart = false;
parseData();
index = 0;
} else if (!isStart) {
if (serialRead >= 1 && serialRead <= 5) { //0x01->forward 0x02->backward
0x03->left 0x04-> right 0x05->stop
if(serialRead == 1) {isDetecte = true;}
else {isDetecte = false;}
leftMotor1.run(serialRead); rightMotor1.run(serialRead);
leftMotor2.run(serialRead); rightMotor2.run(serialRead);
leftMotor1.setSpeed(200); rightMotor1.setSpeed(200);
leftMotor2.setSpeed(200); rightMotor2.setSpeed(200);
neckControllerServoMotor.detach();
delay(100);
isSmartMode = false;
myIR.begin();
}
if (serialRead == 0x06) { //automatic obstacle avoidance
neckControllerServoMotor.attach(SERVO_PIN);
neckControllerServoMotor.write(90);
isSmartMode = true;
}
if (serialRead == 0x11) {
Serial.write(0xAA);
Serial.print("{\"version\":\"K0070\"}");
Serial.write(0xAB);
}
if (serialRead == 0x10) { //automatic obstacle avoidance
neckControllerServoMotor.attach(SERVO_PIN);
neckControllerServoMotor.write(90);
//Serial.println("Come in ");
delay(100);
neckControllerServoMotor.detach();
delay(100);
isSmartMode = false;
myIR.begin();
leftMotor1.run(0x05); rightMotor1.run(0x05);
leftMotor2.run(0x05); rightMotor2.run(0x05);
}
}
}
if (isSmartMode) {
S = readPing();
if (S>0 && S <= TURN_DIST ) {
isIrMode = false;
neckControllerServoMotor.attach(SERVO_PIN);
neckControllerServoMotor.write(90);
turn();
} else if (S > TURN_DIST) {
leftMotor1.run(1); rightMotor1.run(1);//1-> forward
leftMotor2.run(1); rightMotor2.run(1);
leftMotor1.setSpeed(200); rightMotor1.setSpeed(200);
leftMotor2.setSpeed(200); rightMotor2.setSpeed(200);
if (isIrMode == 0) {
isIrMode = true;
neckControllerServoMotor.detach();
delay(100);
myIR.begin();
}
}
}
}
unsigned char readBuffer(int index) {
return isBluetooth ? bufferBt[index] : buffer[index];
}
void writeBuffer(int index, unsigned char c) {
if (isBluetooth) {
bufferBt[index] = c;
} else {
buffer[index] = c;
}
}
void writeHead() {
writeSerial(0xff);
writeSerial(0x55);
}
void writeEnd() {
Serial.println();
#if defined(__AVR_ATmega32U4__)
Serial1.println();
#endif
}
void writeSerial(unsigned char c) {
Serial.write(c);
#if defined(__AVR_ATmega32U4__)
Serial1.write(c);
#endif
}
void readSerial() {
isAvailable = false;
if (Serial.available() > 0) {
isAvailable = true;
isBluetooth = false;
serialRead = Serial.read();
}
if(isDetecte){
S = readPing();
if (S> 0 && S <= TURN_DIST ) {
leftMotor1.run(5); rightMotor1.run(5);//5-> stop
leftMotor1.setSpeed(200); rightMotor1.setSpeed(200);
}
}
while (myIR.available())
{
irValue = myIR.read();
}
if (irValue == 0xFF46B9) //forward
{
void turn() {
leftMotor1.run(5); rightMotor1.run(5);//5-> stop
leftMotor2.run(5); rightMotor2.run(5);
leftMotor1.setSpeed(0); rightMotor1.setSpeed(0);
leftMotor2.setSpeed(0); rightMotor2.setSpeed(0);
neckControllerServoMotor.write(150);
delay(300);
S = readPing();
Sleft = S;
neckControllerServoMotor.write(90);
delay(300);
neckControllerServoMotor.write(30);
delay(300);
S = readPing();
Sright = S;
neckControllerServoMotor.write(90);
delay(300);
if (Sleft <= TURN_DIST && Sright <= TURN_DIST) {
leftMotor1.run(2); rightMotor1.run(2);//2-> backward
leftMotor2.run(2); rightMotor2.run(2);
leftMotor1.setSpeed(200); rightMotor1.setSpeed(200);
leftMotor2.setSpeed(200); rightMotor2.setSpeed(200);
delay(300);
int x = random(1);
if (x = 0) {
leftMotor1.run(4); rightMotor1.run(4);//4-> right
leftMotor2.run(4); rightMotor2.run(4);//4-> right
leftMotor1.setSpeed(200); rightMotor1.setSpeed(200);
leftMotor2.setSpeed(200); rightMotor2.setSpeed(200);
}
else {
leftMotor1.run(3); rightMotor1.run(3);//3-> left
leftMotor2.run(3); rightMotor2.run(3);
leftMotor1.setSpeed(200); rightMotor1.setSpeed(200);
leftMotor2.setSpeed(200); rightMotor2.setSpeed(200);
}
delay(300);
} else {
if (Sleft >= Sright) {
leftMotor1.run(3); rightMotor1.run(3);//3-> left
leftMotor2.run(3); rightMotor2.run(3);
leftMotor1.setSpeed(200); rightMotor1.setSpeed(200);
leftMotor2.setSpeed(200); rightMotor2.setSpeed(200);
} else {
leftMotor1.run(4); rightMotor1.run(4);//4-> right
leftMotor2.run(4); rightMotor2.run(4);//4-> right
leftMotor1.setSpeed(200); rightMotor1.setSpeed(200);
leftMotor2.setSpeed(200); rightMotor2.setSpeed(200);
}
delay(300);
}
}
void runModule(int device) {
//0xff 0x55 0x6 0x0 0x1 0xa 0x9 0x0 0x0 0xa
int port = readBuffer(6);
int pin = port;
switch (device) {
case SERVO: {
int angle = readBuffer(6);
if (angle >= 0 && angle <= 180)
{
neckControllerServoMotor.attach(SERVO_PIN);
neckControllerServoMotor.write(angle);
}
}
break;
case ROBOTCAR: {
int directionMode = readBuffer(6);
int motorSpeed = readBuffer(8);
leftMotor1.run(directionMode); rightMotor1.run(directionMode);
leftMotor2.run(directionMode); rightMotor2.run(directionMode);
leftMotor1.setSpeed(motorSpeed); rightMotor1.setSpeed(motorSpeed);
leftMotor2.setSpeed(motorSpeed); rightMotor2.setSpeed(motorSpeed);
}
break;
}
}
int getUltrasonicVal(void)
{
unsigned char cnt = 0;
long cm, beginTime, stopTime;
long waitCount = 0;
pinMode(TRIG_PIN, OUTPUT); pinMode(ECHO_PIN, INPUT);
digitalWrite(TRIG_PIN, LOW); delayMicroseconds(2);
digitalWrite(TRIG_PIN, HIGH); delayMicroseconds(5);
digitalWrite(TRIG_PIN, LOW); waitCount = 0;
while (!(digitalRead(ECHO_PIN) == 1)) {
if (++waitCount >= 30000)
break;
}
beginTime = micros(); waitCount = 0;
while (!(digitalRead(ECHO_PIN) == 0)) {
if (++waitCount >= 30000)
break;
}
stopTime = micros();
cm = (float)(stopTime - beginTime) / 1000000 * 34000 / 2;
return cm;
}
int readPing(void)
{
int Res[3];
int res;
int maxvalue;
int minvalue;
for(int i=0;i<3;i++)
{
Res[i] = getUltrasonicVal();
delay(10);
}
maxvalue = Res[0] >= Res[1] ? Res[0] : Res[1];
maxvalue = maxvalue >= Res[2] ? maxvalue : Res[2];
minvalue = Res[0] <= Res[1] ? Res[0] : Res[1];
minvalue = minvalue <= Res[2] ? minvalue : Res[2];
res = Res[0] + Res[1] + Res[2] - maxvalue - minvalue;
return res;
}