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@3:2ae8e437d637, 2021-07-23 (annotated)
- Committer:
- steliansaracut
- Date:
- Fri Jul 23 23:22:37 2021 +0300
- Revision:
- 3:2ae8e437d637
- Parent:
- 2:821ffc555d49
- Child:
- 4:44f4d1cdd544
changed ESP init to be non-blocking
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 | 1:755da47160cb | 37 | #define PID_SETPOINT 0.0f |
steliansaracut | 1:755da47160cb | 38 | #define PID_KP 0.6f |
steliansaracut | 1:755da47160cb | 39 | // #define PID_KP 0.25f |
steliansaracut | 2:821ffc555d49 | 40 | #define PID_KI 0.0f |
steliansaracut | 1:755da47160cb | 41 | #define PID_KD 0.0f |
steliansaracut | 1:755da47160cb | 42 | #define NO_LINE_SPEED_MOTOR_A 0.0f |
steliansaracut | 1:755da47160cb | 43 | #define NO_LINE_SPEED_MOTOR_B 1.0f |
steliansaracut | 1:755da47160cb | 44 | |
steliansaracut | 1:755da47160cb | 45 | // web interface parameters |
steliansaracut | 2:821ffc555d49 | 46 | #define WEB_ESP_BAUD 115200 |
steliansaracut | 1:755da47160cb | 47 | #define WEB_RECEIVE_BUFFER_SIZE 2048 |
steliansaracut | 1:755da47160cb | 48 | #define WEB_SEND_BUFFER_SIZE 2048 |
steliansaracut | 3:2ae8e437d637 | 49 | #define WEB_RECEIVE_TIMEOUT 1000 |
steliansaracut | 3:2ae8e437d637 | 50 | #define WEB_ESP_INIT_ATTEMPTS 10 |
steliansaracut | 1:755da47160cb | 51 | #define WEB_IPD_HEADER_MIN_SIZE 9 |
steliansaracut | 1:755da47160cb | 52 | #define WEB_SSID "RWR_1" |
steliansaracut | 1:755da47160cb | 53 | #define WEB_PASSWORD "RWRWRWRW" |
steliansaracut | 1:755da47160cb | 54 | #define WEB_CHANNEL "1" |
steliansaracut | 1:755da47160cb | 55 | #define WEB_ECN "3" |
steliansaracut | 2:821ffc555d49 | 56 | #define WEB_MAX_CONNECTIONS "1" |
steliansaracut | 1:755da47160cb | 57 | #define WEB_HIDDEN "0" |
steliansaracut | 1:755da47160cb | 58 | #define WEB_AP_IP "192.168.0.1" |
steliansaracut | 1:755da47160cb | 59 | #define WEB_AP_GATEWAY "192.168.0.1" |
steliansaracut | 1:755da47160cb | 60 | #define WEB_AP_NETMASK "255.255.255.0" |
steliansaracut | 1:755da47160cb | 61 | #define WEB_AP_PORT "80" |
steliansaracut | 1:755da47160cb | 62 | |
steliansaracut | 1:755da47160cb | 63 | class Motor |
steliansaracut | 1:755da47160cb | 64 | { |
steliansaracut | 1:755da47160cb | 65 | public: |
steliansaracut | 1:755da47160cb | 66 | Motor(const PinName &_in1Pin, const PinName &_in2Pin, const bool _brake = false); |
steliansaracut | 1:755da47160cb | 67 | void setSpeed(const float speed); |
steliansaracut | 1:755da47160cb | 68 | void setBrake(const bool _brake); |
steliansaracut | 1:755da47160cb | 69 | |
steliansaracut | 1:755da47160cb | 70 | private: |
steliansaracut | 1:755da47160cb | 71 | PwmOut in1PWM; |
steliansaracut | 1:755da47160cb | 72 | PwmOut in2PWM; |
steliansaracut | 1:755da47160cb | 73 | bool brake; |
steliansaracut | 1:755da47160cb | 74 | }; |
steliansaracut | 1:755da47160cb | 75 | |
steliansaracut | 1:755da47160cb | 76 | class QTR |
steliansaracut | 1:755da47160cb | 77 | { |
steliansaracut | 1:755da47160cb | 78 | public: |
steliansaracut | 1:755da47160cb | 79 | // QTR CTL pin, array of analog inputs connected to sensor bar, number of pulses applied to CTL pin |
steliansaracut | 1:755da47160cb | 80 | QTR(DigitalOut &_ctl, AnalogIn (&_sensors)[SENSORS_BAR_SIZE], const uint32_t dimPulses = 0); |
steliansaracut | 1:755da47160cb | 81 | |
steliansaracut | 1:755da47160cb | 82 | // dim sensors by applying pulses on CTL pin |
steliansaracut | 1:755da47160cb | 83 | void dim(const uint32_t pulses); |
steliansaracut | 1:755da47160cb | 84 | |
steliansaracut | 1:755da47160cb | 85 | // returns digitized sensor values as bits of an uint32_t |
steliansaracut | 1:755da47160cb | 86 | // a bit of 0 means that the corresponding sensor detects a reflective surface |
steliansaracut | 1:755da47160cb | 87 | // index 0 in sensors array corresponds to LSB in returned value |
steliansaracut | 1:755da47160cb | 88 | // uses hysteresis to reduce noise |
steliansaracut | 1:755da47160cb | 89 | uint32_t readSensorsAsDigital(); |
steliansaracut | 1:755da47160cb | 90 | |
steliansaracut | 1:755da47160cb | 91 | // return line position as a float between -1.0 (left) and 1.0 (right) |
steliansaracut | 1:755da47160cb | 92 | float readSensorsAsFloat(); |
steliansaracut | 1:755da47160cb | 93 | |
steliansaracut | 1:755da47160cb | 94 | // convert digitized sensor values to float |
steliansaracut | 1:755da47160cb | 95 | float digitizedSensorsToFloat(uint32_t digitizedSensors); |
steliansaracut | 1:755da47160cb | 96 | |
steliansaracut | 1:755da47160cb | 97 | private: |
steliansaracut | 1:755da47160cb | 98 | DigitalOut &ctl; |
steliansaracut | 1:755da47160cb | 99 | AnalogIn (&sensors)[SENSORS_BAR_SIZE]; |
steliansaracut | 1:755da47160cb | 100 | uint32_t lastSensorState; |
steliansaracut | 1:755da47160cb | 101 | }; |