Skip to content

Commit 8cbd310

Browse files
committed
mod: added wire lock and unlock, ultrasonic thread, removed i2cgrove dep, RP2040 OK, ESP32 NO
1 parent f795edb commit 8cbd310

6 files changed

+143
-57
lines changed

.vscode/settings.json

+14
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
{
2+
"files.associations": {
3+
"array": "cpp",
4+
"string": "cpp",
5+
"string_view": "cpp",
6+
"vector": "cpp",
7+
"__bit_reference": "cpp",
8+
"__hash_table": "cpp",
9+
"__split_buffer": "cpp",
10+
"deque": "cpp",
11+
"initializer_list": "cpp",
12+
"unordered_map": "cpp"
13+
}
14+
}

library.properties

+1-1
Original file line numberDiff line numberDiff line change
@@ -8,4 +8,4 @@ category=Communication
88
url=https://github.com/arduino-libraries/Arduino_ScienceKitCarrier
99
architectures=mbed,mbed_nano,esp32
1010
includes=Arduino_ScienceKitCarrier.h
11-
depends=Arduino_APDS9960,ArduinoBLE,WiFiNINA,INA2xx,Arduino_BMI270_BMM150,bsec2,Arduino_GroveI2C_Ultrasonic,OneWireNg
11+
depends=Arduino_APDS9960,ArduinoBLE,WiFiNINA,INA2xx,Arduino_BMI270_BMM150,bsec2,OneWireNg

src/Arduino_ScienceKitCarrier.cpp

+93-46
Original file line numberDiff line numberDiff line change
@@ -74,29 +74,36 @@ ScienceKitCarrier::ScienceKitCarrier(){
7474
range1=0;
7575
range2=0;
7676

77-
ultrasonic = new Arduino_GroveI2C_Ultrasonic();
78-
distance=0.0;
79-
travel_time=0.0;
77+
ultrasonic_measure = 0.0;
78+
ultrasonic_data = 0;
79+
distance = 0.0;
80+
travel_time = 0.0;
8081
ultrasonic_is_connected=false;
8182

8283
external_temperature=EXTERNAL_TEMPERATURE_DISABLED;
8384
external_temperature_is_connected=false;
8485

8586
#ifdef ARDUINO_NANO_RP2040_CONNECT
86-
microphone_rms=0;
87-
rms=0;
87+
microphone_rms=0;
88+
rms=0;
8889
#endif
8990

9091
round_robin_index=0;
9192

9293
#ifdef ARDUINO_NANO_RP2040_CONNECT
93-
thread_activity_led = new rtos::Thread();
94-
thread_update_bme = new rtos::Thread();
95-
thread_external_temperature = new rtos::Thread();
94+
thread_activity_led = new rtos::Thread();
95+
thread_update_bme = new rtos::Thread();
96+
thread_external_temperature = new rtos::Thread();
97+
thread_ultrasonic = new rtos::Thread();
98+
#endif
99+
100+
#ifdef ARDUINO_ESP32
101+
wire_semaphore = xSemaphoreCreateMutex();
96102
#endif
97103

98104
thread_bme_is_running = false;
99105
thread_ext_temperature_is_running = false;
106+
thread_ultrasonic_is_running = false;
100107

101108
activity_led_state = ACTIVITY_LED_OFF;
102109
}
@@ -141,7 +148,6 @@ int ScienceKitCarrier::begin(const uint8_t auxiliary_threads){
141148
return ERR_BEGIN_INA;
142149
}
143150

144-
145151
// let's start resistance measurement
146152
if (beginResistance()!=0){
147153
return ERR_BEGIN_RESISTANCE;
@@ -164,14 +170,7 @@ int ScienceKitCarrier::begin(const uint8_t auxiliary_threads){
164170
}
165171
#endif
166172

167-
// let's start ultrasonic and check if it is connected
168-
/* WIP
169-
if (beginUltrasonic()!=0){
170-
return ERR_BEGIN_ULTRASONIC;
171-
}
172-
*/
173-
174-
// let's start bme688 and external ds18b20 probe
173+
// let's start bme688, external ds18b20 probe and ultrasonic sensor
175174
startAuxiliaryThreads(auxiliary_threads);
176175
}
177176

@@ -194,12 +193,8 @@ void ScienceKitCarrier::update(const bool roundrobin){
194193
updateResistance();
195194
updateIMU();
196195
updateFrequencyGeneratorData();
197-
198-
// update external
199-
//WIP updateUltrasonic();
200196
}
201197
else{
202-
//WIP
203198
switch (round_robin_index){
204199
case 0:
205200
if (thread_ext_temperature_is_running){
@@ -218,7 +213,6 @@ void ScienceKitCarrier::update(const bool roundrobin){
218213
break;
219214
case 3:
220215
updateResistance(); // about 1ms
221-
// WIP updateUltrasonic(); // requires about 5ms when not connected
222216
break;
223217
case 4:
224218
updateIMU(); // heavy task, 13ms
@@ -287,12 +281,14 @@ int ScienceKitCarrier::beginAPDS(){
287281
return 0;
288282
}
289283
void ScienceKitCarrier::updateAPDS(){
284+
wire_lock;
290285
if (apds9960->proximityAvailable()){
291286
proximity=apds9960->readProximity();
292287
}
293288
if (apds9960->colorAvailable()){
294289
apds9960->readColor(r,g,b,c);
295290
}
291+
wire_unlock;
296292
}
297293

298294
int ScienceKitCarrier::getProximity(){
@@ -337,9 +333,11 @@ int ScienceKitCarrier::beginINA(){
337333
}
338334

339335
void ScienceKitCarrier::updateINA(){
336+
wire_lock;
340337
voltage = ina->getBusMilliVolts(0);
341-
voltage = voltage/1000.0;
342338
current = ina->getBusMicroAmps(0);
339+
wire_unlock;
340+
voltage = voltage/1000.0;
343341
current = current/1000000.0;
344342
}
345343

@@ -479,12 +477,7 @@ void ScienceKitCarrier::threadBME688(){
479477
beginBME688();
480478
while(1){
481479
updateBME688();
482-
#ifdef ARDUINO_NANO_RP2040_CONNECT
483-
rtos::ThisThread::sleep_for(1000);
484-
#endif
485-
#ifdef ESP32
486480
delay(1000);
487-
#endif
488481
}
489482
}
490483

@@ -508,7 +501,9 @@ int ScienceKitCarrier::beginIMU(){
508501
}
509502
return 0;
510503
}
504+
511505
void ScienceKitCarrier::updateIMU(){
506+
wire_lock;
512507
if (imu->accelerationAvailable()){
513508
imu->readAcceleration(acceleration[0], acceleration[1], acceleration[2]);
514509
// change scale g -> m/s^2 and rotate the TF according the board symbol
@@ -530,6 +525,7 @@ void ScienceKitCarrier::updateIMU(){
530525
magnetic_field[1] = magnetic_field[1];
531526
magnetic_field[2] = magnetic_field[2];
532527
}
528+
wire_unlock;
533529
}
534530

535531
void ScienceKitCarrier::getAcceleration(float & x, float & y , float & z){
@@ -687,7 +683,9 @@ int ScienceKitCarrier::beginFrequencyGeneratorData(){
687683
}
688684

689685
void ScienceKitCarrier::updateFrequencyGeneratorData(){
686+
wire_lock;
690687
function_generator_controller->updateData();
688+
wire_unlock;
691689
function_generator_controller->getData(frequency1, range1, phase1, frequency2, range2, phase2);
692690
}
693691

@@ -721,20 +719,49 @@ uint8_t ScienceKitCarrier::getRange2(){
721719
/* Ultrasonic Sensor */
722720
/********************************************************************/
723721

724-
int ScienceKitCarrier::beginUltrasonic(){
725-
ultrasonic->begin();
726-
updateUltrasonic();
727-
}
728-
729722
void ScienceKitCarrier::updateUltrasonic(){
730-
if (ultrasonic->checkConnection()){
731-
ultrasonic_is_connected=true;
732-
distance=ultrasonic->getMeters();
733-
travel_time=ultrasonic->getTravelTime();
723+
requestUltrasonicUpdate();
724+
delay(120);
725+
retriveUltrasonicUpdate();
726+
if (ultrasonic_data==4294967295){
727+
ultrasonic_measure = -1.0;
728+
ultrasonic_is_connected = false;
734729
}
735730
else{
736-
ultrasonic_is_connected=false;
731+
ultrasonic_measure = float(ultrasonic_data) / 1000.0;
732+
if (ultrasonic_measure>4500.0){
733+
ultrasonic_measure = 4500.0;
734+
}
735+
ultrasonic_is_connected = true;
737736
}
737+
738+
if (ultrasonic_is_connected){
739+
distance = ultrasonic_measure;
740+
travel_time = ultrasonic_measure*2.0/0.343;
741+
}
742+
else{
743+
distance = -1.0;
744+
travel_time = -1.0;
745+
}
746+
}
747+
748+
void ScienceKitCarrier::requestUltrasonicUpdate(){
749+
wire_lock;
750+
Wire.beginTransmission((uint8_t)ULTRASONIC_ADDRESS);
751+
Wire.write(0x01);
752+
Wire.endTransmission();
753+
wire_unlock;
754+
}
755+
756+
void ScienceKitCarrier::retriveUltrasonicUpdate(){
757+
wire_lock;
758+
Wire.requestFrom((uint8_t)ULTRASONIC_ADDRESS,(uint8_t)3);
759+
ultrasonic_data = Wire.read();
760+
ultrasonic_data <<= 8;
761+
ultrasonic_data |= Wire.read();
762+
ultrasonic_data <<= 8;
763+
ultrasonic_data |= Wire.read();
764+
wire_unlock;
738765
}
739766

740767
float ScienceKitCarrier::getDistance(){
@@ -749,7 +776,18 @@ bool ScienceKitCarrier::getUltrasonicIsConnected(){
749776
return ultrasonic_is_connected;
750777
}
751778

779+
void ScienceKitCarrier::threadUltrasonic(){
780+
while(1){
781+
updateUltrasonic();
782+
delay(200);
783+
}
784+
}
752785

786+
#ifdef ESP32
787+
void ScienceKitCarrier::freeRTOSUltrasonic(void * pvParameters){
788+
((ScienceKitCarrier*) pvParameters)->threadUltrasonic();
789+
}
790+
#endif
753791

754792
/********************************************************************/
755793
/* External Temperature Probe */
@@ -812,12 +850,7 @@ void ScienceKitCarrier::threadExternalTemperature(){
812850
updateExternalTemperature();
813851
updateAnalogInput(UPDATE_INPUT_A);
814852

815-
#ifdef ARDUINO_NANO_RP2040_CONNECT
816-
rtos::ThisThread::sleep_for(1000);
817-
#endif
818-
#ifdef ESP32
819-
delay(1000);
820-
#endif
853+
delay(1000);
821854
}
822855
}
823856

@@ -884,8 +917,9 @@ void ScienceKitCarrier::startAuxiliaryThreads(const uint8_t auxiliary_threads){
884917
xTaskCreatePinnedToCore(this->freeRTOSInternalTemperature, "update_internal_temperature", 10000, this, 1, &thread_internal_temperature, INTERNAL_TEMPERATURE_CORE);
885918
#endif
886919
}
887-
thread_bme_is_running=true;
920+
thread_bme_is_running = true;
888921
}
922+
889923
// start ds18b20 thread
890924
if ((auxiliary_threads==START_AUXILIARY_THREADS)||(auxiliary_threads==START_EXTERNAL_AMBIENT_SENSOR)){
891925
if (!thread_ext_temperature_is_running){
@@ -896,7 +930,20 @@ void ScienceKitCarrier::startAuxiliaryThreads(const uint8_t auxiliary_threads){
896930
xTaskCreatePinnedToCore(this->freeRTOSExternalTemperature, "update_external_temperature", 10000, this, 1, &thread_external_temperature, EXTERNAL_TEMPERATURE_CORE);
897931
#endif
898932
}
899-
thread_ext_temperature_is_running=true;
933+
thread_ext_temperature_is_running = true;
934+
}
935+
936+
// start ultrasonic thread
937+
if ((auxiliary_threads==START_AUXILIARY_THREADS)||(auxiliary_threads==START_ULTRASONIC)){
938+
if (!thread_ultrasonic_is_running){
939+
#ifdef ARDUINO_NANO_RP2040_CONNECT
940+
thread_ultrasonic->start(mbed::callback(this, &ScienceKitCarrier::threadUltrasonic));
941+
#endif
942+
#ifdef ESP32
943+
xTaskCreatePinnedToCore(this->freeRTOSUltrasonic, "update_ultrasonic", 1024, this, 1, &thread_ultrasonic, ULTRASONIC_CORE);
944+
#endif
945+
}
946+
thread_ultrasonic_is_running = true;
900947
}
901948
}
902949

src/Arduino_ScienceKitCarrier.h

+28-5
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,6 @@
3636
#include "bsec2.h"
3737

3838
#include "Arduino_BMI270_BMM150.h"
39-
#include "Arduino_GroveI2C_Ultrasonic.h"
4039

4140
#ifdef ARDUINO_NANO_RP2040_CONNECT
4241
#include "../../OneWireNg/src/platform/OneWireNg_PicoRP2040.h" // forces to use gpio instead PIO hw
@@ -55,6 +54,19 @@
5554
static Placeholder<OneWireNg_CurrentPlatform> ow;
5655

5756

57+
#ifdef ARDUINO_NANO_RP2040_CONNECT
58+
#define wire_lock wire_mutex.lock()
59+
#define wire_unlock wire_mutex.unlock()
60+
#endif
61+
62+
#ifdef ESP32
63+
#define wire_lock while (!xSemaphoreTake(wire_semaphore, 5)){}
64+
#define wire_unlock xSemaphoreGive(wire_semaphore)
65+
//#define wire_lock delay(1)
66+
//#define wire_unlock delay(1)
67+
#endif
68+
69+
5870
class ScienceKitCarrier{
5971
private:
6072
uint8_t round_robin_index;
@@ -84,8 +96,8 @@ class ScienceKitCarrier{
8496
FunctionGeneratorController * function_generator_controller;
8597
uint8_t frequency1, frequency2, phase1, phase2, range1, range2;
8698

87-
Arduino_GroveI2C_Ultrasonic * ultrasonic;
88-
float distance, travel_time;
99+
float ultrasonic_measure,distance, travel_time;
100+
uint32_t ultrasonic_data;
89101
bool ultrasonic_is_connected;
90102

91103
bool external_temperature_is_connected;
@@ -100,24 +112,32 @@ class ScienceKitCarrier{
100112
rtos::Thread * thread_activity_led;
101113
rtos::Thread * thread_update_bme;
102114
rtos::Thread * thread_external_temperature;
115+
rtos::Thread * thread_ultrasonic;
116+
rtos::Mutex wire_mutex;
103117
#endif
104118

105119
#ifdef ESP32
106120
TaskHandle_t thread_internal_temperature;
107121
TaskHandle_t thread_external_temperature;
122+
TaskHandle_t thread_ultrasonic;
123+
SemaphoreHandle_t wire_semaphore;
108124
#endif
109125

110126
bool thread_bme_is_running;
111127
bool thread_ext_temperature_is_running;
128+
bool thread_ultrasonic_is_running;
112129

113130
uint8_t activity_led_state;
114131

132+
void requestUltrasonicUpdate();
133+
void retriveUltrasonicUpdate();
134+
115135
public:
116136
ScienceKitCarrier();
117137

118138
int begin(const uint8_t auxiliary_threads=START_AUXILIARY_THREADS);
119139
void update(const bool roundrobin=false); // this makes update on: analog in, imu, apds, ina, resistance, round robin enables one sensor update
120-
140+
121141
void startAuxiliaryThreads(const uint8_t auxiliary_threads=START_AUXILIARY_THREADS);
122142

123143
#ifdef ARDUINO_NANO_RP2040_CONNECT
@@ -217,11 +237,14 @@ class ScienceKitCarrier{
217237

218238

219239
/* Ultrasonic sensor */
220-
int beginUltrasonic();
221240
void updateUltrasonic();
222241
float getDistance(); // meters
223242
float getTravelTime(); // microseconds
224243
bool getUltrasonicIsConnected();
244+
void threadUltrasonic();
245+
#ifdef ESP32
246+
static void freeRTOSUltrasonic(void * pvParameters);
247+
#endif
225248

226249

227250

0 commit comments

Comments
 (0)