JugueBot Code for different parts of project
Servo Motor fo robotic Arm Test at 90 degrees
#include <Servo.h>
Servo myservo1;
Servo myservo2;
Servo myservo3;
Servo myservo4;
int pos1=90, pos2=90, pos3=90, pos4=90;
void setup() {
// put your setup code here, to run once
Serial.begin(9600);
myservo1.attach(3);
myservo2.attach(5);
myservo3.attach(6);
myservo4.attach(9);
delay (5000);
}
void loop() {
// put your main code here, to run repeatedly:
myservo1.write(pos1);
myservo2.write(pos2);
myservo3.write(pos3);
myservo4.write(pos4);
delay(2000);
}
Code for direcction of the wheels
#define SPEED 140
#define TURN_SPEED 160
#define speedPinR 9 // Front Wheel PWM pin connect Right MODEL-X ENA
#define RightMotorDirPin1 22 //Front Right Motor direction pin 1 to Right MODEL-X IN1 (K1)
#define RightMotorDirPin2 24 //Front Right Motor direction pin 2 to Right MODEL-X IN2 (K1)
#define LeftMotorDirPin1 26 //Front Left Motor direction pin 1 to Right MODEL-X IN3 (K3)
#define LeftMotorDirPin2 28 //Front Left Motor direction pin 2 to Right MODEL-X IN4 (K3)
#define speedPinL 10 // Front Wheel PWM pin connect Right MODEL-X ENB
#define speedPinRB 11 // Rear Wheel PWM pin connect Left MODEL-X ENA
#define RightMotorDirPin1B 5 //Rear Right Motor direction pin 1 to Left MODEL-X IN1 ( K1)
#define RightMotorDirPin2B 6 //Rear Right Motor direction pin 2 to Left MODEL-X IN2 ( K1)
#define LeftMotorDirPin1B 7 //Rear Left Motor direction pin 1 to Left MODEL-X IN3 (K3)
#define LeftMotorDirPin2B 8 //Rear Left Motor direction pin 2 to Left MODEL-X IN4 (K3)
#define speedPinLB 12 // Rear Wheel PWM pin connect Left MODEL-X ENB
/*motor control*/
void go_advance(int speed){
RL_fwd(speed);
RR_fwd(speed);
FR_fwd(speed);
FL_fwd(speed);
}
void go_back(int speed){
RL_bck(speed);
RR_bck(speed);
FR_bck(speed);
FL_bck(speed);
}
void right_shift(int speed_fl_fwd,int speed_rl_bck ,int speed_rr_fwd,int speed_fr_bck) {
FL_fwd(speed_fl_fwd);
RL_bck(speed_rl_bck);
RR_fwd(speed_rr_fwd);
FR_bck(speed_fr_bck);
}
void left_shift(int speed_fl_bck,int speed_rl_fwd ,int speed_rr_bck,int speed_fr_fwd){
FL_bck(speed_fl_bck);
RL_fwd(speed_rl_fwd);
RR_bck(speed_rr_bck);
FR_fwd(speed_fr_fwd);
}
void left_turn(int speed){
RL_bck(0);
RR_fwd(speed);
FR_fwd(speed);
FL_bck(0);
}
void right_turn(int speed){
RL_fwd(speed);
RR_bck(0);
FR_bck(0);
FL_fwd(speed);
}
void left_back(int speed){
RL_fwd(0);
RR_bck(speed);
FR_bck(speed);
FL_fwd(0);
}
void right_back(int speed){
RL_bck(speed);
RR_fwd(0);
FR_fwd(0);
FL_bck(speed);
}
void clockwise(int speed){
RL_fwd(speed);
RR_bck(speed);
FR_bck(speed);
FL_fwd(speed);
}
void countclockwise(int speed){
RL_bck(speed);
RR_fwd(speed);
FR_fwd(speed);
FL_bck(speed);
}
void FR_fwd(int speed) //front-right wheel forward turn
{
digitalWrite(RightMotorDirPin1,HIGH);
digitalWrite(RightMotorDirPin2,LOW);
analogWrite(speedPinR,speed);
}
void FR_bck(int speed) // front-right wheel backward turn
{
digitalWrite(RightMotorDirPin1,LOW);
digitalWrite(RightMotorDirPin2,HIGH);
analogWrite(speedPinR,speed);
}
void FL_fwd(int speed) // front-left wheel forward turn
{
digitalWrite(LeftMotorDirPin1,HIGH);
digitalWrite(LeftMotorDirPin2,LOW);
analogWrite(speedPinL,speed);
}
void FL_bck(int speed) // front-left wheel backward turn
{
digitalWrite(LeftMotorDirPin1,LOW);
digitalWrite(LeftMotorDirPin2,HIGH);
analogWrite(speedPinL,speed);
}
void RR_fwd(int speed) //rear-right wheel forward turn
{
digitalWrite(RightMotorDirPin1B, HIGH);
digitalWrite(RightMotorDirPin2B,LOW);
analogWrite(speedPinRB,speed);
}
void RR_bck(int speed) //rear-right wheel backward turn
{
digitalWrite(RightMotorDirPin1B, LOW);
digitalWrite(RightMotorDirPin2B,HIGH);
analogWrite(speedPinRB,speed);
}
void RL_fwd(int speed) //rear-left wheel forward turn
{
digitalWrite(LeftMotorDirPin1B,HIGH);
digitalWrite(LeftMotorDirPin2B,LOW);
analogWrite(speedPinLB,speed);
}
void RL_bck(int speed) //rear-left wheel backward turn
{
digitalWrite(LeftMotorDirPin1B,LOW);
digitalWrite(LeftMotorDirPin2B,HIGH);
analogWrite(speedPinLB,speed);
}
void stop_Stop() //Stop
{
analogWrite(speedPinLB,0);
analogWrite(speedPinRB,0);
analogWrite(speedPinL,0);
analogWrite(speedPinR,0);
}
//Pins initialize
void init_GPIO()
{
pinMode(RightMotorDirPin1, OUTPUT);
pinMode(RightMotorDirPin2, OUTPUT);
pinMode(speedPinL, OUTPUT);
pinMode(LeftMotorDirPin1, OUTPUT);
pinMode(LeftMotorDirPin2, OUTPUT);
pinMode(speedPinR, OUTPUT);
pinMode(RightMotorDirPin1B, OUTPUT);
pinMode(RightMotorDirPin2B, OUTPUT);
pinMode(speedPinLB, OUTPUT);
pinMode(LeftMotorDirPin1B, OUTPUT);
pinMode(LeftMotorDirPin2B, OUTPUT);
pinMode(speedPinRB, OUTPUT);
stop_Stop();
}
void setup()
{
init_GPIO();
go_advance(SPEED);
delay(1000);
stop_Stop();
delay(1000);
go_back(SPEED);
delay(1000);
stop_Stop();
delay(1000);
left_turn(TURN_SPEED);
delay(1000);
stop_Stop();
delay(1000);
right_turn(TURN_SPEED);
delay(1000);
stop_Stop();
delay(1000);
right_shift(200,200,200,200); //right shift
delay(1000);
stop_Stop();
delay(1000);
left_shift(200,200,200,200); //left shift
delay(1000);
stop_Stop();
delay(1000);
left_shift(200,0,200,0); //left diagonal back
delay(1000);
stop_Stop();
delay(1000);
right_shift(200,0,200,0); //right diagonal ahead
delay(1000);
stop_Stop();
delay(1000);
left_shift(0,200,0,200); //left diagonal ahead
delay(1000);
stop_Stop();
delay(1000);
right_shift(0,200,0,200); //right diagonal back
delay(1000);
stop_Stop();
delay(1000);
}
void loop(){
}
Wheels Encoders
#define outputA 6
#define outputB 7
int counter = 0;
int aState;
int aLastState;
void setup() {
pinMode (outputA,INPUT);
pinMode (outputB,INPUT);
Serial.begin (9600);
// Reads the initial state of the outputA
aLastState = digitalRead(outputA);
}
void loop() {
aState = digitalRead(outputA); // Reads the "current" state of the outputA
// If the previous and the current state of the outputA are different, that means a Pulse has occured
if (aState != aLastState){
// If the outputB state is different to the outputA state, that means the encoder is rotating clockwise
if (digitalRead(outputB) != aState) {
counter ++;
} else {
counter --;
}
Serial.print("Position: ");
Serial.println(counter);
}
aLastState = aState; // Updates the previous state of the outputA with the current state
}
Obstacle avoidance code
#include <Servo.h>
#define speedPinR 9 // Front Wheel PWM pin connect Right MODEL-X ENA
#define RightMotorDirPin1 22 //Front Right Motor direction pin 1 to Right MODEL-X IN1 (K1)
#define RightMotorDirPin2 24 //Front Right Motor direction pin 2 to Right MODEL-X IN2 (K1)
#define LeftMotorDirPin1 26 //Front Left Motor direction pin 1 to Right MODEL-X IN3 (K3)
#define LeftMotorDirPin2 28 //Front Left Motor direction pin 2 to Right MODEL-X IN4 (K3)
#define speedPinL 10 // Front Wheel PWM pin connect Right MODEL-X ENB
#define speedPinRB 11 // Rear Wheel PWM pin connect Left MODEL-X ENA
#define RightMotorDirPin1B 5 //Rear Right Motor direction pin 1 to Left MODEL-X IN1 ( K1)
#define RightMotorDirPin2B 6 //Rear Right Motor direction pin 2 to Left MODEL-X IN2 ( K1)
#define LeftMotorDirPin1B 7 //Rear Left Motor direction pin 1 to Left MODEL-X IN3 (K3)
#define LeftMotorDirPin2B 8 //Rear Left Motor direction pin 2 to Left MODEL-X IN4 (K3)
#define speedPinLB 12 // Rear Wheel PWM pin connect Left MODEL-X ENB
#define LPT 2 // scan loop coumter
#define SERVO_PIN 13 //servo connect to D5
#define Echo_PIN 31 // Ultrasonic Echo pin connect to A5
#define Trig_PIN 30 // Ultrasonic Trig pin connect to A4
#define FAST_SPEED 160 //both sides of the motor speed
#define SPEED 120 //both sides of the motor speed
#define TURN_SPEED 120 //both sides of the motor speed
#define BACK_SPEED1 160 //back speed
#define BACK_SPEED2 90 //back speed
int leftscanval, centerscanval, rightscanval, ldiagonalscanval, rdiagonalscanval;
const int distancelimit = 30; //distance limit for obstacles in front
const int sidedistancelimit = 30; //minimum distance in cm to obstacles at both sides (the car will allow a shorter distance sideways)
int distance;
int numcycles = 0;
const int turntime = 250; //Time the robot spends turning (miliseconds)
const int backtime = 300; //Time the robot spends turning (miliseconds)
int thereis;
Servo head;
/*motor control*/
void go_Advance() //Forward
{
FR_fwd();
FL_fwd();
RR_fwd();
RL_fwd();
}
void go_Left() //Turn left
{
FR_fwd();
FL_bck();
RR_fwd();
RL_bck();
}
void go_Right() //Turn right
{
FR_bck();
FL_fwd();
RR_bck();
RL_fwd();
}
void go_Back() //Reverse
{
FR_bck();
FL_bck();
RR_bck();
RL_bck();
}
void stop_Stop() //Stop
{
digitalWrite(RightMotorDirPin1, LOW);
digitalWrite(RightMotorDirPin2,LOW);
digitalWrite(LeftMotorDirPin1,LOW);
digitalWrite(LeftMotorDirPin2,LOW);
digitalWrite(RightMotorDirPin1B, LOW);
digitalWrite(RightMotorDirPin2B,LOW);
digitalWrite(LeftMotorDirPin1B,LOW);
digitalWrite(LeftMotorDirPin2B,LOW);
set_Motorspeed(0,0,0,0);
}
/*set motor speed */
void set_Motorspeed(int leftFront,int rightFront,int leftBack,int rightBack)
{
analogWrite(speedPinL,leftFront);
analogWrite(speedPinR,rightFront);
analogWrite(speedPinLB,leftBack);
analogWrite(speedPinRB,rightBack);
}
void FR_fwd() //front-right wheel forward turn
{
digitalWrite(RightMotorDirPin1,HIGH);
digitalWrite(RightMotorDirPin2,LOW);
}
void FR_bck() // front-right wheel backward turn
{
digitalWrite(RightMotorDirPin1,LOW);
digitalWrite(RightMotorDirPin2,HIGH);
}
void FL_fwd() // front-left wheel forward turn
{
digitalWrite(LeftMotorDirPin1,HIGH);
digitalWrite(LeftMotorDirPin2,LOW);
}
void FL_bck() // front-left wheel backward turn
{
digitalWrite(LeftMotorDirPin1,LOW);
digitalWrite(LeftMotorDirPin2,HIGH);
}
void RR_fwd() //rear-right wheel forward turn
{
digitalWrite(RightMotorDirPin1B,HIGH);
digitalWrite(RightMotorDirPin2B,LOW);
}
void RR_bck() //rear-right wheel backward turn
{
digitalWrite(RightMotorDirPin1B,LOW);
digitalWrite(RightMotorDirPin2B,HIGH);
}
void RL_fwd() //rear-left wheel forward turn
{
digitalWrite(LeftMotorDirPin1B,HIGH);
digitalWrite(LeftMotorDirPin2B,LOW);
}
void RL_bck() //rear-left wheel backward turn
{
digitalWrite(LeftMotorDirPin1B,LOW);
digitalWrite(LeftMotorDirPin2B,HIGH);
}
/*detection of ultrasonic distance*/
int watch(){
long echo_distance;
digitalWrite(Trig_PIN,LOW);
delayMicroseconds(5);
digitalWrite(Trig_PIN,HIGH);
delayMicroseconds(15);
digitalWrite(Trig_PIN,LOW);
echo_distance=pulseIn(Echo_PIN,HIGH);
echo_distance=echo_distance*0.01657; //how far away is the object in cm
//Serial.println((int)echo_distance);
return round(echo_distance);
}
//Meassures distances to the right, left, front, left diagonal, right diagonal and asign them in cm to the variables rightscanval,
//leftscanval, centerscanval, ldiagonalscanval and rdiagonalscanval (there are 5 points for distance testing)
String watchsurrounding(){
/* obstacle_status is a binary integer, its last 5 digits stands for if there is any obstacles in 5 directions,
* for example B101000 last 5 digits is 01000, which stands for Left front has obstacle, B100111 means front, right front and right ha
*/
int obstacle_status =B100000;
centerscanval = watch();
if(centerscanval<distancelimit){
stop_Stop();
obstacle_status =obstacle_status | B100;
}
head.write(120);
delay(100);
ldiagonalscanval = watch();
if(ldiagonalscanval<distancelimit){
stop_Stop();
obstacle_status =obstacle_status | B1000;
}
head.write(170); //Didn't use 180 degrees because my servo is not able to take this angle
delay(300);
leftscanval = watch();
if(leftscanval<sidedistancelimit){
stop_Stop();
obstacle_status =obstacle_status | B10000;
}
head.write(90); //use 90 degrees if you are moving your servo through the whole 180 degrees
delay(100);
centerscanval = watch();
if(centerscanval<distancelimit){
stop_Stop();
obstacle_status =obstacle_status | B100;
}
head.write(40);
delay(100);
rdiagonalscanval = watch();
if(rdiagonalscanval<distancelimit){
stop_Stop();
obstacle_status =obstacle_status | B10;
}
head.write(0);
delay(100);
rightscanval = watch();
if(rightscanval<sidedistancelimit){
stop_Stop();
obstacle_status =obstacle_status | 1;
}
head.write(90); //Finish looking around (look forward again)
delay(300);
String obstacle_str= String(obstacle_status,BIN);
obstacle_str= obstacle_str.substring(1,6);
return obstacle_str; //return 5-character string standing for 5 direction obstacle status
}
void auto_avoidance(){
++numcycles;
if(numcycles>=LPT){ //Watch if something is around every LPT loops while moving forward
stop_Stop();
String obstacle_sign=watchsurrounding(); // 5 digits of obstacle_sign binary value means the 5 direction obstacle status
Serial.print("begin str=");
Serial.println(obstacle_sign);
if( obstacle_sign=="10000"){
Serial.println("SLIT right");
set_Motorspeed(FAST_SPEED,SPEED,FAST_SPEED,SPEED);
go_Advance();
delay(turntime);
stop_Stop();
}
else if( obstacle_sign=="00001" ){
Serial.println("SLIT LEFT");
set_Motorspeed(SPEED,FAST_SPEED,SPEED,FAST_SPEED);
go_Advance();
delay(turntime);
stop_Stop();
}
else if( obstacle_sign=="11100" || obstacle_sign=="01000" || obstacle_sign=="11000" || obstacle_sign=="10100" || obstacle_sign=="01100" ||obstacle_sign=="00100" ||obstacle_sign=="01000" ){
Serial.println("hand right");
go_Right();
set_Motorspeed(TURN_SPEED,TURN_SPEED,TURN_SPEED,TURN_SPEED);
delay(turntime);
stop_Stop();
}
else if( obstacle_sign=="00010" || obstacle_sign=="00111" || obstacle_sign=="00011" || obstacle_sign=="00101" || obstacle_sign=="00110" || obstacle_sign=="01010" ){
Serial.println("hand left");
go_Left();//Turn left
set_Motorspeed(TURN_SPEED,TURN_SPEED,TURN_SPEED,TURN_SPEED);
delay(turntime);
stop_Stop();
}
else if( obstacle_sign=="01111" || obstacle_sign=="10111" || obstacle_sign=="11111" ){
Serial.println("hand back left");
go_Back();
set_Motorspeed( BACK_SPEED1,BACK_SPEED2,BACK_SPEED1,BACK_SPEED2);
delay(backtime);
stop_Stop();
}
else if( obstacle_sign=="11011" || obstacle_sign=="11101" || obstacle_sign=="11110" || obstacle_sign=="01110" ){
Serial.println("hand back right");
go_Back();
set_Motorspeed(BACK_SPEED2,BACK_SPEED1,BACK_SPEED2,BACK_SPEED1);
delay(backtime);
stop_Stop();
}
else Serial.println("no handle");
numcycles=0; //Restart count of cycles
} else {
set_Motorspeed(SPEED,SPEED,SPEED,SPEED);
go_Advance(); // if nothing is wrong go forward using go() function above.
delay(backtime);
stop_Stop();
}
//else Serial.println(numcycles);
distance = watch(); // use the watch() function to see if anything is ahead (when the robot is just moving forward and not looking around it will test the distance in front)
if (distance<distancelimit){ // The robot will just stop if it is completely sure there's an obstacle ahead (must test 25 times) (needed to ignore ultrasonic sensor's false signals)
Serial.println("final go back");
go_Back();
set_Motorspeed(BACK_SPEED1,BACK_SPEED2,BACK_SPEED1,BACK_SPEED2);
delay(backtime);
++thereis;}
if (distance>distancelimit){
thereis=0;} //Count is restarted
if (thereis > 25){
Serial.println("final stop");
stop_Stop(); // Since something is ahead, stop moving.
thereis=0;
}
}
void setup() {
/*setup L298N pin mode*/
pinMode(RightMotorDirPin1, OUTPUT);
pinMode(RightMotorDirPin2, OUTPUT);
pinMode(speedPinL, OUTPUT);
pinMode(LeftMotorDirPin1, OUTPUT);
pinMode(LeftMotorDirPin2, OUTPUT);
pinMode(speedPinR, OUTPUT);
pinMode(RightMotorDirPin1B, OUTPUT);
pinMode(RightMotorDirPin2B, OUTPUT);
pinMode(speedPinLB, OUTPUT);
pinMode(LeftMotorDirPin1B, OUTPUT);
pinMode(LeftMotorDirPin2B, OUTPUT);
pinMode(speedPinRB, OUTPUT);
stop_Stop();//stop move
/*init HC-SR04*/
pinMode(Trig_PIN, OUTPUT);
pinMode(Echo_PIN,INPUT);
/*init buzzer*/
digitalWrite(Trig_PIN,LOW);
/*init servo*/
head.attach(SERVO_PIN);
head.write(0);
delay(1000);
head.write(170);
delay(1000);
head.write(90);
delay(2000);
Serial.begin(9600);
stop_Stop();//Stop
}
void loop() {
auto_avoidance();
// Serial.println( watchsurrounding());
}
Code for Bluethood device if wifi does not work
#define MAX_PACKETSIZE 32 //Serial receive buffer
char buffUART[MAX_PACKETSIZE];
unsigned int buffUARTIndex = 0;
unsigned long preUARTTick = 0;
struct car_status{
int speed;
int angle;
int direct;
};
int move_speed=100 ;
#define MAX_SPEED 250
#define MIN_SPEED 80
#define TURN_SPEED 80
#define SLOW_TURN_SPEED 50
#define BACK_SPEED 80
int buttonState;
char old_status='0';
#define speedPinR 9 // RIGHT WHEEL PWM pin D45 connect front MODEL-X ENA
#define RightMotorDirPin1 22 //Front Right Motor direction pin 1 to Front MODEL-X IN1 (K1)
#define RightMotorDirPin2 24 //Front Right Motor direction pin 2 to Front MODEL-X IN2 (K1)
#define LeftMotorDirPin1 26 //Left front Motor direction pin 1 to Front MODEL-X IN3 ( K3)
#define LeftMotorDirPin2 28 //Left front Motor direction pin 2 to Front MODEL-X IN4 ( K3)
#define speedPinL 10 // Left WHEEL PWM pin D7 connect front MODEL-X ENB
#define speedPinRB 11 // RIGHT WHEEL PWM pin connect Back MODEL-X ENA
#define RightMotorDirPin1B 5 //Rear Right Motor direction pin 1 to Back MODEL-X IN1 ( K1)
#define RightMotorDirPin2B 6 //Rear Right Motor direction pin 2 to Back MODEL-X IN2 ( K1)
#define LeftMotorDirPin1B 7 //Rear left Motor direction pin 1 to Back MODEL-X IN3 K3
#define LeftMotorDirPin2B 8 //Rear left Motor direction pin 2 to Back MODEL-X IN4 k3
#define speedPinLB 12 // LEFT WHEEL PWM pin D8 connect Rear MODEL-X ENB
int car_direction = 1; // 1 means forward, 0 gear backward
/*motor control*/
void right_shift(int speed_fl_fwd,int speed_rl_bck ,int speed_rr_fwd,int speed_fr_bck) {
FL_fwd(speed_fl_fwd);
RL_bck(speed_rl_bck);
RR_fwd(speed_rr_fwd);
FR_bck(speed_fr_bck);
}
void left_shift(int speed_fl_bck,int speed_rl_fwd ,int speed_rr_bck,int speed_fr_fwd){
FL_bck(speed_fl_bck);
RL_fwd(speed_rl_fwd);
RR_bck(speed_rr_bck);
FR_fwd(speed_fr_fwd);
}
void go_advance(int left_speed,int right_speed){
RL_fwd(left_speed);
RR_fwd(right_speed);
FR_fwd(right_speed);
FL_fwd(left_speed);
}
void go_back(int left_speed,int right_speed){
RL_bck(left_speed);
RR_bck(right_speed);
FR_bck(right_speed);
FL_bck(left_speed);
}
void left_turn(int speed){
RL_bck(speed);
RR_fwd(speed);
FR_fwd(speed);
FL_bck(speed);
}
void right_turn(int speed){
RL_fwd(speed);
RR_bck(speed);
FR_bck(speed);
FL_fwd(speed);
}
void left_back(int speed){
RL_fwd(0);
RR_bck(speed);
FR_bck(speed);
FL_fwd(0);
}
void right_back(int speed){
RL_bck(speed);
RR_fwd(0);
FR_fwd(0);
FL_bck(speed);
}
void clockwise(int speed){
RL_fwd(speed);
RR_bck(speed);
FR_bck(speed);
FL_fwd(speed);
}
void countclockwise(int speed){
RL_bck(speed);
RR_fwd(speed);
FR_fwd(speed);
FL_bck(speed);
}
void FR_fwd(int speed) //front-right wheel forward turn
{
digitalWrite(RightMotorDirPin1,HIGH);
digitalWrite(RightMotorDirPin2,LOW);
analogWrite(speedPinR,speed);
}
void FR_bck(int speed) // front-right wheel backward turn
{
digitalWrite(RightMotorDirPin1,LOW);
digitalWrite(RightMotorDirPin2,HIGH);
analogWrite(speedPinR,speed);
}
void FL_fwd(int speed) // front-left wheel forward turn
{
digitalWrite(LeftMotorDirPin1,HIGH);
digitalWrite(LeftMotorDirPin2,LOW);
analogWrite(speedPinL,speed);
}
void FL_bck(int speed) // front-left wheel backward turn
{
digitalWrite(LeftMotorDirPin1,LOW);
digitalWrite(LeftMotorDirPin2,HIGH);
analogWrite(speedPinL,speed);
}
void RR_fwd(int speed) //rear-right wheel forward turn
{
digitalWrite(RightMotorDirPin1B, HIGH);
digitalWrite(RightMotorDirPin2B,LOW);
analogWrite(speedPinRB,speed);
}
void RR_bck(int speed) //rear-right wheel backward turn
{
digitalWrite(RightMotorDirPin1B, LOW);
digitalWrite(RightMotorDirPin2B,HIGH);
analogWrite(speedPinRB,speed);
}
void RL_fwd(int speed) //rear-left wheel forward turn
{
digitalWrite(LeftMotorDirPin1B,HIGH);
digitalWrite(LeftMotorDirPin2B,LOW);
analogWrite(speedPinLB,speed);
}
void RL_bck(int speed) //rear-left wheel backward turn
{
digitalWrite(LeftMotorDirPin1B,LOW);
digitalWrite(LeftMotorDirPin2B,HIGH);
analogWrite(speedPinLB,speed);
}
void stop_stop() //Stop
{
analogWrite(speedPinLB,0);
analogWrite(speedPinRB,0);
analogWrite(speedPinL,0);
analogWrite(speedPinR,0);
}
//Pins initialize
void init_GPIO()
{
pinMode(RightMotorDirPin1, OUTPUT);
pinMode(RightMotorDirPin2, OUTPUT);
pinMode(speedPinL, OUTPUT);
pinMode(LeftMotorDirPin1, OUTPUT);
pinMode(LeftMotorDirPin2, OUTPUT);
pinMode(speedPinR, OUTPUT);
pinMode(RightMotorDirPin1B, OUTPUT);
pinMode(RightMotorDirPin2B, OUTPUT);
pinMode(speedPinLB, OUTPUT);
pinMode(LeftMotorDirPin1B, OUTPUT);
pinMode(LeftMotorDirPin2B, OUTPUT);
pinMode(speedPinRB, OUTPUT);
}
void setup()
{
init_GPIO();
Serial.begin(9600);//In order to fit the Bluetooth module's default baud rate, only 9600
Serial1.begin(9600);
stop_stop();
}
void loop(){
do_Uart_Tick();
}
void do_Uart_Tick()
{
char Uart_Date=0;
if(Serial1.available())
{
size_t len = Serial1.available();
uint8_t sbuf[len + 1];
sbuf[len] = 0x00;
Serial1.readBytes(sbuf, len);
//parseUartPackage((char*)sbuf);
memcpy(buffUART + buffUARTIndex, sbuf, len);//ensure that the serial port can read the entire frame of data
buffUARTIndex += len;
preUARTTick = millis();
if(buffUARTIndex >= MAX_PACKETSIZE - 1)
{
buffUARTIndex = MAX_PACKETSIZE - 2;
preUARTTick = preUARTTick - 200;
}
}
car_status cs;
if(buffUARTIndex > 0 && (millis() - preUARTTick >= 100))//APP send flag to modify the obstacle avoidance parameters
{ //data ready
buffUART[buffUARTIndex] = 0x00;
Uart_Date=buffUART[0];
cs=get_status(buffUART);
car_direction=cs.angle;
buffUARTIndex = 0;
}
move_speed=cs.speed;
if (cs.speed>MAX_SPEED) move_speed= MAX_SPEED;
if (cs.speed<MIN_SPEED) move_speed= MIN_SPEED;
switch (Uart_Date) //serial control instructions
{
case 'M':
if(old_status=='0')
go_advance(110,110);
old_status='M';
delay(55);
go_advance(move_speed,move_speed);
break;
case 'L':
car_direction=1;
if (cs.angle<2)
go_advance(SLOW_TURN_SPEED,TURN_SPEED);
else left_turn(SLOW_TURN_SPEED);
old_status='0';
break;
case 'R':
car_direction=1;
if (cs.angle>-2)
go_advance(TURN_SPEED,SLOW_TURN_SPEED);
else right_turn(SLOW_TURN_SPEED);
old_status='0';
break;
case 'B':
car_direction=0;
go_back(BACK_SPEED,BACK_SPEED);
old_status='0';
break;
case 'X':
car_direction=0;
if (cs.angle>-2)
go_back(SLOW_TURN_SPEED,TURN_SPEED);
else
left_back(TURN_SPEED);
old_status='0';
break;
case 'Y':
car_direction=0;
if (cs.angle<2)
go_back(TURN_SPEED,SLOW_TURN_SPEED);
else
right_back(TURN_SPEED);
old_status='0';
break;
case 'F':
// left_shift(200,150,150,200); //left shift
left_shift(180,170,190,190) ;//left shift
delay(800);
old_status='0';
break;
case 'J':
// right_shift(200,150,150,200); //right shift
right_shift(200,170,170,200); //right shift
delay(800);
old_status='0';
break;
case 'G':
if (car_direction==1) left_shift(0,150,0,150); //left ahead
else left_shift(150,0,150,0); //left back
delay(800);
old_status='0';
break;
case 'I':
if (car_direction==1) right_shift(180,0,150,0); //right ahead
else right_shift(0,130,0,130); //right back
delay(100);
old_status='0';
break;
case 'E': stop_stop();
old_status='0';
break;
default:break;
}
}
car_status get_status( char buffUART[])
{
car_status cstatus;
int index=2;
if (buffUART[index]=='-'){
cstatus.angle=-buffUART[index+1]+'0';
index=index+3;
} else {
cstatus.angle=buffUART[index]-'0';
index=index+2;
}
int currentvalue;
int spd=0;
while (buffUART[index]!=',')
{
currentvalue=buffUART[index]-'0';
spd=spd*10+currentvalue;
index++;
}
cstatus.speed=spd;
index++;
cstatus.direct=buffUART[index]-'0';
return cstatus;
}
Comments
Post a Comment