Skip to content

Commit 1b2ed46

Browse files
committed
ultrasonic in the library
1 parent 265460f commit 1b2ed46

File tree

5 files changed

+93
-3
lines changed

5 files changed

+93
-3
lines changed

.gitignore

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
# General
2+
.DS_Store
3+
.AppleDouble
4+
.LSOverride
5+
6+
# Icon must end with two \r
7+
Icon
8+
9+
# Thumbnails
10+
._*
11+
12+
# Files that might appear in the root of a volume
13+
.DocumentRevisions-V100
14+
.fseventsd
15+
.Spotlight-V100
16+
.TemporaryItems
17+
.Trashes
18+
.VolumeIcon.icns
19+
.com.apple.timemachine.donotpresent
20+
21+
# Directories potentially created on remote AFP share
22+
.AppleDB
23+
.AppleDesktop
24+
Network Trash Folder
25+
Temporary Items
26+
.apdisk

library.properties

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,4 +8,4 @@ category=Communication
88
url=https://github.com/gbr1/Arduino_ScienceKitCarrier
99
architectures=mbed,mbed_nano
1010
includes=Arduino_ScienceKitCarrier.h
11-
depends=Arduino_APDS9960,ArduinoBLE,WiFiNINA,INA2xx,Arduino_BMI270_BMM150,BSEC Software Library
11+
depends=Arduino_APDS9960,ArduinoBLE,WiFiNINA,INA2xx,Arduino_BMI270_BMM150,BSEC Software Library,Arduino_GroveI2C_Ultrasonic

src/Arduino_ScienceKitCarrier.cpp

Lines changed: 50 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,11 @@ ScienceKitCarrier::ScienceKitCarrier(){
6868
range1=0;
6969
range2=0;
7070

71+
ultrasonic = new Arduino_GroveI2C_Ultrasonic();
72+
distance=0.0;
73+
travel_time=0.0;
74+
ultrasonic_is_connected=false;
75+
7176

7277
thread_activity_led = new rtos::Thread();
7378
thread_update_bme = new rtos::Thread();
@@ -90,6 +95,8 @@ int ScienceKitCarrier::begin(const bool auxiliary_threads){
9095

9196
Wire.begin();
9297

98+
// most of begin functions return always 0, it is a code-style or future implementation
99+
93100
// let's start apds89960
94101
if (beginAPDS()!=0){
95102
return ERR_BEGIN_APDS;
@@ -100,7 +107,7 @@ int ScienceKitCarrier::begin(const bool auxiliary_threads){
100107
return ERR_BEGIN_INA;
101108
}
102109

103-
// resistance pin
110+
// let's start resistance measurement
104111
if (beginResistance()!=0){
105112
return ERR_BEGIN_RESISTANCE;
106113
}
@@ -115,6 +122,12 @@ int ScienceKitCarrier::begin(const bool auxiliary_threads){
115122
return ERR_BEGIN_FUNCTION_GENERATOR_CONTROLLER;
116123
}
117124

125+
// let's start ultrasonic and check if it is connected
126+
if (beginUltrasonic()!=0){
127+
return ERR_BEGIN_ULTRASONIC;
128+
}
129+
130+
118131
// let's start activity led and bme688
119132
if (auxiliary_threads){
120133
startAuxiliaryThreads();
@@ -136,11 +149,15 @@ void ScienceKitCarrier::update(const bool roundrobin){
136149
updateINA();
137150
updateResistance();
138151
updateIMU();
152+
153+
// update external
154+
updateUltrasonic();
139155
}
140156
else{
141157
switch (round_robin_index){
142158
case 0:
143-
updateAnalogInput();
159+
updateAnalogInput(); // it is very fast (about 2ms)
160+
updateUltrasonic(); // requires about 5ms when not connected
144161
break;
145162
case 1:
146163
updateAPDS();
@@ -602,6 +619,37 @@ uint8_t ScienceKitCarrier::getRange2(){
602619
return range2;
603620
}
604621

622+
/********************************************************************/
623+
/* Ultrasonic Sensor */
624+
/********************************************************************/
625+
626+
int ScienceKitCarrier::beginUltrasonic(){
627+
ultrasonic->begin();
628+
updateUltrasonic();
629+
}
630+
631+
void ScienceKitCarrier::updateUltrasonic(){
632+
if (ultrasonic->checkConnection()){
633+
ultrasonic_is_connected=true;
634+
distance=ultrasonic->getMeters();
635+
travel_time=ultrasonic->getTravelTime();
636+
}
637+
else{
638+
ultrasonic_is_connected=false;
639+
}
640+
}
641+
642+
float ScienceKitCarrier::getDistance(){
643+
return distance;
644+
}
645+
646+
float ScienceKitCarrier::getTravelTime(){
647+
return travel_time;
648+
}
649+
650+
bool ScienceKitCarrier::getUltrasonicIsConnected(){
651+
return ultrasonic_is_connected;
652+
}
605653

606654

607655

src/Arduino_ScienceKitCarrier.h

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
#include "INA.h"
3030
#include "bsec.h"
3131
#include "Arduino_BMI270_BMM150.h"
32+
#include "Arduino_GroveI2C_Ultrasonic.h"
3233

3334
#include "./utils/function_generator_controller.h"
3435
#include "./utils/Arduino_ScienceKitCarrier_definitions.h"
@@ -63,6 +64,10 @@ class ScienceKitCarrier{
6364
FunctionGeneratorController * function_generator_controller;
6465
uint8_t frequency1, frequency2, phase1, phase2, range1, range2;
6566

67+
Arduino_GroveI2C_Ultrasonic * ultrasonic;
68+
float distance, travel_time;
69+
bool ultrasonic_is_connected;
70+
6671

6772
rtos::Thread * thread_activity_led;
6873
rtos::Thread * thread_update_bme;
@@ -166,6 +171,16 @@ class ScienceKitCarrier{
166171
uint8_t getPhase2();
167172
uint8_t getRange1();
168173
uint8_t getRange2();
174+
175+
176+
177+
/* Ultrasonic sensor */
178+
int beginUltrasonic();
179+
void updateUltrasonic();
180+
float getDistance(); // meters
181+
float getTravelTime(); // microseconds
182+
bool getUltrasonicIsConnected();
183+
169184
};
170185

171186

src/utils/Arduino_ScienceKitCarrier_definitions.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@ const uint16_t MAXIMUM_AMPS{1}; // 1A
5151
#define ERR_BEGIN_BME -6
5252
#define ERR_BEGIN_RESISTANCE -7
5353
#define ERR_BEGIN_FUNCTION_GENERATOR_CONTROLLER -8
54+
#define ERR_BEGIN_ULTRASONIC -9
5455

5556

5657

0 commit comments

Comments
 (0)