Created
February 15, 2023 16:54
-
-
Save squirelo/7ab1bc078c1e17adf5658532ab4b2a3a to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#define SPEED 85 | |
#define TURN_SPEED 90 | |
#define SHIFT_SPEED 130 | |
#define MOVE_DELAY 500 | |
#define TURN_TIME 500 | |
#define MOVE_TIME 500 | |
#define speedPinR 9 // Front Wheel PWM pin connect Model-Y M_B ENA | |
#define RightMotorDirPin1 22 //Front Right Motor direction pin 1 to Model-Y M_B IN1 (K1) | |
#define RightMotorDirPin2 24 //Front Right Motor direction pin 2 to Model-Y M_B IN2 (K1) | |
#define LeftMotorDirPin1 26 //Front Left Motor direction pin 1 to Model-Y M_B IN3 (K3) | |
#define LeftMotorDirPin2 28 //Front Left Motor direction pin 2 to Model-Y M_B IN4 (K3) | |
#define speedPinL 10 // Front Wheel PWM pin connect Model-Y M_B ENB | |
#define speedPinRB 11 // Rear Wheel PWM pin connect Left Model-Y M_A ENA | |
#define RightMotorDirPin1B 5 //Rear Right Motor direction pin 1 to Model-Y M_A IN1 ( K1) | |
#define RightMotorDirPin2B 6 //Rear Right Motor direction pin 2 to Model-Y M_A IN2 ( K1) | |
#define LeftMotorDirPin1B 7 //Rear Left Motor direction pin 1 to Model-Y M_A IN3 (K3) | |
#define LeftMotorDirPin2B 8 //Rear Left Motor direction pin 2 to Model-Y M_A IN4 (K3) | |
#define speedPinLB 12 // Rear Wheel PWM pin connect Model-Y M_A ENB | |
/*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); | |
FR_bck(speed_fr_bck); | |
RR_fwd(speed_rr_fwd); | |
; | |
} | |
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); | |
FR_fwd(speed_fr_fwd); | |
RR_bck(speed_rr_bck); | |
} | |
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 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_bck(int speed) //front-right wheel forward turn | |
{ | |
digitalWrite(RightMotorDirPin1, LOW); | |
digitalWrite(RightMotorDirPin2,HIGH); | |
analogWrite(speedPinR,speed); | |
} | |
void FR_fwd(int speed) // front-right wheel backward turn | |
{ | |
digitalWrite(RightMotorDirPin1,HIGH); | |
digitalWrite(RightMotorDirPin2,LOW); | |
analogWrite(speedPinR,speed); | |
} | |
void FL_bck(int speed) // front-left wheel forward turn | |
{ | |
digitalWrite(LeftMotorDirPin1,LOW); | |
digitalWrite(LeftMotorDirPin2,HIGH); | |
analogWrite(speedPinL,speed); | |
} | |
void FL_fwd(int speed) // front-left wheel backward turn | |
{ | |
digitalWrite(LeftMotorDirPin1,HIGH); | |
digitalWrite(LeftMotorDirPin2,LOW); | |
analogWrite(speedPinL,speed); | |
} | |
void RR_bck(int speed) //rear-right wheel forward turn | |
{ | |
digitalWrite(RightMotorDirPin1B, LOW); | |
digitalWrite(RightMotorDirPin2B,HIGH); | |
analogWrite(speedPinRB,speed); | |
} | |
void RR_fwd(int speed) //rear-right wheel backward turn | |
{ | |
digitalWrite(RightMotorDirPin1B, HIGH); | |
digitalWrite(RightMotorDirPin2B,LOW); | |
analogWrite(speedPinRB,speed); | |
} | |
void RL_bck(int speed) //rear-left wheel forward turn | |
{ | |
digitalWrite(LeftMotorDirPin1B,LOW); | |
digitalWrite(LeftMotorDirPin2B,HIGH); | |
analogWrite(speedPinLB,speed); | |
} | |
void RL_fwd(int speed) //rear-left wheel backward turn | |
{ | |
digitalWrite(LeftMotorDirPin1B,HIGH); | |
digitalWrite(LeftMotorDirPin2B,LOW); | |
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(); | |
} | |
#include "WiFiEsp.h" | |
#include "WiFiEspUDP.h" | |
char ssid[] = "blarouter"; // replace ****** with your network SSID (name) | |
char pass[] = "blablabla"; // replace ****** with your network password | |
int status = WL_IDLE_STATUS; | |
// use a ring buffer to increase speed and reduce memory allocation | |
char packetBuffer[5]; | |
WiFiEspUDP Udp; | |
unsigned int localPort = 8888; // local port to listen on | |
void setup() | |
{ | |
init_GPIO(); | |
Serial.begin(9600); // initialize serial for debugging | |
Serial1.begin(115200); | |
Serial1.write("AT+UART_DEF=9600,8,1,0,0\r\n"); | |
delay(200); | |
Serial1.write("AT+RST\r\n"); | |
delay(200); | |
Serial1.begin(9600); // initialize serial for ESP module | |
WiFi.init(&Serial1); // initialize ESP module | |
// check for the presence of the shield | |
if (WiFi.status() == WL_NO_SHIELD) { | |
Serial.println("WiFi shield not present"); | |
// don't continue | |
while (true); | |
} | |
// attempt to connect to WiFi network | |
while (status != WL_CONNECTED) { | |
Serial.print("Attempting to connect to WPA SSID: "); | |
Serial.println(ssid); | |
// Connect to WPA/WPA2 network | |
status = WiFi.begin(ssid, pass); | |
} | |
Serial.println("You're connected to the network"); | |
printWifiStatus(); | |
Udp.begin(localPort); | |
Serial.print("Listening on port "); | |
Serial.println(localPort); | |
} | |
void loop() | |
{ | |
int packetSize = Udp.parsePacket(); | |
if (packetSize) { // if you get a client, | |
Serial.print("Received packet of size "); | |
Serial.println(packetSize); | |
int len = Udp.read(packetBuffer, 255); | |
if (len > 0) { | |
packetBuffer[len] = 0; | |
} | |
char c=packetBuffer[0]; | |
switch (c) //serial control instructions | |
{ | |
case 'A':go_advance(SPEED);delay(MOVE_DELAY);stop_Stop();break; | |
case 'L':left_turn(TURN_SPEED);delay(MOVE_DELAY);stop_Stop();break; | |
case 'R':right_turn(TURN_SPEED);delay(MOVE_DELAY);stop_Stop();break; | |
case 'B':go_back(SPEED);delay(MOVE_DELAY);stop_Stop();break; | |
case 'E':stop_Stop();break; | |
case 'F':left_shift(0,150,0,150);delay(MOVE_DELAY);stop_Stop();break; //left ahead | |
case 'H':right_shift(180,0,150,0);delay(MOVE_DELAY);stop_Stop();break; //right ahead | |
case 'I':left_shift(150,0,150,0);delay(MOVE_DELAY);stop_Stop();break;//left back | |
case 'K':right_shift(0,130,0,130);delay(MOVE_DELAY);stop_Stop();break;//right back | |
case 'O':left_shift(200,150,150,200);delay(MOVE_DELAY);stop_Stop();break;//left shift | |
case 'T':right_shift(200,200,200,200);delay(MOVE_DELAY);stop_Stop();break;//left shift | |
default:break; | |
} | |
} | |
} | |
void printWifiStatus() | |
{ | |
// print the SSID of the network you're attached to | |
Serial.print("SSID: "); | |
Serial.println(WiFi.SSID()); | |
// print your WiFi shield's IP address | |
IPAddress ip = WiFi.localIP(); | |
Serial.print("IP Address: "); | |
Serial.println(ip); | |
// print where to go in the browser | |
Serial.println(); | |
Serial.print("To see this page in action, open a browser to http://"); | |
Serial.println(ip); | |
Serial.println(); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment