Skip to content

Commit 0ea3eae

Browse files
committed
fix: resistance, function_generator API
1 parent 98df2eb commit 0ea3eae

5 files changed

+60
-20
lines changed

library.properties

+1-1
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,6 @@ sentence=Library and firmware for Arduino Science Kit R3
66
paragraph=This library can be used to flash the Arduino Science Kit R3 and hack it.
77
category=Communication
88
url=https://github.com/arduino-libraries/Arduino_ScienceKitCarrier
9-
architectures=mbed,mbed_nano
9+
architectures=mbed,mbed_nano,esp32
1010
includes=Arduino_ScienceKitCarrier.h
1111
depends=Arduino_APDS9960,ArduinoBLE,WiFiNINA,INA2xx,Arduino_BMI270_BMM150,BSEC Software Library,Arduino_GroveI2C_Ultrasonic,OneWireNg

src/Arduino_ScienceKitCarrier.cpp

+41-15
Original file line numberDiff line numberDiff line change
@@ -111,11 +111,20 @@ ScienceKitCarrier::ScienceKitCarrier(){
111111

112112
int ScienceKitCarrier::begin(const uint8_t auxiliary_threads){
113113
pinMode(LEDR,OUTPUT);
114-
digitalWrite(LEDR,LOW);
115114
pinMode(LEDG,OUTPUT);
116-
digitalWrite(LEDG,LOW);
117115
pinMode(LEDB,OUTPUT);
116+
117+
#ifdef ARDUINO_NANO_RP2040_CONNECT
118+
digitalWrite(LEDR,LOW);
119+
digitalWrite(LEDG,LOW);
118120
digitalWrite(LEDB,LOW);
121+
#endif
122+
123+
#ifdef ESP32
124+
digitalWrite(LEDR,HIGH);
125+
digitalWrite(LEDG,HIGH);
126+
digitalWrite(LEDB,HIGH);
127+
#endif
119128

120129

121130
Wire.begin();
@@ -131,6 +140,7 @@ int ScienceKitCarrier::begin(const uint8_t auxiliary_threads){
131140
if (beginINA()!=0){
132141
return ERR_BEGIN_INA;
133142
}
143+
134144

135145
// let's start resistance measurement
136146
if (beginResistance()!=0){
@@ -154,10 +164,12 @@ int ScienceKitCarrier::begin(const uint8_t auxiliary_threads){
154164
}
155165
#endif
156166

157-
// let's start ultrasonic and check if it is connected
167+
// let's start ultrasonic and check if it is connected
168+
/* WIP
158169
if (beginUltrasonic()!=0){
159170
return ERR_BEGIN_ULTRASONIC;
160171
}
172+
*/
161173

162174
// let's start bme688 and external ds18b20 probe
163175
//WIP startAuxiliaryThreads(auxiliary_threads);
@@ -184,9 +196,10 @@ void ScienceKitCarrier::update(const bool roundrobin){
184196
updateFrequencyGeneratorData();
185197

186198
// update external
187-
updateUltrasonic();
199+
//WIP updateUltrasonic();
188200
}
189201
else{
202+
//WIP
190203
switch (round_robin_index){
191204
case 0:
192205
if (thread_ext_temperature_is_running){
@@ -205,7 +218,7 @@ void ScienceKitCarrier::update(const bool roundrobin){
205218
break;
206219
case 3:
207220
updateResistance(); // about 1ms
208-
updateUltrasonic(); // requires about 5ms when not connected
221+
// WIP updateUltrasonic(); // requires about 5ms when not connected
209222
break;
210223
case 4:
211224
updateIMU(); // heavy task, 13ms
@@ -347,10 +360,10 @@ int ScienceKitCarrier::beginResistance(){
347360
pinMode(resistance_pin,INPUT);
348361
// search for minimum open circuit resistance
349362
for (int i=0; i<20; i++){
350-
resistance=REF_VOLTAGE*analogRead(resistance_pin)/1024.0;
351-
resistance=(RESISTOR_AUX*REF_VOLTAGE/resistance)-RESISTOR_AUX;
363+
resistance = getResistanceMeasureVolts();
364+
resistance = (RESISTOR_AUX*REF_VOLTAGE/resistance)-RESISTOR_AUX;
352365
if (opencircuit_resistance>resistance){
353-
opencircuit_resistance=resistance;
366+
opencircuit_resistance = resistance;
354367
}
355368
delay(5);
356369
}
@@ -360,23 +373,36 @@ int ScienceKitCarrier::beginResistance(){
360373
}
361374

362375
void ScienceKitCarrier::updateResistance(){
363-
resistance=REF_VOLTAGE*analogRead(resistance_pin)/1024.0;
364-
if (resistance<=0){
365-
resistance=-2.0;
376+
resistance = getResistanceMeasureVolts();
377+
if (resistance <= 0){
378+
resistance = -2.0;
366379
}
367380
else{
368-
resistance=(RESISTOR_AUX*REF_VOLTAGE/resistance)-RESISTOR_AUX;
369-
if (resistance<=RESISTANCE_CALIBRATION_LOW){
370-
resistance=0.0;
381+
resistance = (RESISTOR_AUX*REF_VOLTAGE/resistance)-RESISTOR_AUX;
382+
if (resistance <= RESISTANCE_CALIBRATION_LOW){
383+
resistance = 0.0;
371384
}
372385
else{
373386
if (resistance>=opencircuit_resistance){
374-
resistance=-1.0;
387+
resistance = -1.0;
375388
}
376389
}
377390
}
378391
}
379392

393+
float ScienceKitCarrier::getResistanceMeasureVolts(){
394+
float value = 0.0;
395+
#ifdef ARDUINO_NANO_RP2040_CONNECT
396+
value = REF_VOLTAGE*analogRead(resistance_pin)/ADC_RESOLUTION;
397+
#endif
398+
#ifdef ESP32
399+
value = analogReadMilliVolts(resistance_pin)/1000.0;
400+
#endif
401+
return value;
402+
}
403+
404+
405+
380406
float ScienceKitCarrier::getResistance(){
381407
return resistance;
382408
}

src/Arduino_ScienceKitCarrier.h

+1
Original file line numberDiff line numberDiff line change
@@ -161,6 +161,7 @@ class ScienceKitCarrier{
161161
int beginResistance();
162162
void updateResistance();
163163
float getResistance(); // Ohm
164+
float getResistanceMeasureVolts(); // Volt
164165

165166

166167

src/utils/Arduino_ScienceKitCarrier_definitions.h

+15-2
Original file line numberDiff line numberDiff line change
@@ -38,9 +38,20 @@ const uint16_t MAXIMUM_AMPS{1}; // 1A
3838
// Resistance
3939
#define RESISTANCE_PIN A2
4040
#define RESISTOR_AUX 1000.0
41+
#ifdef ARDUINO_NANO_RP2040_CONNECT
4142
#define REF_VOLTAGE 3.3
43+
#endif
44+
#ifdef ESP32
45+
#define REF_VOLTAGE 3.15
46+
#endif
4247
#define RESISTANCE_CALIBRATION_HIGH 200000
4348
#define RESISTANCE_CALIBRATION_LOW 10
49+
#ifdef ARDUINO_NANO_RP2040_CONNECT
50+
#define ADC_RESOLUTION 1023.0
51+
#endif
52+
#ifdef ESP32
53+
#define ADC_RESOLUTION 4095.0
54+
#endif
4455

4556
// Imu
4657
#define G_EARTH 9.807
@@ -53,10 +64,11 @@ const uint16_t MAXIMUM_AMPS{1}; // 1A
5364
#define EXTERNAL_TEMPERATURE_DISABLED -273.15; // absolute zero xD
5465

5566
// Microphone - PDM on Arduino Nano RP2040 Connect
67+
#ifdef ARDUINO_NANO_RP2040_CONNECT
5668
#define MICROPHONE_BUFFER_SIZE 512
5769
#define MICROPHONE_CHANNELS 1
5870
#define MICROPHONE_FREQUENCY 16000
59-
71+
#endif
6072

6173
// Errors
6274
#define ERR_BEGIN_APDS -3
@@ -67,8 +79,9 @@ const uint16_t MAXIMUM_AMPS{1}; // 1A
6779
#define ERR_BEGIN_FUNCTION_GENERATOR_CONTROLLER -8
6880
#define ERR_BEGIN_ULTRASONIC -9
6981
#define ERR_BEGIN_EXTERNAL_TEMPERATURE -10
82+
#ifdef ARDUINO_NANO_RP2040_CONNECT
7083
#define ERR_BEGIN_MICROPHONE -11
71-
84+
#endif
7285

7386

7487

src/utils/function_generator_controller.h

+2-2
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ class FunctionGeneratorController{
4949
wire.beginTransmission(address);
5050
wire.write(FG_VERSION);
5151
wire.endTransmission();
52-
wire.requestFrom(address,2);
52+
wire.requestFrom((uint8_t)address,(uint8_t)2);
5353
wire.readBytes(version,2);
5454
return String(version[0])+"."+String(version[1]);
5555
}
@@ -58,7 +58,7 @@ class FunctionGeneratorController{
5858
wire.beginTransmission(address);
5959
wire.write(FG_DATA);
6060
wire.endTransmission();
61-
wire.requestFrom(address,6);
61+
wire.requestFrom((uint8_t)address,(uint8_t)6);
6262
wire.readBytes(data,6);
6363
}
6464

0 commit comments

Comments
 (0)