@@ -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}
289283void 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
298294int ScienceKitCarrier::getProximity (){
@@ -337,9 +333,11 @@ int ScienceKitCarrier::beginINA(){
337333}
338334
339335void 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+
511505void 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
535531void ScienceKitCarrier::getAcceleration (float & x, float & y , float & z){
@@ -687,7 +683,9 @@ int ScienceKitCarrier::beginFrequencyGeneratorData(){
687683}
688684
689685void 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-
729722void 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
740767float 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
0 commit comments