Skip to content

Commit 94b5759

Browse files
committed
Make MPU6050 position configurable
On NorthTrack12_2Si the MPU6050 is put in an angualr position compared to the running area. This can be selected by setting hasMPU6050inAngle to 1 This solves the problem with that the code is shared, now the special calculation is only used when needed. Signed-off-by: Zingo Andersen <zingo@zingo.org>
1 parent 044c9b5 commit 94b5759

File tree

9 files changed

+52
-41
lines changed

9 files changed

+52
-41
lines changed

config/treadmill.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,5 +7,6 @@ speed_interval_step=0.1
77
incline_interval_step=1.0
88
belt_distance=250
99
hasMPU6050=1
10+
hasMPU6050inAngle=0
1011
hasIrSense=0
1112
hasReed=1

config/treadmill_Northtrack12_2_Si.txt

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ min_incline=0.0
66
speed_interval_step=0.1
77
incline_interval_step=0.5
88
belt_distance=153.3
9-
hasMPU6050=0
9+
hasMPU6050=1
10+
hasMPU6050inAngle=1
1011
hasIrSense=0
11-
hasReed=0
12+
hasReed=1

config/treadmill_SoleF85.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,5 +7,6 @@ speed_interval_step=0.1
77
incline_interval_step=1.0
88
belt_distance=152
99
hasMPU6050=0
10+
hasMPU6050inAngle=0
1011
hasIrSense=0
1112
hasReed=0

config/treadmill_Taurus9_5.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,5 +7,6 @@ speed_interval_step=0.1
77
incline_interval_step=1.0
88
belt_distance=250
99
hasMPU6050=1
10+
hasMPU6050inAngle=0
1011
hasIrSense=0
1112
hasReed=1

include/common.h

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,6 @@ void initWebSocket();
7878
void notifyClientsWebSockets();
7979
void doReset(void);
8080

81-
double angleSensorTreadmillConversion(double inAngle);
8281
void setSpeedInterval(float interval);
8382
void speedDown();
8483
void speedUp();

include/config.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ struct TreadmillConfiguration
1919
float speed_interval_step;
2020
long belt_distance; // mm ... actually circumfence of motor wheel!
2121
bool hasMPU6050;
22+
bool hasMPU6050inAngle;
2223
bool hasIrSense;
2324
bool hasReed;
2425
};

src/ESP32_TTGO_FTMS.cpp

Lines changed: 1 addition & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -170,41 +170,6 @@ void inclineDown()
170170
logText("\n");
171171
}
172172

173-
// angleSensorTreadmillConversion()
174-
// If a treadmill has some special placement of the angle sensor
175-
// here is where that value is converted from sensor read to proper angle of running area.
176-
177-
double angleSensorTreadmillConversion(double inAngle) {
178-
double convertedAngle = inAngle;
179-
180-
#warning TODO make this a configurable depending on where on the threadmill you place the MPU6050
181-
//#if TREADMILL_MODEL == NORDICTRACK_12SI
182-
// TODO: Maybe this should be a config somewhere together with sensor orientation
183-
184-
/* If Sensor is placed in inside the treadmill engine
185-
TODO: Maybe this can be automatic, e.g. Let user selct a incline at a time
186-
and record values and select inbeween bounderies.
187-
If treadmill support stearing the incline maybe it can also be an automatic
188-
calibration step. e.g. move it max down, callibrate sensor, step up max and measure
189-
User input treadmill "Max incline" value and Running are length somehow.
190-
/|--- ___
191-
c / | --- ___ a
192-
/ |x --- ___ Running area
193-
/ |_ --- ___
194-
/ A | | C --- ___
195-
196-
A = inAngle (but we want the angle C)
197-
sin(A)=x/c sin(C)=x/a
198-
x=c*sin(A) x=a*sin(C)
199-
C=asin(c*sin(A)/a)
200-
*/
201-
double c = 32.0; // lenght of motor part in cm
202-
double a = 150.0; // lenght of running area in cm
203-
convertedAngle = asin(c*sin(inAngle * DEG_TO_RAD)/a) * RAD_TO_DEG;
204-
//#endif
205-
return convertedAngle;
206-
}
207-
208173
void setSpeed(float speed)
209174
{
210175
kmph = speed;
@@ -243,6 +208,7 @@ static void showInfo()
243208
logText(intoText.c_str());
244209
intoText = String("HW: REED: ") + configTreadmill.hasReed +
245210
String(" MPU6050: ") + configTreadmill.hasMPU6050 +
211+
String(" inAngle: ") + configTreadmill.hasMPU6050inAngle +
246212
String(" IrSense: ") + configTreadmill.hasIrSense +
247213
String(" GPIOExtender(AW9523): ") + GPIOExtender.isAvailable() + String("\n");
248214
logText(intoText.c_str());

src/config.cpp

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
#define default_incline_interval_step 1.0
2020
#define default_belt_distance 250
2121
#define default_hasMPU6050 false
22+
#define default_hasMPU6050inAngle false
2223
#define default_hasIrSense false
2324
#define default_hasReed false
2425

@@ -55,6 +56,8 @@ void dump_settings(void)
5556
DEBUG_PRINTLN(configTreadmill.belt_distance);
5657
DEBUG_PRINT("hasMPU6050: ");
5758
DEBUG_PRINTLN(configTreadmill.hasMPU6050);
59+
DEBUG_PRINT("hasMPU6050inAngle: ");
60+
DEBUG_PRINTLN(configTreadmill.hasMPU6050inAngle);
5861
DEBUG_PRINT("hasIrSense: ");
5962
DEBUG_PRINTLN(configTreadmill.hasIrSense);
6063
DEBUG_PRINT("hasReed: ");
@@ -205,6 +208,7 @@ void initConfig(void)
205208
configTreadmill.belt_distance = LittleFS_findFloat(F("belt_distance"));
206209
// Get Your HW config, e.g. interface to Treadmill and other added HW
207210
configTreadmill.hasMPU6050 = LittleFS_findInt(F("hasMPU6050"));
211+
configTreadmill.hasMPU6050inAngle = LittleFS_findInt(F("hasMPU6050inAngle"));
208212
configTreadmill.hasIrSense = LittleFS_findInt(F("hasIrSense"));
209213
configTreadmill.hasReed = LittleFS_findInt(F("hasReed"));
210214

@@ -252,7 +256,11 @@ void initConfig(void)
252256
if ((configTreadmill.hasMPU6050 < 0) || (configTreadmill.hasMPU6050 > 1)) {
253257
configTreadmill.hasMPU6050 = default_hasMPU6050;
254258
logText("invalid MPU6050 setting, using default (false)\n");
259+
}
255260

261+
if ((configTreadmill.hasMPU6050inAngle < 0) || (configTreadmill.hasMPU6050inAngle > 1)) {
262+
configTreadmill.hasMPU6050inAngle = default_hasMPU6050inAngle;
263+
logText("invalid hasMPU6050inAngle setting, using default (false)\n");
256264
}
257265

258266
if ((configTreadmill.hasIrSense < 0) || (configTreadmill.hasIrSense > 1)) {

src/hardware.cpp

Lines changed: 36 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -340,6 +340,39 @@ static void IRAM_ATTR speedSensor2_ISR(void)
340340
}
341341
}
342342

343+
// angleSensorTreadmillConversion()
344+
// If a treadmill has some special placement of the angle sensor
345+
// here is where that value is converted from sensor read to proper angle of running area.
346+
347+
double angleSensorTreadmillConversion(double inAngle) {
348+
double convertedAngle = inAngle;
349+
350+
if (configTreadmill.hasMPU6050inAngle) {
351+
352+
/* If Sensor is placed in inside the treadmill engine
353+
TODO: Maybe this can be automatic, e.g. Let user selct a incline at a time
354+
and record values and select inbeween bounderies.
355+
If treadmill support stearing the incline maybe it can also be an automatic
356+
calibration step. e.g. move it max down, callibrate sensor, step up max and measure
357+
User input treadmill "Max incline" value and Running are length somehow.
358+
/|--- ___
359+
c / | --- ___ a
360+
/ |x --- ___ Running area
361+
/ |_ --- ___
362+
/ A | | C --- ___
363+
364+
A = inAngle (but we want the angle C)
365+
sin(A)=x/c sin(C)=x/a
366+
x=c*sin(A) x=a*sin(C)
367+
C=asin(c*sin(A)/a)
368+
*/
369+
double c = 32.0; // lenght of motor part in cm TODO treadmill config?
370+
double a = 150.0; // lenght of running area in cm TODO treadmill config?
371+
convertedAngle = asin(c*sin(inAngle * DEG_TO_RAD)/a) * RAD_TO_DEG;
372+
}
373+
return convertedAngle;
374+
}
375+
343376
// getIncline()
344377
// This will read the used "incline" sensor, run this periodically
345378
// The following global variables will be updated
@@ -353,14 +386,14 @@ float getIncline(void)
353386

354387
if (configTreadmill.hasMPU6050) {
355388
// MPU6050 returns a incline/grade in degrees(!)
356-
// TODO: configure sensor orientation via webinterface
357389
// FIXME: maybe get some rolling-average of Y-angle to smooth things a bit (same for speed)
358390
// mpu.getAngle[XYZ]
359391
sensorAngle = mpu6050.getAngleY(); //This does not use I2C no need to I2C begin/end
360392

361-
angle = angleSensorTreadmillConversion(sensorAngle); // convert angle depending on treadmill placment of mpu6050
393+
angle = angleSensorTreadmillConversion(sensorAngle); // convert angle depending placment of mpu6050 on treadmill
362394

363-
if (angle < 0) angle = 0; // TODO We might allow running downhill
395+
if (angle < 0) angle = 0; // TODO We might allow running downhill, some threadmill support it and pratically
396+
// you could put something under it on the back side
364397

365398
char yStr[5];
366399
snprintf(yStr, 5, "%.2f", angle);

0 commit comments

Comments
 (0)