Skip to content

Commit

Permalink
Merge pull request #35 from zingo/main
Browse files Browse the repository at this point in the history
Make MPU6050 position configurable
  • Loading branch information
lefty01 authored Jan 14, 2023
2 parents 044c9b5 + 94b5759 commit ac6a9f3
Show file tree
Hide file tree
Showing 9 changed files with 52 additions and 41 deletions.
1 change: 1 addition & 0 deletions config/treadmill.txt
Original file line number Diff line number Diff line change
Expand Up @@ -7,5 +7,6 @@ speed_interval_step=0.1
incline_interval_step=1.0
belt_distance=250
hasMPU6050=1
hasMPU6050inAngle=0
hasIrSense=0
hasReed=1
5 changes: 3 additions & 2 deletions config/treadmill_Northtrack12_2_Si.txt
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ min_incline=0.0
speed_interval_step=0.1
incline_interval_step=0.5
belt_distance=153.3
hasMPU6050=0
hasMPU6050=1
hasMPU6050inAngle=1
hasIrSense=0
hasReed=0
hasReed=1
1 change: 1 addition & 0 deletions config/treadmill_SoleF85.txt
Original file line number Diff line number Diff line change
Expand Up @@ -7,5 +7,6 @@ speed_interval_step=0.1
incline_interval_step=1.0
belt_distance=152
hasMPU6050=0
hasMPU6050inAngle=0
hasIrSense=0
hasReed=0
1 change: 1 addition & 0 deletions config/treadmill_Taurus9_5.txt
Original file line number Diff line number Diff line change
Expand Up @@ -7,5 +7,6 @@ speed_interval_step=0.1
incline_interval_step=1.0
belt_distance=250
hasMPU6050=1
hasMPU6050inAngle=0
hasIrSense=0
hasReed=1
1 change: 0 additions & 1 deletion include/common.h
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,6 @@ void initWebSocket();
void notifyClientsWebSockets();
void doReset(void);

double angleSensorTreadmillConversion(double inAngle);
void setSpeedInterval(float interval);
void speedDown();
void speedUp();
Expand Down
1 change: 1 addition & 0 deletions include/config.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ struct TreadmillConfiguration
float speed_interval_step;
long belt_distance; // mm ... actually circumfence of motor wheel!
bool hasMPU6050;
bool hasMPU6050inAngle;
bool hasIrSense;
bool hasReed;
};
Expand Down
36 changes: 1 addition & 35 deletions src/ESP32_TTGO_FTMS.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -170,41 +170,6 @@ void inclineDown()
logText("\n");
}

// angleSensorTreadmillConversion()
// If a treadmill has some special placement of the angle sensor
// here is where that value is converted from sensor read to proper angle of running area.

double angleSensorTreadmillConversion(double inAngle) {
double convertedAngle = inAngle;

#warning TODO make this a configurable depending on where on the threadmill you place the MPU6050
//#if TREADMILL_MODEL == NORDICTRACK_12SI
// TODO: Maybe this should be a config somewhere together with sensor orientation

/* If Sensor is placed in inside the treadmill engine
TODO: Maybe this can be automatic, e.g. Let user selct a incline at a time
and record values and select inbeween bounderies.
If treadmill support stearing the incline maybe it can also be an automatic
calibration step. e.g. move it max down, callibrate sensor, step up max and measure
User input treadmill "Max incline" value and Running are length somehow.
/|--- ___
c / | --- ___ a
/ |x --- ___ Running area
/ |_ --- ___
/ A | | C --- ___
A = inAngle (but we want the angle C)
sin(A)=x/c sin(C)=x/a
x=c*sin(A) x=a*sin(C)
C=asin(c*sin(A)/a)
*/
double c = 32.0; // lenght of motor part in cm
double a = 150.0; // lenght of running area in cm
convertedAngle = asin(c*sin(inAngle * DEG_TO_RAD)/a) * RAD_TO_DEG;
//#endif
return convertedAngle;
}

void setSpeed(float speed)
{
kmph = speed;
Expand Down Expand Up @@ -243,6 +208,7 @@ static void showInfo()
logText(intoText.c_str());
intoText = String("HW: REED: ") + configTreadmill.hasReed +
String(" MPU6050: ") + configTreadmill.hasMPU6050 +
String(" inAngle: ") + configTreadmill.hasMPU6050inAngle +
String(" IrSense: ") + configTreadmill.hasIrSense +
String(" GPIOExtender(AW9523): ") + GPIOExtender.isAvailable() + String("\n");
logText(intoText.c_str());
Expand Down
8 changes: 8 additions & 0 deletions src/config.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
#define default_incline_interval_step 1.0
#define default_belt_distance 250
#define default_hasMPU6050 false
#define default_hasMPU6050inAngle false
#define default_hasIrSense false
#define default_hasReed false

Expand Down Expand Up @@ -55,6 +56,8 @@ void dump_settings(void)
DEBUG_PRINTLN(configTreadmill.belt_distance);
DEBUG_PRINT("hasMPU6050: ");
DEBUG_PRINTLN(configTreadmill.hasMPU6050);
DEBUG_PRINT("hasMPU6050inAngle: ");
DEBUG_PRINTLN(configTreadmill.hasMPU6050inAngle);
DEBUG_PRINT("hasIrSense: ");
DEBUG_PRINTLN(configTreadmill.hasIrSense);
DEBUG_PRINT("hasReed: ");
Expand Down Expand Up @@ -205,6 +208,7 @@ void initConfig(void)
configTreadmill.belt_distance = LittleFS_findFloat(F("belt_distance"));
// Get Your HW config, e.g. interface to Treadmill and other added HW
configTreadmill.hasMPU6050 = LittleFS_findInt(F("hasMPU6050"));
configTreadmill.hasMPU6050inAngle = LittleFS_findInt(F("hasMPU6050inAngle"));
configTreadmill.hasIrSense = LittleFS_findInt(F("hasIrSense"));
configTreadmill.hasReed = LittleFS_findInt(F("hasReed"));

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

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

if ((configTreadmill.hasIrSense < 0) || (configTreadmill.hasIrSense > 1)) {
Expand Down
39 changes: 36 additions & 3 deletions src/hardware.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -340,6 +340,39 @@ static void IRAM_ATTR speedSensor2_ISR(void)
}
}

// angleSensorTreadmillConversion()
// If a treadmill has some special placement of the angle sensor
// here is where that value is converted from sensor read to proper angle of running area.

double angleSensorTreadmillConversion(double inAngle) {
double convertedAngle = inAngle;

if (configTreadmill.hasMPU6050inAngle) {

/* If Sensor is placed in inside the treadmill engine
TODO: Maybe this can be automatic, e.g. Let user selct a incline at a time
and record values and select inbeween bounderies.
If treadmill support stearing the incline maybe it can also be an automatic
calibration step. e.g. move it max down, callibrate sensor, step up max and measure
User input treadmill "Max incline" value and Running are length somehow.
/|--- ___
c / | --- ___ a
/ |x --- ___ Running area
/ |_ --- ___
/ A | | C --- ___
A = inAngle (but we want the angle C)
sin(A)=x/c sin(C)=x/a
x=c*sin(A) x=a*sin(C)
C=asin(c*sin(A)/a)
*/
double c = 32.0; // lenght of motor part in cm TODO treadmill config?
double a = 150.0; // lenght of running area in cm TODO treadmill config?
convertedAngle = asin(c*sin(inAngle * DEG_TO_RAD)/a) * RAD_TO_DEG;
}
return convertedAngle;
}

// getIncline()
// This will read the used "incline" sensor, run this periodically
// The following global variables will be updated
Expand All @@ -353,14 +386,14 @@ float getIncline(void)

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

angle = angleSensorTreadmillConversion(sensorAngle); // convert angle depending on treadmill placment of mpu6050
angle = angleSensorTreadmillConversion(sensorAngle); // convert angle depending placment of mpu6050 on treadmill

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

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

0 comments on commit ac6a9f3

Please sign in to comment.