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.
Diff: RWR_Utils.hxx
- Revision:
- 1:755da47160cb
- Parent:
- 0:3960679bf525
- Child:
- 2:821ffc555d49
diff -r 3960679bf525 -r 755da47160cb RWR_Utils.hxx --- a/RWR_Utils.hxx Sun Jun 27 12:20:30 2021 +0000 +++ b/RWR_Utils.hxx Wed Jul 14 08:49:04 2021 +0300 @@ -1,75 +1,99 @@ -#pragma once - -#include "mbed.h" - -// pin assignment -#define ESP_RX PA_9 -#define ESP_TX PA_10 -#define ESP_RESET PA_12 -#define QTR_13 PB_0 -#define ESP_CHPD PB_7 -#define DRV_BENBL PB_6 -#define QTR_11 PB_1 -#define DRV_BPHASE PF_0 -#define QTR_CTL PF_1 -#define ESP_GPIO0 PA_8 -#define DRV_APHASE PA_11 -#define QTR_9 PA_7 -#define DRV_AENBL PA_4 -#define QTR_7 PA_3 -#define QTR_5 PA_1 -#define QTR_3 PA_0 - -// motor driver PWM frequency -#define PWM_FREQUENCY 32.0e3f -#define PWM_PERIOD 1.0f / PWM_FREQUENCY - -// number of sensors on QTR module -#define SENSORS_BAR_SIZE 6 - -// hysteresis thresholds for analog sensors -#define QTR_THRESHOLD_HIGH (uint32_t) 5000U -#define QTR_THRESHOLD_LOW (uint32_t) 20000U - -// PID parameters -#define PID_SETPOINT 0.0f -#define PID_KP 0.3f -#define PID_KD 0.0f -#define PID_KI 0.0001f - -class Motor -{ - public: - Motor(const PinName &_in1Pin, const PinName &_in2Pin, const bool _brake = false); - void setSpeed(const float speed); - void setBrake(const bool _brake); - - private: - PwmOut in1PWM; - PwmOut in2PWM; - bool brake; -}; - -class QTR -{ - public: - // QTR CTL pin, array of analog inputs connected to sensor bar, number of pulses applied to CTL pin - QTR(DigitalOut &_ctl, AnalogIn (&_sensors)[SENSORS_BAR_SIZE], const uint32_t dimPulses = 0); - - // dim sensors by applying pulses on CTL pin - void dim(const uint32_t pulses); - - // returns digitized sensor values as bits of an uint32_t - // a bit of 0 means that the corresponding sensor detects a reflective surface - // index 0 in sensors array corresponds to LSB in returned value - // uses hysteresis to reduce noise - uint32_t readSensorsAsDigital(); - - // return line position as a float between -1.0 (left) and 1.0 (right) - float readSensorsAsFloat(); - - private: - DigitalOut &ctl; - AnalogIn (&sensors)[SENSORS_BAR_SIZE]; - uint32_t lastSensorState; -}; +#pragma once + +#include "mbed.h" + +// pin assignment +#define ESP_RX PA_9 +#define ESP_TX PA_10 +#define ESP_RESET PA_12 +#define QTR_13 PB_0 +#define ESP_CHPD PB_7 +#define DRV_BENBL PB_6 +#define QTR_11 PB_1 +#define DRV_BPHASE PF_0 +#define QTR_CTL PF_1 +#define ESP_GPIO0 PA_8 +#define DRV_APHASE PA_11 +#define QTR_9 PA_7 +#define DRV_AENBL PA_4 +#define QTR_7 PA_3 +#define QTR_5 PA_1 +#define QTR_3 PA_0 + +// motor driver PWM frequency +#define PWM_FREQUENCY 32.0e3f +#define PWM_PERIOD 1.0f / PWM_FREQUENCY + +// number of sensors on QTR module +#define SENSORS_BAR_SIZE 6 +// sensors reading when no line is detected +#define SENSORS_ALL_ONES ((1 << SENSORS_BAR_SIZE) - 1U) + +// hysteresis thresholds for analog sensors +#define QTR_THRESHOLD_HIGH (uint32_t) 5000U +#define QTR_THRESHOLD_LOW (uint32_t) 20000U + +// PID parameters +#define PID_SETPOINT 0.0f +#define PID_KP 0.6f +// #define PID_KP 0.25f +#define PID_KD 0.0f +#define PID_KI 0.0f +#define NO_LINE_SPEED_MOTOR_A 0.0f +#define NO_LINE_SPEED_MOTOR_B 1.0f + +// web interface parameters +#define WEB_RECEIVE_BUFFER_SIZE 2048 +#define WEB_SEND_BUFFER_SIZE 2048 +#define WEB_RECEIVE_TIMEOUT 10000 +#define WEB_IPD_HEADER_MIN_SIZE 9 +#define WEB_SSID "RWR_1" +#define WEB_PASSWORD "RWRWRWRW" +#define WEB_CHANNEL "1" +#define WEB_ECN "3" +#define WEB_MAX_CONNECTIONS "2" +#define WEB_HIDDEN "0" +#define WEB_AP_IP "192.168.0.1" +#define WEB_AP_GATEWAY "192.168.0.1" +#define WEB_AP_NETMASK "255.255.255.0" +#define WEB_AP_PORT "80" + +class Motor +{ + public: + Motor(const PinName &_in1Pin, const PinName &_in2Pin, const bool _brake = false); + void setSpeed(const float speed); + void setBrake(const bool _brake); + + private: + PwmOut in1PWM; + PwmOut in2PWM; + bool brake; +}; + +class QTR +{ + public: + // QTR CTL pin, array of analog inputs connected to sensor bar, number of pulses applied to CTL pin + QTR(DigitalOut &_ctl, AnalogIn (&_sensors)[SENSORS_BAR_SIZE], const uint32_t dimPulses = 0); + + // dim sensors by applying pulses on CTL pin + void dim(const uint32_t pulses); + + // returns digitized sensor values as bits of an uint32_t + // a bit of 0 means that the corresponding sensor detects a reflective surface + // index 0 in sensors array corresponds to LSB in returned value + // uses hysteresis to reduce noise + uint32_t readSensorsAsDigital(); + + // return line position as a float between -1.0 (left) and 1.0 (right) + float readSensorsAsFloat(); + + // convert digitized sensor values to float + float digitizedSensorsToFloat(uint32_t digitizedSensors); + + private: + DigitalOut &ctl; + AnalogIn (&sensors)[SENSORS_BAR_SIZE]; + uint32_t lastSensorState; +};