Skip to content

Commit ef39524

Browse files
author
jenkie
committed
Fix torque reading
No more skipped interrupts due to cli() sei() commands before analogRead.
1 parent 89e98cd commit ef39524

File tree

1 file changed

+37
-14
lines changed

1 file changed

+37
-14
lines changed

Arduino_Pedelec_Controller/Arduino_Pedelec_Controller.ino

Lines changed: 37 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -213,6 +213,8 @@ double power_human=0.0; //cyclist's power
213213
double wh_human=0;
214214
#ifdef SUPPORT_XCELL_RT
215215
int torque_zero=533; //Offset of X-Cell RT torque sensor. Adjusted at startup
216+
static volatile boolean analogRead_in_use = false; //read torque values in interrupt only if no analogRead in process
217+
static volatile boolean thun_want_calculation = false; //read torque values in interrupt only if no analogRead in process
216218
#if HARDWARE_REV<20
217219
const int torquevalues_count=8;
218220
volatile int torquevalues[torquevalues_count]= {0,0,0,0,0,0,0,0}; //stores the 8 torque values per pedal roundtrip
@@ -221,7 +223,7 @@ const int torquevalues_count=16;
221223
volatile int torquevalues[torquevalues_count]= {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0}; //stores the 16 torque values per pedal roundtrip
222224
#endif
223225
volatile byte torqueindex=0; //index to write next torque value
224-
volatile boolean readtorque=false; //true if pas-interrupt received -> read torque in main loop. unfortunately analogRead gives wrong values inside the PAS-interrupt-routine
226+
volatile boolean readtorque=false; //true if torque array has been updated -> recalculate in main loop
225227
#endif
226228

227229
#if (SERIAL_MODE & SERIAL_MODE_MMC) //communicate with mmc-app
@@ -257,6 +259,7 @@ void save_eeprom();
257259
void save_shutdown();
258260
void handle_unused_pins();
259261
void send_bluetooth_data();
262+
void read_current_torque();
260263
int analogRead_noISR(uint8_t pin);
261264

262265
#ifdef DEBUG_MEMORY_USAGE
@@ -844,14 +847,26 @@ void pas_change_thun(boolean signal)
844847
cad=7500/(millis()-last_pas_event);
845848
last_pas_event = millis();
846849
}
847-
torquevalues[torqueindex]=analogRead_noISR(option_pin)-torque_zero;
850+
if (analogRead_in_use)
851+
{
852+
thun_want_calculation = true;
853+
return;
854+
}
855+
read_current_torque();
856+
}
857+
#endif
858+
#endif
859+
860+
#ifdef SUPPORT_XCELL_RT
861+
void read_current_torque() //this reads the current torque value
862+
{
863+
torquevalues[torqueindex]=analogRead(option_pin)-torque_zero;
848864
torqueindex++;
849865
if (torqueindex==torquevalues_count)
850866
torqueindex=0;
851-
readtorque=true;
867+
readtorque=true;
852868
}
853869
#endif
854-
#endif
855870

856871
#ifdef SUPPORT_PAS
857872
void pas_change() //Are we pedaling? PAS Sensor Change------------------------------------------------------------------------------------------------------------------
@@ -862,11 +877,10 @@ void pas_change() //Are we pedaling? PAS Sensor Change--------------------
862877
{
863878
pas_off_time=millis()-last_pas_event;
864879
#ifdef SUPPORT_XCELL_RT
865-
torquevalues[torqueindex]=analogRead_noISR(option_pin)-torque_zero;
866-
torqueindex++;
867-
if (torqueindex==torquevalues_count)
868-
torqueindex=0;
869-
readtorque=true;
880+
if (analogRead_in_use)
881+
thun_want_calculation = true;
882+
else
883+
read_current_torque();
870884
#endif
871885
}
872886
else
@@ -1243,10 +1257,19 @@ void handle_unused_pins()
12431257
#endif
12441258
}
12451259

1246-
int analogRead_noISR(uint8_t pin) //this function disables globals interrupt before analogRead because analogRead does not like interrupts
1260+
int analogRead_noISR(uint8_t pin) //this function makes sure that analogRead is never used in interrupt. only important for X-Cell RT bottom brackets
12471261
{
1248-
cli();
1249-
int temp=analogRead(pin);
1250-
sei();
1251-
return temp;
1262+
#ifdef SUPPORT_XCELL_RT
1263+
analogRead_in_use = true; //this prevents analogReads in Interrupt from Thun bracket
1264+
if (thun_want_calculation)
1265+
{
1266+
thun_want_calculation=false;
1267+
read_current_torque();
1268+
}
1269+
#endif
1270+
int temp=analogRead(pin);
1271+
#ifdef SUPPORT_XCELL_RT
1272+
analogRead_in_use = false;
1273+
#endif
1274+
return temp;
12521275
}

0 commit comments

Comments
 (0)