Skip to content

Commit 0b6ce1e

Browse files
committed
non blocking commands, 3. Party libs and updates
1 parent fd4e9ac commit 0b6ce1e

28 files changed

+1317
-990
lines changed

.github/workflows/TestCompile.yml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@ jobs:
7676
uses: actions/checkout@master
7777

7878
- name: Checkout new BlueDisplay library
79-
uses: actions/checkout@v3
79+
uses: actions/checkout@master
8080
with:
8181
repository: ArminJo/Arduino-BlueDisplay
8282
ref: master
@@ -85,7 +85,7 @@ jobs:
8585

8686
# Currently included in source directory
8787
# - name: Checkout new PWMMotorControl library
88-
# uses: actions/checkout@v3
88+
# uses: actions/checkout@master
8989
# with:
9090
# repository: ArminJo/PWMMotorControl
9191
# ref: master

README.md

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -24,11 +24,13 @@ Requires the Arduino library [BlueDisplay](https://github.com/ArminJo/Arduino-Bl
2424

2525
#### If you find this program useful, please give it a star.
2626

27+
🌎 [Google Translate](https://translate.google.com/translate?sl=en&u=https://github.com/ArminJo/Arduino-RobotCar)
28+
2729
<br/>
2830

2931
# Features
3032
- Obstacles avoidance by a HC-SR04 ultrasonic sensor mounted on a SG90 Servo which continuously scans the environment.
31-
- To overcome the drawbacks of ultrasonic sensors, an additional IR or TOF (TimeOfFlight) sensor can be mounted to the servo.
33+
- To overcome the drawbacks of ultrasonic sensors, an additional Sharp 430/1080/20150/100550 (GP2Y0A21YK0F) IR or VL53L1X TOF (TimeOfFlight) sensor can be mounted to the servo.
3234
- Manual control implemented by a GUI using a Bluetooth HC-05 Module and the [BlueDisplay library](https://github.com/ArminJo/Arduino-BlueDisplay).
3335

3436
**Just overwrite the function doUserCollisionAvoiding() to test your own skill**.
@@ -101,10 +103,10 @@ Compile options for the used **PWMMotorControl library** like `USE_ENCODER_MOTOR
101103
| `CAR_HAS_TOF_DISTANCE_SENSOR` | disabled | Use VL53L1X TimeOfFlight distance sensor. |
102104
| `CAR_HAS_DISTANCE_SERVO` | disabled | Distance sensor is mounted on a pan servo (default for most China smart cars). |
103105
| `DISTANCE_SERVO_IS_MOUNTED_HEAD_DOWN` | disabled | Distance.h | The distance servo is mounted head down to detect even small obstacles. The Servo direction is reverse then. |
104-
| `CAR_HAS_PAN_SERVO` | disabled | Enables the pan slider for the `PanServo` at the `PIN_PAN_SERVO` pin. |
105-
| `CAR_HAS_TILT_SERVO` | disabled | Enables the tilt slider for the `TiltServo` at the `PIN_TILT_SERVO` pin. |
106-
| `CAR_HAS_CAMERA` | disabled | Enables the `Camera` button for the `PIN_CAMERA_SUPPLY_CONTROL` pin. |
107-
| `CAR_HAS_LASER` | disabled | Enables the `Laser` button for the `PIN_LASER_OUT` / `LED_BUILTIN` pin. |
106+
| `CAR_HAS_PAN_SERVO` | disabled | Enables the pan slider for the `PanServo` at the `PAN_SERVO_PIN` pin. |
107+
| `CAR_HAS_TILT_SERVO` | disabled | Enables the tilt slider for the `TiltServo` at the `TILT_SERVO_PIN` pin. |
108+
| `CAR_HAS_CAMERA` | disabled | Enables the `Camera` button for the `CAMERA_SUPPLY_CONTROL_PIN` pin. |
109+
| `CAR_HAS_LASER` | disabled | Enables the `Laser` button for the `LASER_OUT_PIN` / `LED_BUILTIN` pin. |
108110
| `ENABLE_RTTTL_FOR_CAR` | undefined | Plays melody after initial timeout has reached. Enables the Melody button, which plays a random melody. |
109111
| `MONITOR_VIN_VOLTAGE` | disabled | Shows VIN voltage and monitors it for undervoltage. VIN/11 at A2, 1 M&ohm; to VIN, 100 k&ohm; to ground. |
110112
| `ENABLE_EEPROM_STORAGE` | disabled | Activates the buttons to store compensation values. Normally they are stored after calibration. |

src/ADCUtils.h

Lines changed: 62 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -112,8 +112,47 @@
112112
#error "No temperature channel definitions specified for this AVR CPU"
113113
#endif
114114

115-
extern float sVCCVoltage;
116-
extern uint16_t sVCCVoltageMillivolt;
115+
/*
116+
* Thresholds for OVER and UNDER voltage and detection of kind of power supply (USB or Li-ion)
117+
*
118+
* Default values are suitable for Li-ion batteries.
119+
* We normally have voltage drop at the connectors, so the battery voltage is assumed slightly higher, than the Arduino VCC.
120+
* But keep in mind that the ultrasonic distance module HC-SR04 may not work reliable below 3.7 volt.
121+
*/
122+
#if !defined(LI_ION_VCC_UNDERVOLTAGE_THRESHOLD_MILLIVOLT)
123+
#define LI_ION_VCC_UNDERVOLTAGE_THRESHOLD_MILLIVOLT 3400 // Do not stress your battery and we require some power for standby
124+
#endif
125+
#if !defined(LI_ION_VCC_EMERGENCY_UNDERVOLTAGE_THRESHOLD_MILLIVOLT)
126+
#define LI_ION_VCC_EMERGENCY_UNDERVOLTAGE_THRESHOLD_MILLIVOLT 3000 // Many Li-ions are specified down to 3.0 volt
127+
#endif
128+
129+
#if !defined(VCC_UNDERVOLTAGE_THRESHOLD_MILLIVOLT)
130+
#define VCC_UNDERVOLTAGE_THRESHOLD_MILLIVOLT LI_ION_VCC_UNDERVOLTAGE_THRESHOLD_MILLIVOLT
131+
#endif
132+
#if !defined(VCC_EMERGENCY_UNDERVOLTAGE_THRESHOLD_MILLIVOLT)
133+
#define VCC_EMERGENCY_UNDERVOLTAGE_THRESHOLD_MILLIVOLT LI_ION_VCC_EMERGENCY_UNDERVOLTAGE_THRESHOLD_MILLIVOLT
134+
#endif
135+
#if !defined(VCC_OVERVOLTAGE_THRESHOLD_MILLIVOLT)
136+
#define VCC_OVERVOLTAGE_THRESHOLD_MILLIVOLT 5250 // + 5 % operation voltage
137+
#endif
138+
#if !defined(VCC_EMERGENCY_OVERVOLTAGE_THRESHOLD_MILLIVOLT)
139+
#define VCC_EMERGENCY_OVERVOLTAGE_THRESHOLD_MILLIVOLT 5500 // +10 %. Max recommended operation voltage
140+
#endif
141+
#if !defined(VCC_CHECK_PERIOD_MILLIS)
142+
#define VCC_CHECK_PERIOD_MILLIS 10000L // 10 seconds period of VCC checks
143+
#endif
144+
#if !defined(VCC_UNDERVOLTAGE_CHECKS_BEFORE_STOP)
145+
#define VCC_UNDERVOLTAGE_CHECKS_BEFORE_STOP 6 // Shutdown after 6 times (60 seconds) VCC below VCC_UNDERVOLTAGE_THRESHOLD_MILLIVOLT or 1 time below VCC_EMERGENCY_UNDERVOLTAGE_THRESHOLD_MILLIVOLT
146+
#endif
147+
148+
#if !defined(VOLTAGE_USB_POWERED_LOWER_THRESHOLD_MILLIVOLT)
149+
#define VOLTAGE_USB_POWERED_LOWER_THRESHOLD_MILLIVOLT 4300 // Assume USB powered above this voltage
150+
#endif
151+
152+
#if !defined(VOLTAGE_USB_POWERED_UPPER_THRESHOLD_MILLIVOLT)
153+
#define VOLTAGE_USB_POWERED_UPPER_THRESHOLD_MILLIVOLT 4950 // Assume USB powered below this voltage, because of the loss in USB cable. If we have > 4950, we assume to be powered by VIN.
154+
// In contrast to e.g. powered by VIN, which results in almost perfect 5 volt supply
155+
#endif
117156

118157
extern long sLastVCCCheckMillis;
119158
extern uint8_t sVCCTooLowCounter;
@@ -124,7 +163,6 @@ uint16_t waitAndReadADCChannelWithReference(uint8_t aADCChannelNumber, uint8_t a
124163
uint16_t waitAndReadADCChannelWithReferenceAndRestoreADMUXAndReference(uint8_t aADCChannelNumber, uint8_t aReference);
125164
uint16_t readADCChannelWithOversample(uint8_t aADCChannelNumber, uint8_t aOversampleExponent);
126165
void setADCChannelAndReferenceForNextConversion(uint8_t aADCChannelNumber, uint8_t aReference);
127-
uint16_t readADCChannelWithReferenceOversample(uint8_t aADCChannelNumber, uint8_t aReference, uint8_t aOversampleExponent);
128166
uint16_t readADCChannelWithReferenceOversampleFast(uint8_t aADCChannelNumber, uint8_t aReference, uint8_t aOversampleExponent);
129167
uint16_t readADCChannelWithReferenceMultiSamples(uint8_t aADCChannelNumber, uint8_t aReference, uint8_t aNumberOfSamples);
130168
uint16_t readADCChannelWithReferenceMax(uint8_t aADCChannelNumber, uint8_t aReference, uint16_t aNumberOfSamples);
@@ -139,9 +177,7 @@ uint8_t checkAndWaitForReferenceAndChannelToSwitch(uint8_t aADCChannelNumber, ui
139177
*/
140178
float getVCCVoltageSimple(void);
141179
void readVCCVoltageSimple(void);
142-
uint16_t getVCCVoltageMillivoltSimple(void);
143180
void readVCCVoltageMillivoltSimple(void);
144-
float getVCCVoltage(void);
145181
void readVCCVoltage(void);
146182
uint16_t getVCCVoltageMillivolt(void);
147183
void readVCCVoltageMillivolt(void);
@@ -156,11 +192,27 @@ float getCPUTemperatureSimple(void);
156192
float getCPUTemperature(void);
157193
float getTemperature(void) __attribute__ ((deprecated ("Renamed to getCPUTemperature()"))); // deprecated
158194

159-
bool isVCCTooLowMultipleTimes();
160-
void resetVCCTooLowMultipleTimes();
161-
bool isVCCTooLow();
162-
bool isVCCTooHigh();
163-
bool isVCCTooHighSimple();
195+
bool isVCCUSBPowered();
196+
bool isVCCUSBPowered(Print *aSerial);
197+
bool isVCCUndervoltageMultipleTimes();
198+
void resetCounterForVCCUndervoltageMultipleTimes();
199+
bool isVCCUndervoltage();
200+
bool isVCCEmergencyUndervoltage();
201+
bool isVCCOvervoltage();
202+
bool isVCCOvervoltageSimple();
164203

165204
#endif // defined(__AVR__) ...
205+
206+
/*
207+
* Variables and functions defined as dummies to allow for seamless compiling on non AVR platforms
208+
*/
209+
extern float sVCCVoltage;
210+
extern uint16_t sVCCVoltageMillivolt;
211+
212+
uint16_t readADCChannelWithReferenceOversample(uint8_t aADCChannelNumber, uint8_t aReference, uint8_t aOversampleExponent);
213+
214+
uint16_t getVCCVoltageMillivoltSimple(void);
215+
float getVCCVoltage(void);
216+
float getCPUTemperature(void);
217+
166218
#endif // _ADC_UTILS_H

src/ADCUtils.hpp

Lines changed: 70 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ union WordUnionForADCUtils {
5858
* Enable this to see information on each call.
5959
* Since there should be no library which uses Serial, it should only be enabled for development purposes.
6060
*/
61-
#if defined(DEBUG)
61+
#if defined(DEBUG) && !defined(LOCAL_DEBUG)
6262
#define LOCAL_DEBUG
6363
#else
6464
//#define LOCAL_DEBUG // This enables debug output only for this file
@@ -481,7 +481,7 @@ void readAndPrintVCCVoltageMillivolt(Print *aSerial) {
481481
void readVCCVoltageSimple(void) {
482482
// use AVCC with (optional) external capacitor at AREF pin as reference
483483
float tVCC = readADCChannelWithReferenceMultiSamples(ADC_1_1_VOLT_CHANNEL_MUX, DEFAULT, 4);
484-
sVCCVoltage = (1023 * 1.1 * 4) / tVCC;
484+
sVCCVoltage = (1023 * (((float) ADC_INTERNAL_REFERENCE_MILLIVOLT) / 1000) * 4) / tVCC;
485485
}
486486

487487
/*
@@ -540,33 +540,37 @@ uint16_t getVoltageMillivoltWith_1_1VoltReference(uint8_t aADCChannelForVoltageM
540540
}
541541

542542
/*
543-
* Default values are suitable for Li-ion batteries.
544-
* We normally have voltage drop at the connectors, so the battery voltage is assumed slightly higher, than the Arduino VCC.
545-
* But keep in mind that the ultrasonic distance module HC-SR04 may not work reliable below 3.7 volt.
543+
* Return true if sVCCVoltageMillivolt is > 4.3 V and < 4.95 V
546544
*/
547-
#if !defined(VCC_STOP_THRESHOLD_MILLIVOLT)
548-
#define VCC_STOP_THRESHOLD_MILLIVOLT 3400 // Do not stress your battery and we require some power for standby
549-
#endif
550-
#if !defined(VCC_EMERGENCY_STOP_MILLIVOLT)
551-
#define VCC_EMERGENCY_STOP_MILLIVOLT 3000 // Many Li-ions are specified down to 3.0 volt
552-
#endif
553-
#if !defined(VCC_TOO_HIGH_STOP_MILLIVOLT)
554-
#define VCC_TOO_HIGH_STOP_MILLIVOLT 5250 // + 5 % operation voltage
555-
#endif
556-
#if !defined(VCC_TOO_HIGH_EMERGENCY_STOP_MILLIVOLT)
557-
#define VCC_TOO_HIGH_EMERGENCY_STOP_MILLIVOLT 5500 // +10 %. Max recommended operation voltage
558-
#endif
559-
#if !defined(VCC_CHECK_PERIOD_MILLIS)
560-
#define VCC_CHECK_PERIOD_MILLIS 10000 // Period of VCC checks
561-
#endif
562-
#if !defined(VCC_CHECKS_TOO_LOW_BEFORE_STOP)
563-
#define VCC_CHECKS_TOO_LOW_BEFORE_STOP 6 // Shutdown after 6 times (60 seconds) VCC below VCC_STOP_THRESHOLD_MILLIVOLT or 1 time below VCC_EMERGENCY_STOP_MILLIVOLT
564-
#endif
545+
bool isVCCUSBPowered() {
546+
readVCCVoltageMillivolt();
547+
return (VOLTAGE_USB_POWERED_LOWER_THRESHOLD_MILLIVOLT < sVCCVoltageMillivolt
548+
&& sVCCVoltageMillivolt < VOLTAGE_USB_POWERED_UPPER_THRESHOLD_MILLIVOLT);
549+
}
565550

566551
/*
567-
* @ return true only once, when VCC_CHECKS_TOO_LOW_BEFORE_STOP (6) times voltage too low -> shutdown
552+
* Return true if sVCCVoltageMillivolt is > 4.3 V and < 4.95 V
568553
*/
569-
bool isVCCTooLowMultipleTimes() {
554+
bool isVCCUSBPowered(Print *aSerial) {
555+
readVCCVoltageMillivolt();
556+
aSerial->print(F("USB powered is "));
557+
bool tReturnValue;
558+
if (VOLTAGE_USB_POWERED_LOWER_THRESHOLD_MILLIVOLT
559+
< sVCCVoltageMillivolt && sVCCVoltageMillivolt < VOLTAGE_USB_POWERED_UPPER_THRESHOLD_MILLIVOLT) {
560+
tReturnValue = true;
561+
aSerial->print(F("true "));
562+
} else {
563+
tReturnValue = false;
564+
aSerial->print(F("false "));
565+
}
566+
printVCCVoltageMillivolt(aSerial);
567+
return tReturnValue;
568+
}
569+
570+
/*
571+
* @ return true only once, when VCC_UNDERVOLTAGE_CHECKS_BEFORE_STOP (6) times voltage too low -> shutdown
572+
*/
573+
bool isVCCUndervoltageMultipleTimes() {
570574
/*
571575
* Check VCC every VCC_CHECK_PERIOD_MILLIS (10) seconds
572576
*/
@@ -580,33 +584,35 @@ bool isVCCTooLowMultipleTimes() {
580584
readVCCVoltageMillivolt();
581585
# endif
582586

583-
if (sVCCTooLowCounter < VCC_CHECKS_TOO_LOW_BEFORE_STOP) {
587+
if (sVCCTooLowCounter < VCC_UNDERVOLTAGE_CHECKS_BEFORE_STOP) {
584588
/*
585589
* Do not check again if shutdown has happened
586590
*/
587-
if (sVCCVoltageMillivolt > VCC_STOP_THRESHOLD_MILLIVOLT) {
591+
if (sVCCVoltageMillivolt > VCC_UNDERVOLTAGE_THRESHOLD_MILLIVOLT) {
588592
sVCCTooLowCounter = 0; // reset counter
589593
} else {
590594
/*
591-
* Voltage too low, wait VCC_CHECKS_TOO_LOW_BEFORE_STOP (6) times and then shut down.
595+
* Voltage too low, wait VCC_UNDERVOLTAGE_CHECKS_BEFORE_STOP (6) times and then shut down.
592596
*/
593-
if (sVCCVoltageMillivolt < VCC_EMERGENCY_STOP_MILLIVOLT) {
597+
if (sVCCVoltageMillivolt < VCC_EMERGENCY_UNDERVOLTAGE_THRESHOLD_MILLIVOLT) {
594598
// emergency shutdown
595-
sVCCTooLowCounter = VCC_CHECKS_TOO_LOW_BEFORE_STOP;
599+
sVCCTooLowCounter = VCC_UNDERVOLTAGE_CHECKS_BEFORE_STOP;
596600
# if defined(INFO)
597-
Serial.println(F("Voltage < " STR(VCC_EMERGENCY_STOP_MILLIVOLT) " mV detected -> emergency shutdown"));
601+
Serial.println(
602+
F(
603+
"Voltage < " STR(VCC_EMERGENCY_UNDERVOLTAGE_THRESHOLD_MILLIVOLT) " mV detected -> emergency shutdown"));
598604
# endif
599605
} else {
600606
sVCCTooLowCounter++;
601607
# if defined(INFO)
602-
Serial.print(F("Voltage < " STR(VCC_STOP_THRESHOLD_MILLIVOLT) " mV detected: "));
603-
Serial.print(VCC_CHECKS_TOO_LOW_BEFORE_STOP - sVCCTooLowCounter);
608+
Serial.print(F("Voltage < " STR(VCC_UNDERVOLTAGE_THRESHOLD_MILLIVOLT) " mV detected: "));
609+
Serial.print(VCC_UNDERVOLTAGE_CHECKS_BEFORE_STOP - sVCCTooLowCounter);
604610
Serial.println(F(" tries left"));
605611
# endif
606612
}
607-
if (sVCCTooLowCounter == VCC_CHECKS_TOO_LOW_BEFORE_STOP) {
613+
if (sVCCTooLowCounter == VCC_UNDERVOLTAGE_CHECKS_BEFORE_STOP) {
608614
/*
609-
* 6 times voltage too low -> shutdown
615+
* 6 times voltage too low -> return signal for shutdown etc.
610616
*/
611617
return true;
612618
}
@@ -617,13 +623,22 @@ bool isVCCTooLowMultipleTimes() {
617623
}
618624

619625
/*
620-
* Return true if VCC_EMERGENCY_STOP_MILLIVOLT (3 V) reached
626+
* Return true if VCC_EMERGENCY_UNDERVOLTAGE_THRESHOLD_MILLIVOLT (3 V) reached
621627
*/
622-
bool isVCCTooLow() {
623-
return (sVCCVoltageMillivolt < VCC_EMERGENCY_STOP_MILLIVOLT);
628+
bool isVCCUndervoltage() {
629+
readVCCVoltageMillivolt();
630+
return (sVCCVoltageMillivolt < VCC_UNDERVOLTAGE_THRESHOLD_MILLIVOLT);
624631
}
625632

626-
void resetVCCTooLowMultipleTimes() {
633+
/*
634+
* Return true if VCC_EMERGENCY_UNDERVOLTAGE_THRESHOLD_MILLIVOLT (3 V) reached
635+
*/
636+
bool isVCCEmergencyUndervoltage() {
637+
readVCCVoltageMillivolt();
638+
return (sVCCVoltageMillivolt < VCC_EMERGENCY_UNDERVOLTAGE_THRESHOLD_MILLIVOLT);
639+
}
640+
641+
void resetCounterForVCCUndervoltageMultipleTimes() {
627642
sVCCTooLowCounter = 0;
628643
}
629644

@@ -636,13 +651,13 @@ void resetVCCTooLowMultipleTimes() {
636651
* Raw reading of 1.1 V is 204 at 5.5 V (+10 %).
637652
* @return true if 5 % overvoltage reached
638653
*/
639-
bool isVCCTooHigh() {
654+
bool isVCCOvervoltage() {
640655
readVCCVoltageMillivolt();
641-
return (sVCCVoltageMillivolt > VCC_TOO_HIGH_STOP_MILLIVOLT);
656+
return (sVCCVoltageMillivolt > VCC_OVERVOLTAGE_THRESHOLD_MILLIVOLT);
642657
}
643-
bool isVCCTooHighSimple() {
658+
bool isVCCOvervoltageSimple() {
644659
readVCCVoltageMillivoltSimple();
645-
return (sVCCVoltageMillivolt > VCC_TOO_HIGH_STOP_MILLIVOLT);
660+
return (sVCCVoltageMillivolt > VCC_OVERVOLTAGE_THRESHOLD_MILLIVOLT);
646661
}
647662

648663
/*
@@ -688,22 +703,27 @@ float getCPUTemperature(void) {
688703
#endif
689704
}
690705

691-
#elif defined(ARDUINO_ARCH_APOLLO3) // defined(ADC_UTILS_ARE_AVAILABLE)
692-
void ADCUtilsDummyToAvoidBFDAssertions(){
693-
;
694-
}
695-
696706
#else // defined(ADC_UTILS_ARE_AVAILABLE)
697707
// Dummy definition of functions defined in ADCUtils to compile examples without errors
708+
/*
709+
* Persistent storage for VCC value
710+
*/
711+
float sVCCVoltage;
712+
uint16_t sVCCVoltageMillivolt;
713+
714+
uint16_t getVCCVoltageMillivoltSimple(void){
715+
return 3300;
716+
}
717+
698718
uint16_t readADCChannelWithReferenceOversample(uint8_t aChannelNumber __attribute__((unused)),
699719
uint8_t aReference __attribute__((unused)), uint8_t aOversampleExponent __attribute__((unused))) {
700720
return 0;
701721
}
702722
float getCPUTemperature() {
703-
return 0.0;
723+
return 20.0;
704724
}
705725
float getVCCVoltage() {
706-
return 0.0;
726+
return 3.3;
707727
}
708728
#endif // defined(ADC_UTILS_ARE_AVAILABLE)
709729

0 commit comments

Comments
 (0)