Skip to content

Commit a04c94d

Browse files
committed
added probe, optimization on analogin is required
1 parent a0d3250 commit a04c94d

File tree

5 files changed

+80
-23
lines changed

5 files changed

+80
-23
lines changed

examples/ScienceJournal/ScienceJournal.ino

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
This file is part of the Arduino_GroveI2C_Ultrasonic library.
2+
This file is part of the Arduino_ScienceKitCarrier library.
33
Copyright (c) 2023 Arduino SA. All rights reserved.
44
55
This library is free software; you can redistribute it and/or
@@ -76,7 +76,7 @@ void setup() {
7676
BLE.addService(service);
7777
BLE.advertise();
7878
science_kit.startAuxiliaryThreads(); // start the BME688 thread
79-
_t.start(loop_data);
79+
//_t.start(loop_data);
8080
_tu.start(update);
8181
}
8282

@@ -112,7 +112,6 @@ void loop() {
112112
updateSubscribedCharacteristics();
113113
lastNotify=millis();
114114
}
115-
116115
}
117116
} else {
118117
delay(100);
@@ -195,6 +194,6 @@ void updateSubscribedCharacteristics() {
195194
}
196195

197196
if (inputBCharacteristic.subscribed()){
198-
inputBCharacteristic.writeValue(science_kit.getInputB());
197+
inputBCharacteristic.writeValue(science_kit.getExternalTemperature());
199198
}
200199
}

examples/ScienceJournal/ble_config.h

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,22 @@
1+
/*
2+
This file is part of the Arduino_ScienceKitCarrier library.
3+
Copyright (c) 2023 Arduino SA. All rights reserved.
4+
5+
This library is free software; you can redistribute it and/or
6+
modify it under the terms of the GNU Lesser General Public
7+
License as published by the Free Software Foundation; either
8+
version 2.1 of the License, or (at your option) any later version.
9+
10+
This library is distributed in the hope that it will be useful,
11+
but WITHOUT ANY WARRANTY; without even the implied warranty of
12+
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13+
Lesser General Public License for more details.
14+
15+
You should have received a copy of the GNU Lesser General Public
16+
License along with this library; if not, write to the Free Software
17+
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
18+
*/
19+
120
#ifndef __BLE_CONFIG_H__
221
#define __BLE_CONFIG_H__
322

src/Arduino_ScienceKitCarrier.cpp

Lines changed: 45 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ ScienceKitCarrier::ScienceKitCarrier(){
2727
inputB_pin = INPUTB_PIN;
2828
inputA=0;
2929
inputB=0;
30+
timer_inputA = 0;
3031

3132
apds9960 = new APDS9960(Wire,INT_APDS9960);
3233
proximity=0;
@@ -73,9 +74,14 @@ ScienceKitCarrier::ScienceKitCarrier(){
7374
travel_time=0.0;
7475
ultrasonic_is_connected=false;
7576

77+
external_temperature=EXTERNAL_TEMPERATURE_DISABLED;
78+
external_temperature_is_connected=false;
79+
80+
round_robin_index = 0;
7681

7782
thread_activity_led = new rtos::Thread();
7883
thread_update_bme = new rtos::Thread();
84+
thread_external_temperature = new rtos::Thread();
7985

8086
activity_led_state = ACTIVITY_LED_OFF;
8187
}
@@ -84,6 +90,10 @@ ScienceKitCarrier::ScienceKitCarrier(){
8490

8591

8692

93+
/********************************************************************/
94+
/* Begin */
95+
/********************************************************************/
96+
8797
int ScienceKitCarrier::begin(const bool auxiliary_threads){
8898
pinMode(LEDR,OUTPUT);
8999
digitalWrite(LEDR,LOW);
@@ -97,7 +107,7 @@ int ScienceKitCarrier::begin(const bool auxiliary_threads){
97107

98108
// most of begin functions return always 0, it is a code-style or future implementation
99109

100-
// let's start apds89960
110+
// let's start apds9960
101111
if (beginAPDS()!=0){
102112
return ERR_BEGIN_APDS;
103113
}
@@ -128,7 +138,7 @@ int ScienceKitCarrier::begin(const bool auxiliary_threads){
128138
}
129139

130140

131-
// let's start activity led and bme688
141+
// let's start bme688 and external ds18b20 probe
132142
if (auxiliary_threads){
133143
startAuxiliaryThreads();
134144
}
@@ -149,27 +159,29 @@ void ScienceKitCarrier::update(const bool roundrobin){
149159
updateINA();
150160
updateResistance();
151161
updateIMU();
162+
updateFrequencyGeneratorData();
152163

153164
// update external
154165
updateUltrasonic();
155166
}
156167
else{
157168
switch (round_robin_index){
158169
case 0:
159-
updateAnalogInput(); // it is very fast (about 2ms)
160-
updateUltrasonic(); // requires about 5ms when not connected
170+
//updateAnalogInput(); // it is very fast (about 1ms)
171+
updateFrequencyGeneratorData(); // less than 1ms
161172
break;
162173
case 1:
163-
updateAPDS();
174+
updateAPDS(); // about 5ms
164175
break;
165176
case 2:
166-
updateINA();
177+
updateINA(); // about 3ms
167178
break;
168179
case 3:
169-
updateResistance();
180+
updateResistance(); // about 1ms
181+
updateUltrasonic(); // requires about 5ms when not connected
170182
break;
171183
case 4:
172-
updateIMU();
184+
updateIMU(); // heavy task, 13ms
173185
break;
174186
default:
175187
break;
@@ -189,13 +201,20 @@ void ScienceKitCarrier::update(const bool roundrobin){
189201
/********************************************************************/
190202

191203
int ScienceKitCarrier::beginAnalogInput(){
204+
/*
192205
pinMode(inputA_pin, INPUT);
193206
pinMode(inputB_pin, INPUT);
207+
*/
194208
return 0;
195209
}
196210

197211
void ScienceKitCarrier::updateAnalogInput(){
198-
inputA=analogRead(inputA_pin);
212+
if (!getExternalTemperatureIsConnected()){
213+
inputA=analogRead(inputA_pin);
214+
}
215+
else{
216+
inputA=ANALOGIN_DISABLED;
217+
}
199218
inputB=analogRead(inputB_pin);
200219
}
201220

@@ -397,7 +416,7 @@ float ScienceKitCarrier::getAirQuality(){
397416

398417
void ScienceKitCarrier::threadBME688(){
399418
beginBME688();
400-
while(true){
419+
while(1){
401420
updateBME688();
402421
rtos::ThisThread::sleep_for(1000);
403422
}
@@ -656,7 +675,9 @@ bool ScienceKitCarrier::getUltrasonicIsConnected(){
656675
/********************************************************************/
657676

658677
int ScienceKitCarrier::beginExternalTemperature(){
659-
678+
new (&ow) OneWireNg_CurrentPlatform(OW_PIN, false);
679+
DSTherm drv(ow);
680+
return 0;
660681
}
661682

662683
void ScienceKitCarrier::updateExternalTemperature(){
@@ -670,8 +691,10 @@ void ScienceKitCarrier::updateExternalTemperature(){
670691
if (ec == OneWireNg::EC_SUCCESS) {
671692
if (scrpd->getAddr()!=15){
672693
external_temperature_is_connected=false;
694+
external_temperature = EXTERNAL_TEMPERATURE_DISABLED;
673695
}
674696
else{
697+
external_temperature_is_connected=true;
675698
long temp = scrpd->getTemp();
676699
int sign=1;
677700
if (temp < 0) {
@@ -680,21 +703,27 @@ void ScienceKitCarrier::updateExternalTemperature(){
680703
}
681704
temperature = (temp/1000)+(temp%1000)*0.001;
682705
external_temperature = sign*temperature;
683-
external_temperature_is_connected=true;
684706
}
685707
}
686708
}
687709

688710
float ScienceKitCarrier::getExternalTemperature(){
689-
711+
return external_temperature;
690712
}
691713

692714
bool ScienceKitCarrier::getExternalTemperatureIsConnected(){
693-
715+
return external_temperature_is_connected;
694716
}
695717

696718
void ScienceKitCarrier::threadExternalTemperature(){
697-
719+
beginExternalTemperature();
720+
while(1){
721+
updateAnalogInput();
722+
digitalWrite(2,HIGH);
723+
updateExternalTemperature();
724+
digitalWrite(2,LOW);
725+
rtos::ThisThread::sleep_for(1000);
726+
}
698727
}
699728

700729

@@ -708,4 +737,5 @@ void ScienceKitCarrier::threadExternalTemperature(){
708737
void ScienceKitCarrier::startAuxiliaryThreads(){
709738
//thread_activity_led->start(mbed::callback(this, &ScienceKitCarrier::threadActivityLed));
710739
thread_update_bme->start(mbed::callback(this, &ScienceKitCarrier::threadBME688));
740+
thread_external_temperature->start(mbed::callback(this, &ScienceKitCarrier::threadExternalTemperature));
711741
}

src/Arduino_ScienceKitCarrier.h

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,14 +39,17 @@
3939
#include "./utils/function_generator_controller.h"
4040
#include "./utils/Arduino_ScienceKitCarrier_definitions.h"
4141

42+
static Placeholder<OneWireNg_CurrentPlatform> ow;
43+
4244

4345

4446
class ScienceKitCarrier{
4547
private:
46-
uint8_t round_robin_index = 0;
48+
uint8_t round_robin_index;
4749

4850
uint8_t inputA_pin, inputB_pin;
4951
int inputA, inputB;
52+
uint8_t timer_inputA;
5053

5154
APDS9960 * apds9960;
5255
int r,g,b,c, proximity;
@@ -77,6 +80,8 @@ class ScienceKitCarrier{
7780
bool external_temperature_is_connected;
7881
float external_temperature;
7982

83+
84+
8085
rtos::Thread * thread_activity_led;
8186
rtos::Thread * thread_update_bme;
8287
rtos::Thread * thread_external_temperature;
@@ -192,7 +197,7 @@ class ScienceKitCarrier{
192197

193198

194199

195-
/* External temperature probe - dallas */
200+
/* External temperature probe - Dallas DS18B20 */
196201
int beginExternalTemperature();
197202
void updateExternalTemperature();
198203
float getExternalTemperature(); // celsius

src/utils/Arduino_ScienceKitCarrier_definitions.h

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
// Grove pads
2424
#define INPUTA_PIN A0
2525
#define INPUTB_PIN A1
26+
#define ANALOGIN_DISABLED 0
2627

2728
// APDS9960
2829
#define INT_APDS9960 9
@@ -44,8 +45,10 @@ const uint16_t MAXIMUM_AMPS{1}; // 1A
4445
// Bme688
4546
#define BME688_CS 10
4647

47-
// External temperature
48-
#define OW_PIN p26
48+
// External temperature connected on input A
49+
#define OW_PIN p26 //digitalPinToPinName(INPUTA_PIN)
50+
#define EXTERNAL_TEMPERATURE_DISABLED -273.0; // absolute zero xD
51+
4952

5053
// Errors
5154
#define ERR_BEGIN_APDS -3
@@ -55,6 +58,7 @@ const uint16_t MAXIMUM_AMPS{1}; // 1A
5558
#define ERR_BEGIN_RESISTANCE -7
5659
#define ERR_BEGIN_FUNCTION_GENERATOR_CONTROLLER -8
5760
#define ERR_BEGIN_ULTRASONIC -9
61+
#define ERR_BEGIN_EXTERNAL_TEMPERATURE -10
5862

5963

6064

0 commit comments

Comments
 (0)