@@ -74,29 +74,36 @@ ScienceKitCarrier::ScienceKitCarrier(){
74
74
range1=0 ;
75
75
range2=0 ;
76
76
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 ;
80
81
ultrasonic_is_connected=false ;
81
82
82
83
external_temperature=EXTERNAL_TEMPERATURE_DISABLED;
83
84
external_temperature_is_connected=false ;
84
85
85
86
#ifdef ARDUINO_NANO_RP2040_CONNECT
86
- microphone_rms=0 ;
87
- rms=0 ;
87
+ microphone_rms=0 ;
88
+ rms=0 ;
88
89
#endif
89
90
90
91
round_robin_index=0 ;
91
92
92
93
#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 ();
96
102
#endif
97
103
98
104
thread_bme_is_running = false ;
99
105
thread_ext_temperature_is_running = false ;
106
+ thread_ultrasonic_is_running = false ;
100
107
101
108
activity_led_state = ACTIVITY_LED_OFF;
102
109
}
@@ -141,7 +148,6 @@ int ScienceKitCarrier::begin(const uint8_t auxiliary_threads){
141
148
return ERR_BEGIN_INA;
142
149
}
143
150
144
-
145
151
// let's start resistance measurement
146
152
if (beginResistance ()!=0 ){
147
153
return ERR_BEGIN_RESISTANCE;
@@ -164,14 +170,7 @@ int ScienceKitCarrier::begin(const uint8_t auxiliary_threads){
164
170
}
165
171
#endif
166
172
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
175
174
startAuxiliaryThreads (auxiliary_threads);
176
175
}
177
176
@@ -194,12 +193,8 @@ void ScienceKitCarrier::update(const bool roundrobin){
194
193
updateResistance ();
195
194
updateIMU ();
196
195
updateFrequencyGeneratorData ();
197
-
198
- // update external
199
- // WIP updateUltrasonic();
200
196
}
201
197
else {
202
- // WIP
203
198
switch (round_robin_index){
204
199
case 0 :
205
200
if (thread_ext_temperature_is_running){
@@ -218,7 +213,6 @@ void ScienceKitCarrier::update(const bool roundrobin){
218
213
break ;
219
214
case 3 :
220
215
updateResistance (); // about 1ms
221
- // WIP updateUltrasonic(); // requires about 5ms when not connected
222
216
break ;
223
217
case 4 :
224
218
updateIMU (); // heavy task, 13ms
@@ -287,12 +281,14 @@ int ScienceKitCarrier::beginAPDS(){
287
281
return 0 ;
288
282
}
289
283
void ScienceKitCarrier::updateAPDS (){
284
+ wire_lock;
290
285
if (apds9960->proximityAvailable ()){
291
286
proximity=apds9960->readProximity ();
292
287
}
293
288
if (apds9960->colorAvailable ()){
294
289
apds9960->readColor (r,g,b,c);
295
290
}
291
+ wire_unlock;
296
292
}
297
293
298
294
int ScienceKitCarrier::getProximity (){
@@ -337,9 +333,11 @@ int ScienceKitCarrier::beginINA(){
337
333
}
338
334
339
335
void ScienceKitCarrier::updateINA (){
336
+ wire_lock;
340
337
voltage = ina->getBusMilliVolts (0 );
341
- voltage = voltage/1000.0 ;
342
338
current = ina->getBusMicroAmps (0 );
339
+ wire_unlock;
340
+ voltage = voltage/1000.0 ;
343
341
current = current/1000000.0 ;
344
342
}
345
343
@@ -479,12 +477,7 @@ void ScienceKitCarrier::threadBME688(){
479
477
beginBME688 ();
480
478
while (1 ){
481
479
updateBME688 ();
482
- #ifdef ARDUINO_NANO_RP2040_CONNECT
483
- rtos::ThisThread::sleep_for (1000 );
484
- #endif
485
- #ifdef ESP32
486
480
delay (1000 );
487
- #endif
488
481
}
489
482
}
490
483
@@ -508,7 +501,9 @@ int ScienceKitCarrier::beginIMU(){
508
501
}
509
502
return 0 ;
510
503
}
504
+
511
505
void ScienceKitCarrier::updateIMU (){
506
+ wire_lock;
512
507
if (imu->accelerationAvailable ()){
513
508
imu->readAcceleration (acceleration[0 ], acceleration[1 ], acceleration[2 ]);
514
509
// change scale g -> m/s^2 and rotate the TF according the board symbol
@@ -530,6 +525,7 @@ void ScienceKitCarrier::updateIMU(){
530
525
magnetic_field[1 ] = magnetic_field[1 ];
531
526
magnetic_field[2 ] = magnetic_field[2 ];
532
527
}
528
+ wire_unlock;
533
529
}
534
530
535
531
void ScienceKitCarrier::getAcceleration (float & x, float & y , float & z){
@@ -687,7 +683,9 @@ int ScienceKitCarrier::beginFrequencyGeneratorData(){
687
683
}
688
684
689
685
void ScienceKitCarrier::updateFrequencyGeneratorData (){
686
+ wire_lock;
690
687
function_generator_controller->updateData ();
688
+ wire_unlock;
691
689
function_generator_controller->getData (frequency1, range1, phase1, frequency2, range2, phase2);
692
690
}
693
691
@@ -721,20 +719,49 @@ uint8_t ScienceKitCarrier::getRange2(){
721
719
/* Ultrasonic Sensor */
722
720
/* *******************************************************************/
723
721
724
- int ScienceKitCarrier::beginUltrasonic (){
725
- ultrasonic->begin ();
726
- updateUltrasonic ();
727
- }
728
-
729
722
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 ;
734
729
}
735
730
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 ;
737
736
}
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;
738
765
}
739
766
740
767
float ScienceKitCarrier::getDistance (){
@@ -749,7 +776,18 @@ bool ScienceKitCarrier::getUltrasonicIsConnected(){
749
776
return ultrasonic_is_connected;
750
777
}
751
778
779
+ void ScienceKitCarrier::threadUltrasonic (){
780
+ while (1 ){
781
+ updateUltrasonic ();
782
+ delay (200 );
783
+ }
784
+ }
752
785
786
+ #ifdef ESP32
787
+ void ScienceKitCarrier::freeRTOSUltrasonic (void * pvParameters){
788
+ ((ScienceKitCarrier*) pvParameters)->threadUltrasonic ();
789
+ }
790
+ #endif
753
791
754
792
/* *******************************************************************/
755
793
/* External Temperature Probe */
@@ -812,12 +850,7 @@ void ScienceKitCarrier::threadExternalTemperature(){
812
850
updateExternalTemperature ();
813
851
updateAnalogInput (UPDATE_INPUT_A);
814
852
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 );
821
854
}
822
855
}
823
856
@@ -884,8 +917,9 @@ void ScienceKitCarrier::startAuxiliaryThreads(const uint8_t auxiliary_threads){
884
917
xTaskCreatePinnedToCore (this ->freeRTOSInternalTemperature , " update_internal_temperature" , 10000 , this , 1 , &thread_internal_temperature, INTERNAL_TEMPERATURE_CORE);
885
918
#endif
886
919
}
887
- thread_bme_is_running= true ;
920
+ thread_bme_is_running = true ;
888
921
}
922
+
889
923
// start ds18b20 thread
890
924
if ((auxiliary_threads==START_AUXILIARY_THREADS)||(auxiliary_threads==START_EXTERNAL_AMBIENT_SENSOR)){
891
925
if (!thread_ext_temperature_is_running){
@@ -896,7 +930,20 @@ void ScienceKitCarrier::startAuxiliaryThreads(const uint8_t auxiliary_threads){
896
930
xTaskCreatePinnedToCore (this ->freeRTOSExternalTemperature , " update_external_temperature" , 10000 , this , 1 , &thread_external_temperature, EXTERNAL_TEMPERATURE_CORE);
897
931
#endif
898
932
}
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 ;
900
947
}
901
948
}
902
949
0 commit comments