Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
RWR_Utils.hxx@11:2329cb17b332, 2021-07-26 (annotated)
- Committer:
- steliansaracut
- Date:
- Mon Jul 26 20:32:36 2021 +0300
- Revision:
- 11:2329cb17b332
- Parent:
- 9:6c8dcbfc625a
changed SSID
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
steliansaracut | 1:755da47160cb | 1 | #pragma once |
steliansaracut | 1:755da47160cb | 2 | |
steliansaracut | 1:755da47160cb | 3 | #include "mbed.h" |
steliansaracut | 1:755da47160cb | 4 | |
steliansaracut | 1:755da47160cb | 5 | // pin assignment |
steliansaracut | 1:755da47160cb | 6 | #define ESP_RX PA_9 |
steliansaracut | 1:755da47160cb | 7 | #define ESP_TX PA_10 |
steliansaracut | 1:755da47160cb | 8 | #define ESP_RESET PA_12 |
steliansaracut | 1:755da47160cb | 9 | #define QTR_13 PB_0 |
steliansaracut | 1:755da47160cb | 10 | #define ESP_CHPD PB_7 |
steliansaracut | 1:755da47160cb | 11 | #define DRV_BENBL PB_6 |
steliansaracut | 1:755da47160cb | 12 | #define QTR_11 PB_1 |
steliansaracut | 1:755da47160cb | 13 | #define DRV_BPHASE PF_0 |
steliansaracut | 1:755da47160cb | 14 | #define QTR_CTL PF_1 |
steliansaracut | 1:755da47160cb | 15 | #define ESP_GPIO0 PA_8 |
steliansaracut | 1:755da47160cb | 16 | #define DRV_APHASE PA_11 |
steliansaracut | 1:755da47160cb | 17 | #define QTR_9 PA_7 |
steliansaracut | 1:755da47160cb | 18 | #define DRV_AENBL PA_4 |
steliansaracut | 1:755da47160cb | 19 | #define QTR_7 PA_3 |
steliansaracut | 1:755da47160cb | 20 | #define QTR_5 PA_1 |
steliansaracut | 1:755da47160cb | 21 | #define QTR_3 PA_0 |
steliansaracut | 1:755da47160cb | 22 | |
steliansaracut | 1:755da47160cb | 23 | // motor driver PWM frequency |
steliansaracut | 1:755da47160cb | 24 | #define PWM_FREQUENCY 32.0e3f |
steliansaracut | 1:755da47160cb | 25 | #define PWM_PERIOD 1.0f / PWM_FREQUENCY |
steliansaracut | 1:755da47160cb | 26 | |
steliansaracut | 1:755da47160cb | 27 | // number of sensors on QTR module |
steliansaracut | 1:755da47160cb | 28 | #define SENSORS_BAR_SIZE 6 |
steliansaracut | 1:755da47160cb | 29 | // sensors reading when no line is detected |
steliansaracut | 1:755da47160cb | 30 | #define SENSORS_ALL_ONES ((1 << SENSORS_BAR_SIZE) - 1U) |
steliansaracut | 1:755da47160cb | 31 | |
steliansaracut | 1:755da47160cb | 32 | // hysteresis thresholds for analog sensors |
steliansaracut | 1:755da47160cb | 33 | #define QTR_THRESHOLD_HIGH (uint32_t) 5000U |
steliansaracut | 1:755da47160cb | 34 | #define QTR_THRESHOLD_LOW (uint32_t) 20000U |
steliansaracut | 1:755da47160cb | 35 | |
steliansaracut | 1:755da47160cb | 36 | // PID parameters |
steliansaracut | 9:6c8dcbfc625a | 37 | #define PID_UPDATE_PERIOD_US 1000 |
steliansaracut | 9:6c8dcbfc625a | 38 | #define PID_SETPOINT 0.0f |
steliansaracut | 9:6c8dcbfc625a | 39 | #define PID_DEFAULT_KP 0.6f |
steliansaracut | 9:6c8dcbfc625a | 40 | // #define PID_DEFAULT_KP 0.25f |
steliansaracut | 9:6c8dcbfc625a | 41 | #define PID_DEFAULT_KI 0.0f |
steliansaracut | 9:6c8dcbfc625a | 42 | #define PID_DEFAULT_KD 0.0f |
steliansaracut | 9:6c8dcbfc625a | 43 | #define NO_LINE_SPEED_MOTOR_A 0.0f |
steliansaracut | 9:6c8dcbfc625a | 44 | #define NO_LINE_SPEED_MOTOR_B 1.0f |
steliansaracut | 1:755da47160cb | 45 | |
steliansaracut | 1:755da47160cb | 46 | // web interface parameters |
steliansaracut | 9:6c8dcbfc625a | 47 | #define WEB_ESP_BAUD 115200 |
steliansaracut | 9:6c8dcbfc625a | 48 | #define WEB_RECEIVE_BUFFER_SIZE 1024 |
steliansaracut | 9:6c8dcbfc625a | 49 | #define WEB_SEND_BUFFER_SIZE 2048 |
steliansaracut | 9:6c8dcbfc625a | 50 | #define WEB_RECEIVE_TIMEOUT 10000 |
steliansaracut | 9:6c8dcbfc625a | 51 | #define WEB_ESP_INIT_ATTEMPTS 10 |
steliansaracut | 9:6c8dcbfc625a | 52 | #define WEB_IPD_HEADER_MIN_SIZE 9 |
steliansaracut | 11:2329cb17b332 | 53 | #define WEB_SSID "RWR_1" |
steliansaracut | 9:6c8dcbfc625a | 54 | #define WEB_PASSWORD "RWRWRWRW" |
steliansaracut | 9:6c8dcbfc625a | 55 | #define WEB_CHANNEL "1" |
steliansaracut | 9:6c8dcbfc625a | 56 | #define WEB_ECN "3" |
steliansaracut | 9:6c8dcbfc625a | 57 | #define WEB_MAX_CONNECTIONS "1" |
steliansaracut | 9:6c8dcbfc625a | 58 | #define WEB_HIDDEN "0" |
steliansaracut | 9:6c8dcbfc625a | 59 | #define WEB_AP_IP "192.168.0.1" |
steliansaracut | 9:6c8dcbfc625a | 60 | #define WEB_AP_GATEWAY "192.168.0.1" |
steliansaracut | 9:6c8dcbfc625a | 61 | #define WEB_AP_NETMASK "255.255.255.0" |
steliansaracut | 9:6c8dcbfc625a | 62 | #define WEB_AP_PORT "80" |
steliansaracut | 9:6c8dcbfc625a | 63 | #define WEB_MOTORS_ENABLED_DEFAULT 0 |
steliansaracut | 9:6c8dcbfc625a | 64 | |
steliansaracut | 9:6c8dcbfc625a | 65 | #define ESP_DEBUG |
steliansaracut | 9:6c8dcbfc625a | 66 | |
steliansaracut | 9:6c8dcbfc625a | 67 | #define ESP_SEND_AND_PROCESS_STATUS(command, next_state) status = ESPSendCommandAndProcessReply(command);\ |
steliansaracut | 9:6c8dcbfc625a | 68 | if (status == STATUS_OK)\ |
steliansaracut | 9:6c8dcbfc625a | 69 | {\ |
steliansaracut | 9:6c8dcbfc625a | 70 | ESPParserState = next_state;\ |
steliansaracut | 9:6c8dcbfc625a | 71 | } else\ |
steliansaracut | 9:6c8dcbfc625a | 72 | if (status < STATUS_PENDING)\ |
steliansaracut | 9:6c8dcbfc625a | 73 | {\ |
steliansaracut | 9:6c8dcbfc625a | 74 | serial.printf("ESP command failed: %s\r\n", command );\ |
steliansaracut | 9:6c8dcbfc625a | 75 | ESPParserState = RESET_ESP;\ |
steliansaracut | 9:6c8dcbfc625a | 76 | }\ |
steliansaracut | 9:6c8dcbfc625a | 77 | |
steliansaracut | 9:6c8dcbfc625a | 78 | // forward USB serial to ESP serial |
steliansaracut | 9:6c8dcbfc625a | 79 | // #define ENABLE_PASSTHROUGH |
steliansaracut | 9:6c8dcbfc625a | 80 | |
steliansaracut | 9:6c8dcbfc625a | 81 | typedef enum { |
steliansaracut | 9:6c8dcbfc625a | 82 | STATUS_OK = 1, |
steliansaracut | 9:6c8dcbfc625a | 83 | STATUS_PENDING = 0, |
steliansaracut | 9:6c8dcbfc625a | 84 | STATUS_TIMEOUT = -1, |
steliansaracut | 9:6c8dcbfc625a | 85 | STATUS_ERROR = -2 |
steliansaracut | 9:6c8dcbfc625a | 86 | } status_t; |
steliansaracut | 9:6c8dcbfc625a | 87 | |
steliansaracut | 9:6c8dcbfc625a | 88 | // web interface state type |
steliansaracut | 9:6c8dcbfc625a | 89 | typedef enum { |
steliansaracut | 9:6c8dcbfc625a | 90 | IDLE, |
steliansaracut | 9:6c8dcbfc625a | 91 | RESET_ESP, |
steliansaracut | 9:6c8dcbfc625a | 92 | WAIT_FOR_READY, |
steliansaracut | 9:6c8dcbfc625a | 93 | INIT_SEND_AT, |
steliansaracut | 9:6c8dcbfc625a | 94 | INIT_SEND_CWMODE, |
steliansaracut | 9:6c8dcbfc625a | 95 | INIT_SEND_CWSAP, |
steliansaracut | 9:6c8dcbfc625a | 96 | INIT_SEND_CIPAP, |
steliansaracut | 9:6c8dcbfc625a | 97 | INIT_SEND_CIPMUX, |
steliansaracut | 9:6c8dcbfc625a | 98 | INIT_SEND_CIPSERVER, |
steliansaracut | 9:6c8dcbfc625a | 99 | PARSE_IPD, |
steliansaracut | 9:6c8dcbfc625a | 100 | PARSE_REQUEST_BODY, |
steliansaracut | 9:6c8dcbfc625a | 101 | GENERATE_WEB_PAGE, |
steliansaracut | 9:6c8dcbfc625a | 102 | WAIT_FOR_CIPSEND_READY, |
steliansaracut | 9:6c8dcbfc625a | 103 | SEND_PAGE, |
steliansaracut | 9:6c8dcbfc625a | 104 | WAIT_FOR_SEND_OK, |
steliansaracut | 9:6c8dcbfc625a | 105 | CLOSE_CONNECTION |
steliansaracut | 9:6c8dcbfc625a | 106 | } state_t; |
steliansaracut | 9:6c8dcbfc625a | 107 | |
steliansaracut | 9:6c8dcbfc625a | 108 | typedef struct { |
steliansaracut | 9:6c8dcbfc625a | 109 | int32_t motorsEnabled; |
steliansaracut | 9:6c8dcbfc625a | 110 | int32_t Kp; |
steliansaracut | 9:6c8dcbfc625a | 111 | int32_t Ki; |
steliansaracut | 9:6c8dcbfc625a | 112 | int32_t Kd; |
steliansaracut | 9:6c8dcbfc625a | 113 | } web_settings_t; |
steliansaracut | 9:6c8dcbfc625a | 114 | |
steliansaracut | 9:6c8dcbfc625a | 115 | typedef struct { |
steliansaracut | 9:6c8dcbfc625a | 116 | uint32_t digitizedSensorValues; |
steliansaracut | 9:6c8dcbfc625a | 117 | uint32_t analogSensorValues[SENSORS_BAR_SIZE]; |
steliansaracut | 9:6c8dcbfc625a | 118 | uint32_t analogSensorValuesAverage; |
steliansaracut | 9:6c8dcbfc625a | 119 | } web_status_t; |
steliansaracut | 9:6c8dcbfc625a | 120 | |
steliansaracut | 9:6c8dcbfc625a | 121 | static state_t ESPParserState = RESET_ESP; |
steliansaracut | 9:6c8dcbfc625a | 122 | static web_status_t webStatus = {}; |
steliansaracut | 9:6c8dcbfc625a | 123 | static Timer ESPCommunicationTimer; |
steliansaracut | 9:6c8dcbfc625a | 124 | static char webReceiveBuffer[WEB_RECEIVE_BUFFER_SIZE]; |
steliansaracut | 9:6c8dcbfc625a | 125 | static char webSendBuffer[WEB_SEND_BUFFER_SIZE]; |
steliansaracut | 9:6c8dcbfc625a | 126 | static volatile char *webReceiveBufferPointer = webReceiveBuffer; |
steliansaracut | 9:6c8dcbfc625a | 127 | static char *webReceiveRequestsData = NULL; |
steliansaracut | 9:6c8dcbfc625a | 128 | static volatile bool webReceivedData = false; |
steliansaracut | 9:6c8dcbfc625a | 129 | static volatile bool webReceiveBufferFull = false; |
steliansaracut | 9:6c8dcbfc625a | 130 | static int webConnectionId = -1; |
steliansaracut | 9:6c8dcbfc625a | 131 | static int webPayloadSize = -1; |
steliansaracut | 9:6c8dcbfc625a | 132 | static bool webSendNewCommand = true; |
steliansaracut | 9:6c8dcbfc625a | 133 | static int32_t espInitAttempts = 1; |
steliansaracut | 9:6c8dcbfc625a | 134 | |
steliansaracut | 9:6c8dcbfc625a | 135 | void ESPReceiveIRQ(); |
steliansaracut | 9:6c8dcbfc625a | 136 | status_t ESPProcessReply(); |
steliansaracut | 9:6c8dcbfc625a | 137 | status_t ESPSendCommandAndProcessReply(const char *command); |
steliansaracut | 9:6c8dcbfc625a | 138 | status_t ESPRunStateMachine(); |
steliansaracut | 1:755da47160cb | 139 | |
steliansaracut | 1:755da47160cb | 140 | class Motor |
steliansaracut | 1:755da47160cb | 141 | { |
steliansaracut | 1:755da47160cb | 142 | public: |
steliansaracut | 1:755da47160cb | 143 | Motor(const PinName &_in1Pin, const PinName &_in2Pin, const bool _brake = false); |
steliansaracut | 1:755da47160cb | 144 | void setSpeed(const float speed); |
steliansaracut | 1:755da47160cb | 145 | void setBrake(const bool _brake); |
steliansaracut | 1:755da47160cb | 146 | |
steliansaracut | 1:755da47160cb | 147 | private: |
steliansaracut | 1:755da47160cb | 148 | PwmOut in1PWM; |
steliansaracut | 1:755da47160cb | 149 | PwmOut in2PWM; |
steliansaracut | 1:755da47160cb | 150 | bool brake; |
steliansaracut | 1:755da47160cb | 151 | }; |
steliansaracut | 1:755da47160cb | 152 | |
steliansaracut | 1:755da47160cb | 153 | class QTR |
steliansaracut | 1:755da47160cb | 154 | { |
steliansaracut | 1:755da47160cb | 155 | public: |
steliansaracut | 1:755da47160cb | 156 | // QTR CTL pin, array of analog inputs connected to sensor bar, number of pulses applied to CTL pin |
steliansaracut | 1:755da47160cb | 157 | QTR(DigitalOut &_ctl, AnalogIn (&_sensors)[SENSORS_BAR_SIZE], const uint32_t dimPulses = 0); |
steliansaracut | 1:755da47160cb | 158 | |
steliansaracut | 1:755da47160cb | 159 | // dim sensors by applying pulses on CTL pin |
steliansaracut | 1:755da47160cb | 160 | void dim(const uint32_t pulses); |
steliansaracut | 1:755da47160cb | 161 | |
steliansaracut | 1:755da47160cb | 162 | // returns digitized sensor values as bits of an uint32_t |
steliansaracut | 1:755da47160cb | 163 | // a bit of 0 means that the corresponding sensor detects a reflective surface |
steliansaracut | 1:755da47160cb | 164 | // index 0 in sensors array corresponds to LSB in returned value |
steliansaracut | 1:755da47160cb | 165 | // uses hysteresis to reduce noise |
steliansaracut | 1:755da47160cb | 166 | uint32_t readSensorsAsDigital(); |
steliansaracut | 1:755da47160cb | 167 | |
steliansaracut | 5:6cd52beadb7c | 168 | // writes raw sensor values to buffer via the pointer argument |
steliansaracut | 5:6cd52beadb7c | 169 | void readSensorsAsAnalog(uint32_t *buffer); |
steliansaracut | 5:6cd52beadb7c | 170 | |
steliansaracut | 1:755da47160cb | 171 | // return line position as a float between -1.0 (left) and 1.0 (right) |
steliansaracut | 1:755da47160cb | 172 | float readSensorsAsFloat(); |
steliansaracut | 1:755da47160cb | 173 | |
steliansaracut | 1:755da47160cb | 174 | // convert digitized sensor values to float |
steliansaracut | 1:755da47160cb | 175 | float digitizedSensorsToFloat(uint32_t digitizedSensors); |
steliansaracut | 1:755da47160cb | 176 | |
steliansaracut | 1:755da47160cb | 177 | private: |
steliansaracut | 1:755da47160cb | 178 | DigitalOut &ctl; |
steliansaracut | 1:755da47160cb | 179 | AnalogIn (&sensors)[SENSORS_BAR_SIZE]; |
steliansaracut | 1:755da47160cb | 180 | uint32_t lastSensorState; |
steliansaracut | 1:755da47160cb | 181 | }; |