RBE1001Lib
RCCTL.ino
Go to the documentation of this file.
1 #include <Arduino.h>
2 #include <RBE1001Lib.h>
3 #include "Motor.h"
4 #include "Rangefinder.h"
5 #include <ESP32Servo.h>
6 #include <ESP32AnalogRead.h>
7 #include <Esp32WifiManager.h>
8 #include "wifi/WifiManager.h"
9 #include "WebPage.h"
10 #include <Timer.h>
11 
12 // https://wpiroboticsengineering.github.io/RBE1001Lib/classMotor.html
15 // https://wpiroboticsengineering.github.io/RBE1001Lib/classRangefinder.html
17 // https://wpiroboticsengineering.github.io/RBE1001Lib/classServo.html
19 // https://wpiroboticsengineering.github.io/RBE1001Lib/classESP32AnalogRead.html
23 
25 
27 
28 Timer dashboardUpdateTimer; // times when the dashboard should update
29 /*
30  * This is the standard setup function that is called when the ESP32 is rebooted
31  * It is used to initialize anything that needs to be done once.
32  * In this example, it sets the Serial Console speed, initializes the web server,
33  * sets up some web page buttons, resets some timers, and sets the initial state
34  * the robot should start in
35  *
36  * HOWTO enter passwords:
37  *
38  * The Detailed documentation is here: https://github.com/madhephaestus/Esp32WifiManager
39  *
40  * You will use the Serial Monitor to enter the name of the network and the password to use.
41  * The ESP32 will store them for later.
42  * First type the SSID you want and hit send
43  * It will prompt you for a password, type that and hit send
44  *
45  * IN LAB use:
46  * In the lab, you will want to use AP mode. To set the AP, type AP:myNetName
47  * to set myNetName as the AP mode and hit send
48  * the ESP will prompt you for a password to use, enter it and hit send
49  *
50  * The ESP will default to trying to connect to a network, then fail over to AP mode
51  *
52  * To make the ESP use AP mode by default on boot, change the line below manager.setup();
53  * to manager.setupAP();
54  *
55  * To access the RC control interface for Station mode you will watch the serial monitor as the
56  * ESP boots, it will print out the IP address. Enter that address in your computer or phones web browser.
57  * Make sure your ESP and computer or phone are on the same network.
58  *
59  * To access teh RC Control interface in AP mode, connect to the ESP with either you phone or laptop
60  * then open in a browser on that device and go to:
61  *
62  * http://192.168.4.1
63  *
64  * to access the control website.
65  *
66  * NOTE you can use this class in your final project code to visualize the state of your system while running wirelessly.
67  */
68 int inc = 0;
69 void setup()
70 {
71  manager.setup(); // Connect to an infrastructure network first, then fail over to AP mode
72  //manager.setupAP();// Launch AP mode first, then fail over to connecting to a station
73  while (manager.getState() != Connected)
74  {
75  manager.loop();
76  delay(1);
77  }
78  ESP32PWM::allocateTimer(1); // Used by servos
79 
80  // pin definitions https://wpiroboticsengineering.github.io/RBE1001Lib/RBE1001Lib_8h.html#define-members
82  lifter.attach(SERVO_PIN);
83  leftLineSensor.attach(LEFT_LINE_SENSE);
84  rightLineSensor.attach(RIGHT_LINE_SENSE);
85  servoPositionFeedback.attach(SERVO_FEEDBACK_SENSOR);
86  lifter.write(0);
87  control_page.initalize(); // Init UI after everything else.
88  dashboardUpdateTimer.reset(); // reset the dashbaord refresh timer
89 }
90 
91 /*
92  * this is the state machine.
93  * You can add additional states as desired. The switch statement will execute
94  * the correct code depending on the state the robot is in currently.
95  * For states that require timing, like turning and straight, they use a timer
96  * that is zeroed when the state begins. It is compared with the number of
97  * milliseconds the robot should reamain in that state.
98  */
100 {
101 
102  float left = (control_page.getJoystickX() - control_page.getJoystickY()) * 180;
103  float right = (control_page.getJoystickX() + control_page.getJoystickY()) * 180;
104 
105  left_motor.setSpeed(left);
106  right_motor.setSpeed(right);
107  lifter.write(control_page.getSliderValue(0) * 180);
108 }
109 
110 /*
111  * This function updates all the dashboard status that you would like to see
112  * displayed periodically on the web page. You are free to add more values
113  * to be displayed to help debug your robot program by calling the
114  * "setValue" function with a name and a value.
115  */
116 
117 uint32_t packet_old = 0;
119 {
120  // This writes values to the dashboard area at the bottom of the web page
121  if (dashboardUpdateTimer.getMS() > 100)
122  {
123 
124  control_page.setValue("Left linetracker", leftLineSensor.readMiliVolts());
125  control_page.setValue("Right linetracker",
126  rightLineSensor.readMiliVolts());
127  control_page.setValue("Ultrasonic",
128  rangefinder1.getDistanceCM());
129 
130  control_page.setValue("Simple Counter",
131  inc++);
132  //if(control_page.getJoystickMagnitude()>0.1)
133  //Serial.println("Joystick angle="+String(control_page.getJoystickAngle())+
134  // " magnitude="+String(control_page.getJoystickMagnitude())+
135  // " x="+String(control_page.getJoystickX())+
136  // " y="+String(control_page.getJoystickY()) +
137  // " slider="+String(control_page.getSliderValue(0)));
138 
139  control_page.setValue("packets from Web to ESP",
140  control_page.rxPacketCount);
141 
142  control_page.setValue("slider",
143  control_page.getSliderValue(0) * 100);
144 
145  control_page.setValue("Left Encoder Degrees", left_motor.getCurrentDegrees());
146  control_page.setValue("Left Effort", (int)left_motor.getEffortPercent());
147  control_page.setValue("Left Encoder Degrees/sec", left_motor.getDegreesPerSecond());
148 
149  control_page.setValue("Right Encoder Degrees", right_motor.getCurrentDegrees());
150  control_page.setValue("Right Effort", (int)right_motor.getEffortPercent());
151  control_page.setValue("Free Ram", esp_get_free_heap_size());
152  control_page.setValue("Right Encoder Degrees/sec", right_motor.getDegreesPerSecond());
153 
154  control_page.setValue("Simple Counter",
155  inc++);
156  control_page.setValue("packets from Web to ESP",
157  control_page.rxPacketCount);
158  control_page.setValue("packets to Web from ESP",
159  control_page.txPacketCount);
160 
161  dashboardUpdateTimer.reset();
162  }
163 }
164 
165 /*
166  * The main loop for the program. The loop function is repeatedly called
167  * once the ESP32 is started. In here we run the state machine, update the
168  * dashboard data, and handle any web server requests.
169  */
170 
171 void loop()
172 {
173 
174  manager.loop();
175  runStateMachine(); // do a pass through the state machine
176  if (manager.getState() == Connected) // only update if WiFi is up
177  updateDashboard(); // update the dashboard values
178 }
#define SIDE_ULTRASONIC_ECHO
Definition: RBE1001Lib.h:25
float getDistanceCM()
get the distance of an object from the sensor in centimeters
static void allocateTimer(int timerNumber)
Definition: ESP32PWM.cpp:25
void write(int value)
Definition: ESP32Servo.cpp:131
void updateDashboard()
Definition: RCCTL.ino:118
void setup()
Definition: WifiManager.cpp:68
RightMotor right_motor
Definition: RCCTL.ino:14
void reset()
Reset the timer to the current time + interval.
Definition: Timer.cpp:16
#define RIGHT_LINE_SENSE
Definition: RBE1001Lib.h:30
LeftMotor left_motor
Definition: RCCTL.ino:13
#define LEFT_LINE_SENSE
Definition: RBE1001Lib.h:28
uint32_t rxPacketCount
Definition: WebPage.h:58
#define SERVO_FEEDBACK_SENSOR
Definition: RBE1001Lib.h:32
float getSliderValue(uint32_t number)
Definition: WebPage.cpp:303
WebPage control_page
Definition: RCCTL.ino:24
void setValue(String name, float data)
Definition: WebPage.cpp:343
void setSpeed(float newDegreesPerSecond)
Definition: Motor.cpp:295
float getJoystickX()
Definition: WebPage.cpp:314
Rangefinder objects wrap a FreeRTOS thread with pin change interrupts to read trigger/echo style ultr...
Definition: Rangefinder.h:21
WifiManager manager
Definition: RCCTL.ino:26
void initalize()
Definition: WebPage.cpp:198
void loop()
Definition: RCCTL.ino:171
float getCurrentDegrees()
Definition: Motor.cpp:568
float getJoystickY()
Definition: WebPage.cpp:317
float getEffortPercent()
Definition: Motor.h:265
ESP32AnalogRead servoPositionFeedback
Definition: RCCTL.ino:22
void setup()
Definition: RCCTL.ino:69
ESP32AnalogRead leftLineSensor
Definition: RCCTL.ino:20
enum connectionState getState()
Definition: WifiManager.cpp:23
void attach(int pin)
Servo lifter
Definition: RCCTL.ino:18
Timer dashboardUpdateTimer
Definition: RCCTL.ino:28
void attach(int trigger, int echo)
Attach 2 pins to be used as triger and echo.
Definition: Rangefinder.cpp:92
#define SERVO_PIN
Definition: RBE1001Lib.h:36
uint32_t readMiliVolts()
void runStateMachine()
Definition: RCCTL.ino:99
Rangefinder rangefinder1
Definition: RCCTL.ino:16
float getDegreesPerSecond()
Definition: Motor.cpp:551
int attach(int pin)
Definition: ESP32Servo.cpp:73
A Timer that remembers a starting time and interval allow easy periodic events.
Definition: Timer.h:11
uint32_t txPacketCount
Definition: WebPage.h:57
ESP32AnalogRead rightLineSensor
Definition: RCCTL.ino:21
int inc
Definition: RCCTL.ino:68
uint32_t packet_old
Definition: RCCTL.ino:117
#define SIDE_ULTRASONIC_TRIG
Definition: RBE1001Lib.h:24