Advertisement
Cai_B

Alternative Form of Automatic Braking -Stop Motors on EEE Bot 2

Feb 13th, 2024 (edited)
80
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 3.88 KB | None | 0 0
  1. #include <ESP32Encoder.h>
  2. #include <WiFi.h>
  3. #include <PubSubClient.h>
  4.  
  5. // WiFi and MQTT details
  6. const char* ssid = "Issacb20";
  7. const char* password = "electricalengineering";
  8. const char* mqtt_server = "B01-20.local";
  9.  
  10. WiFiClient espClient;
  11. PubSubClient client(espClient);
  12.  
  13. #define enA 33  //EnableA command line
  14. #define enB 25  //EnableA command line
  15.  
  16. #define INa 26  //Channel A Direction
  17. #define INb 27  //Channel A Direction
  18. #define INc 14  //Channel B Direction
  19. #define INd 12  //Channel B Direction
  20.  
  21. byte speedSetting = 200;   //initial speed = 0
  22. byte speedRampFlag = 1;  //define a direction controller for the loop
  23. byte changeDirection = 0;
  24.  
  25. // setting PWM properties
  26. const int freq = 2000;
  27. const int servoFrequency = 50;
  28. const int ledChannela = 0;
  29. const int ledChannelb = 1;
  30. const int servoChannel = 2;
  31. const int resolution = 8;
  32. const int servoResolution = 12;
  33.  
  34. int servoPin = 13;
  35. float steeringAngle;  // variable to store the servo position
  36.  
  37. void setup() {
  38.   // put your setup code here, to run once:
  39.  
  40.   pinMode(INa, OUTPUT);
  41.   pinMode(INb, OUTPUT);
  42.   pinMode(INc, OUTPUT);
  43.   pinMode(INd, OUTPUT);
  44.  
  45.   // configure LED PWM functionalitites
  46.   ledcSetup(ledChannela, freq, resolution);
  47.   ledcSetup(ledChannelb, freq, resolution);
  48.   ledcSetup(servoChannel, servoFrequency, servoResolution); //servo setup on PWM2, 50Hz, 12-bit (0-4096)
  49.  
  50.   //attach the channel to the GPIO to be controlled
  51.   ledcAttachPin(enA, ledChannela);
  52.   ledcAttachPin(enB, ledChannelb);
  53.   ledcAttachPin(servoPin, servoChannel);
  54.  
  55.   //initialise Serial Communication
  56.   Serial.begin(9600);
  57.  
  58.  
  59.   // Connect to WiFi
  60.   WiFi.begin(ssid, password);
  61.   while (WiFi.status() != WL_CONNECTED) {
  62.     delay(500);
  63.     Serial.println("WIFI not connected");
  64.   }
  65.   Serial.println("WIFI connected");
  66.  
  67.   // Connect to MQTT
  68.   client.setServer(mqtt_server, 1883);
  69.   client.setCallback(callback);
  70.   while (!client.connected()) {
  71.     if (client.connect("ESP32Client")) {
  72.       Serial.println("Trying to connect to MQTT Server");
  73.     } else {
  74.       delay(5000);
  75.       Serial.println("Could not connect. Connection retrying ...");
  76.     }
  77.   }
  78.   client.subscribe("esp32/stopmotors");
  79. }
  80. void reconnect() {
  81.   // loop until we're reconnected
  82.   while (!client.connected()) {
  83.     Serial.print("Attempting MQTT connection...");
  84.     // attempt to connect
  85.     if (client.connect("ESP8266Client")) {
  86.       Serial.println("connected");
  87.       // subscribe
  88.       client.subscribe("esp32/stopmotors");
  89.     } else {
  90.       Serial.print("failed, rc=");
  91.       Serial.print(client.state());
  92.       Serial.println(" try again in 5 seconds");
  93.       // wait 5 seconds before retrying
  94.       delay(5000);
  95.     }
  96.   }
  97. }
  98.  
  99. void motors(int leftSpeed, int rightSpeed) {
  100.   //set individual motor speed
  101.   //direction is set separately
  102.  
  103.   ledcWrite(ledChannela, leftSpeed);
  104.   ledcWrite(ledChannelb, rightSpeed);
  105.  
  106.   delay(25);
  107. }
  108.  
  109. void goForwards() {
  110.   //direction is set to 0
  111.   digitalWrite(INa, HIGH);
  112.   digitalWrite(INb, LOW);
  113.   digitalWrite(INc, HIGH);
  114.   digitalWrite(INd, LOW);
  115. }
  116.  
  117. void goBackwards() {
  118.   //direction is set to 1
  119.   digitalWrite(INa, LOW);
  120.   digitalWrite(INb, HIGH);
  121.   digitalWrite(INc, LOW);
  122.   digitalWrite(INd, HIGH);
  123. }
  124.  
  125. void stopMotors() {
  126.   //stop both motors
  127.   digitalWrite(INa, LOW);
  128.   digitalWrite(INb, LOW);
  129.   digitalWrite(INc, LOW);
  130.   digitalWrite(INd, LOW);
  131. }
  132. void loop() {
  133.   if (!client.connected()) {
  134.     reconnect();
  135.   }
  136.   client.loop();
  137.  
  138.       goForwards();
  139.       motors(150,150);
  140.       Serial.println("Going forwards");
  141. }
  142.  
  143. void callback(char* topic, byte* message, unsigned int length) {
  144.   String messageTemp;
  145.  
  146.   for (int i = 0; i < length; i++) {
  147.     messageTemp += (char)message[i];
  148.   }
  149.  
  150.   if (String(topic) == "esp32/stopmotors") {
  151.      if(messageTemp=="stop_car_B")
  152.         {
  153.           stopMotors();
  154.           motors(0,0);
  155.         }
  156.  
  157.     }
  158.    
  159.    
  160.   }
  161.  
  162.  
  163.  
  164.  
  165.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement