From 2d2ce2bcb4af573dc91a759807355980feae6b7d Mon Sep 17 00:00:00 2001 From: benv12 <151891474+benv12@users.noreply.github.com> Date: Tue, 5 Nov 2024 13:54:40 -0600 Subject: [PATCH 1/3] created and set totalTravel --- include/BLE_Custom_Characteristic.h | 1 + include/ERG_Mode.h | 2 ++ include/SmartSpin_parameters.h | 4 ++++ src/BLE_Custom_Characteristic.cpp | 22 ++++++++++++++++++++++ src/Main.cpp | 3 +++ 5 files changed, 32 insertions(+) diff --git a/include/BLE_Custom_Characteristic.h b/include/BLE_Custom_Characteristic.h index ce91a0be..8cacbf1c 100644 --- a/include/BLE_Custom_Characteristic.h +++ b/include/BLE_Custom_Characteristic.h @@ -60,6 +60,7 @@ const uint8_t BLE_simulatedTargetWatts = 0x28; // current target watts const uint8_t BLE_simulateTargetWatts = 0x29; // are we sending target watts const uint8_t BLE_hMin = 0x2A; // Minimum homing value const uint8_t BLE_hMax = 0x2B; // Maximum homing value +const uint8_t BLE_totalTravel = 0x2C; // Total travel class BLE_ss2kCustomCharacteristic { public: diff --git a/include/ERG_Mode.h b/include/ERG_Mode.h index afdba436..4880333a 100644 --- a/include/ERG_Mode.h +++ b/include/ERG_Mode.h @@ -19,6 +19,7 @@ class PowerEntry { public: int watts; + int resistance; int32_t targetPosition; int cad; int readings; @@ -28,6 +29,7 @@ class PowerEntry { this->targetPosition = 0; this->cad = 0; this->readings = 0; + this->resistance = 0; } }; diff --git a/include/SmartSpin_parameters.h b/include/SmartSpin_parameters.h index d4ccd60d..1799c0b2 100644 --- a/include/SmartSpin_parameters.h +++ b/include/SmartSpin_parameters.h @@ -126,6 +126,7 @@ class userParameters { bool udpLogEnabled = false; int32_t hMin = INT32_MIN; int32_t hMax = INT32_MIN; + int32_t totalTravel; bool FTMSControlPointWrite = false; String ssid; @@ -196,6 +197,9 @@ class userParameters { void setShifterDir(bool shd) { shifterDir = shd; } bool getShifterDir() { return shifterDir; } + void setTotalTravel(int tT) { totalTravel = tT; } + bool getTotalTravel() { return totalTravel; } + void setUdpLogEnabled(bool enabled) { udpLogEnabled = enabled; } bool getUdpLogEnabled() { return udpLogEnabled; } diff --git a/src/BLE_Custom_Characteristic.cpp b/src/BLE_Custom_Characteristic.cpp index cce568ea..70baf77d 100644 --- a/src/BLE_Custom_Characteristic.cpp +++ b/src/BLE_Custom_Characteristic.cpp @@ -738,6 +738,22 @@ void BLE_ss2kCustomCharacteristic::process(std::string rxValue) { ss2k->setTargetPosition(int32_t((uint8_t)(rxValue[2]) << 0 | (uint8_t)(rxValue[3]) << 8 | (uint8_t)(rxValue[4]) << 16 | (uint8_t)(rxValue[5]) << 24)); logBufLength += snprintf(logBuf + logBufLength, kLogBufCapacity - logBufLength, " (%f)", userConfig->getHMax()); } + + case BLE_totalTravel: // 0x2C + logBufLength += snprintf(logBuf + logBufLength, kLogBufCapacity - logBufLength, "<-totalTravel"); + if (rxValue[0] == cc_read) { + returnValue[0] = cc_success; + returnValue[2] = (uint8_t)(userConfig->getTotalTravel() & 0xff); + returnValue[3] = (uint8_t)(userConfig->getTotalTravel() >> 8); + returnValue[4] = (uint8_t)(userConfig->getTotalTravel() >> 16); + returnValue[5] = (uint8_t)(userConfig->getTotalTravel() >> 24); + returnLength += 4; + } + if (rxValue[0] == cc_write) { + returnValue[0] = cc_success; + ss2k->setTargetPosition(int32_t((uint8_t)(rxValue[2]) << 0 | (uint8_t)(rxValue[3]) << 8 | (uint8_t)(rxValue[4]) << 16 | (uint8_t)(rxValue[5]) << 24)); + logBufLength += snprintf(logBuf + logBufLength, kLogBufCapacity - logBufLength, " (%f)", userConfig->getTotalTravel()); + } break; } @@ -900,4 +916,10 @@ void BLE_ss2kCustomCharacteristic::parseNemit() { BLE_ss2kCustomCharacteristic::notify(BLE_hMax); return; } + + if(userConfig->getTotalTravel() != _oldParams.getTotalTravel()){ + _oldParams.setTotalTravel(userConfig->getTotalTravel()); + BLE_ss2kCustomCharacteristic::notify(BLE_totalTravel); + return; + } } \ No newline at end of file diff --git a/src/Main.cpp b/src/Main.cpp index 13b2a0ff..dd7675fb 100644 --- a/src/Main.cpp +++ b/src/Main.cpp @@ -584,6 +584,7 @@ void SS2K::goHome(bool bothDirections) { vTaskDelay(2000 / portTICK_PERIOD_MS); rtConfig->setMaxStep(stepper->getCurrentPosition() - 200); SS2K_LOG(MAIN_LOG_TAG, "Max Position found: %d.", rtConfig->getMaxStep()); + rtConfig->setMaxResistance(rtConfig->resistance.getValue()); stepper->enableOutputs(); } stepper->runBackward(); @@ -600,12 +601,14 @@ void SS2K::goHome(bool bothDirections) { stepper->setCurrentPosition((int32_t)0); rtConfig->setMinStep(stepper->getCurrentPosition() + userConfig->getShiftStep()); SS2K_LOG(MAIN_LOG_TAG, "Min Position found: %d.", rtConfig->getMinStep()); + rtConfig->setMinResistance(rtConfig->resistance.getValue()); stepper->enableOutputs(); // Start Saving Settings if (bothDirections) { userConfig->setHMin(rtConfig->getMinStep()); userConfig->setHMax(rtConfig->getMaxStep()); + userConfig->setTotalTravel(userConfig->getHMax() - userConfig->getHMin()); } // In case this was only one direction homing. rtConfig->setMaxStep(userConfig->getHMax()); From b5410d3c3874d40268ef0f59cf5467b3685f93a2 Mon Sep 17 00:00:00 2001 From: benv12 <151891474+benv12@users.noreply.github.com> Date: Fri, 8 Nov 2024 23:01:25 -0600 Subject: [PATCH 2/3] calculated resistance when homed --- include/BLE_Custom_Characteristic.h | 1 + include/SmartSpin_parameters.h | 12 ++++++---- src/BLE_Custom_Characteristic.cpp | 37 +++++++++++++++++++++++------ src/Main.cpp | 5 ++-- src/SensorCollector.cpp | 4 ++++ 5 files changed, 46 insertions(+), 13 deletions(-) diff --git a/include/BLE_Custom_Characteristic.h b/include/BLE_Custom_Characteristic.h index 8cacbf1c..d95df029 100644 --- a/include/BLE_Custom_Characteristic.h +++ b/include/BLE_Custom_Characteristic.h @@ -61,6 +61,7 @@ const uint8_t BLE_simulateTargetWatts = 0x29; // are we sending target watts const uint8_t BLE_hMin = 0x2A; // Minimum homing value const uint8_t BLE_hMax = 0x2B; // Maximum homing value const uint8_t BLE_totalTravel = 0x2C; // Total travel +const uint8_t BLE_calculatedTotalTravel = 0x2D; // Calculated Total Travel class BLE_ss2kCustomCharacteristic { public: diff --git a/include/SmartSpin_parameters.h b/include/SmartSpin_parameters.h index 1799c0b2..bfe26292 100644 --- a/include/SmartSpin_parameters.h +++ b/include/SmartSpin_parameters.h @@ -65,6 +65,8 @@ class RuntimeParameters { int minResistance = -DEFAULT_RESISTANCE_RANGE; int maxResistance = DEFAULT_RESISTANCE_RANGE; bool simTargetWatts = false; + int32_t totalTravel = INT32_MIN; + int16_t calculatedTotalTravel = INT16_MIN; public: Measurement watts; @@ -104,6 +106,12 @@ class RuntimeParameters { void setMaxResistance(int max) { maxResistance = max; } int getMaxResistance() { return maxResistance; } + void setTotalTravel(int32_t tT) { totalTravel = tT; } + int32_t getTotalTravel() { return totalTravel; } + + void setCalculatedTotalTravel(int16_t calctT) { calculatedTotalTravel = calctT; } + int16_t getCalculatedTotalTravel() { return calculatedTotalTravel; } + String returnJSON(); }; @@ -126,7 +134,6 @@ class userParameters { bool udpLogEnabled = false; int32_t hMin = INT32_MIN; int32_t hMax = INT32_MIN; - int32_t totalTravel; bool FTMSControlPointWrite = false; String ssid; @@ -197,9 +204,6 @@ class userParameters { void setShifterDir(bool shd) { shifterDir = shd; } bool getShifterDir() { return shifterDir; } - void setTotalTravel(int tT) { totalTravel = tT; } - bool getTotalTravel() { return totalTravel; } - void setUdpLogEnabled(bool enabled) { udpLogEnabled = enabled; } bool getUdpLogEnabled() { return udpLogEnabled; } diff --git a/src/BLE_Custom_Characteristic.cpp b/src/BLE_Custom_Characteristic.cpp index 70baf77d..8dcab6d6 100644 --- a/src/BLE_Custom_Characteristic.cpp +++ b/src/BLE_Custom_Characteristic.cpp @@ -743,17 +743,34 @@ void BLE_ss2kCustomCharacteristic::process(std::string rxValue) { logBufLength += snprintf(logBuf + logBufLength, kLogBufCapacity - logBufLength, "<-totalTravel"); if (rxValue[0] == cc_read) { returnValue[0] = cc_success; - returnValue[2] = (uint8_t)(userConfig->getTotalTravel() & 0xff); - returnValue[3] = (uint8_t)(userConfig->getTotalTravel() >> 8); - returnValue[4] = (uint8_t)(userConfig->getTotalTravel() >> 16); - returnValue[5] = (uint8_t)(userConfig->getTotalTravel() >> 24); + returnValue[2] = (uint8_t)(rtConfig->getTotalTravel() & 0xff); + returnValue[3] = (uint8_t)(rtConfig->getTotalTravel() >> 8); + returnValue[4] = (uint8_t)(rtConfig->getTotalTravel() >> 16); + returnValue[5] = (uint8_t)(rtConfig->getTotalTravel() >> 24); returnLength += 4; } if (rxValue[0] == cc_write) { returnValue[0] = cc_success; ss2k->setTargetPosition(int32_t((uint8_t)(rxValue[2]) << 0 | (uint8_t)(rxValue[3]) << 8 | (uint8_t)(rxValue[4]) << 16 | (uint8_t)(rxValue[5]) << 24)); - logBufLength += snprintf(logBuf + logBufLength, kLogBufCapacity - logBufLength, " (%f)", userConfig->getTotalTravel()); + logBufLength += snprintf(logBuf + logBufLength, kLogBufCapacity - logBufLength, " (%f)", rtConfig->getTotalTravel()); } + + case BLE_calculatedTotalTravel: // 0x2D + logBufLength += snprintf(logBuf + logBufLength, kLogBufCapacity - logBufLength, "<-calculatedTotalTravel"); + if (rxValue[0] == cc_read) { + returnValue[0] = cc_success; + returnValue[2] = (uint8_t)(rtConfig->getCalculatedTotalTravel() & 0xff); + returnValue[3] = (uint8_t)(rtConfig->getCalculatedTotalTravel() >> 8); + returnValue[4] = (uint8_t)(rtConfig->getCalculatedTotalTravel() >> 16); + returnValue[5] = (uint8_t)(rtConfig->getCalculatedTotalTravel() >> 24); + returnLength += 4; + } + if (rxValue[0] == cc_write) { + returnValue[0] = cc_success; + ss2k->setTargetPosition(int32_t((uint8_t)(rxValue[2]) << 0 | (uint8_t)(rxValue[3]) << 8 | (uint8_t)(rxValue[4]) << 16 | (uint8_t)(rxValue[5]) << 24)); + logBufLength += snprintf(logBuf + logBufLength, kLogBufCapacity - logBufLength, " (%f)", rtConfig->getCalculatedTotalTravel()); + } + break; } @@ -917,9 +934,15 @@ void BLE_ss2kCustomCharacteristic::parseNemit() { return; } - if(userConfig->getTotalTravel() != _oldParams.getTotalTravel()){ - _oldParams.setTotalTravel(userConfig->getTotalTravel()); + if(rtConfig->getTotalTravel() != _oldRTParams.getTotalTravel()){ + _oldRTParams.setTotalTravel(rtConfig->getTotalTravel()); BLE_ss2kCustomCharacteristic::notify(BLE_totalTravel); return; } + + if(rtConfig->getCalculatedTotalTravel() != _oldRTParams.getCalculatedTotalTravel()){ + _oldRTParams.setCalculatedTotalTravel(rtConfig->getCalculatedTotalTravel()); + BLE_ss2kCustomCharacteristic::notify(BLE_calculatedTotalTravel); + return; + } } \ No newline at end of file diff --git a/src/Main.cpp b/src/Main.cpp index dd7675fb..8f75339e 100644 --- a/src/Main.cpp +++ b/src/Main.cpp @@ -601,14 +601,15 @@ void SS2K::goHome(bool bothDirections) { stepper->setCurrentPosition((int32_t)0); rtConfig->setMinStep(stepper->getCurrentPosition() + userConfig->getShiftStep()); SS2K_LOG(MAIN_LOG_TAG, "Min Position found: %d.", rtConfig->getMinStep()); - rtConfig->setMinResistance(rtConfig->resistance.getValue()); + rtConfig->setMinResistance(rtConfig->resistance.getValue()); stepper->enableOutputs(); // Start Saving Settings if (bothDirections) { userConfig->setHMin(rtConfig->getMinStep()); userConfig->setHMax(rtConfig->getMaxStep()); - userConfig->setTotalTravel(userConfig->getHMax() - userConfig->getHMin()); + rtConfig->setTotalTravel(userConfig->getHMax() - userConfig->getHMin()); + rtConfig->setCalculatedTotalTravel(rtConfig->getTotalTravel()/100); } // In case this was only one direction homing. rtConfig->setMaxStep(userConfig->getHMax()); diff --git a/src/SensorCollector.cpp b/src/SensorCollector.cpp index 2452dcf3..25604854 100644 --- a/src/SensorCollector.cpp +++ b/src/SensorCollector.cpp @@ -64,6 +64,10 @@ void collectAndSet(NimBLEUUID charUUID, NimBLEUUID serviceUUID, NimBLEAddress ad if (sensorData->hasResistance()) { if ((rtConfig->getMaxResistance() == MAX_PELOTON_RESISTANCE) && (charUUID != PELOTON_DATA_UUID)) { // Peloton connected but using BLE Power Meter. So skip resistance for UUID's that aren't Peloton. + } else if (rtConfig->getHomed()) { + int resistance = rtConfig->getMaxResistance() *(ss2k->getCurrentPosition()/rtConfig->getCalculatedTotalTravel()); //might be total travel without dividing by 100 + rtConfig->resistance.setValue(resistance); + logBufLength += snprintf(logBuf + logBufLength, kLogBufMaxLength - logBufLength, " RS(%d)", resistance % 1000); } else { int resistance = sensorData->getResistance(); rtConfig->resistance.setValue(resistance); From 5cfb9a3f2b44d62fc2b80556ee8f9833497f6046 Mon Sep 17 00:00:00 2001 From: benv12 <151891474+benv12@users.noreply.github.com> Date: Mon, 11 Nov 2024 18:49:37 -0600 Subject: [PATCH 3/3] fixed target watt bug --- src/BLE_Fitness_Machine_Service.cpp | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/src/BLE_Fitness_Machine_Service.cpp b/src/BLE_Fitness_Machine_Service.cpp index e3c9bac1..5491dcb2 100644 --- a/src/BLE_Fitness_Machine_Service.cpp +++ b/src/BLE_Fitness_Machine_Service.cpp @@ -130,13 +130,13 @@ void BLE_Fitness_Machine_Service::processFTMSWrite() { switch ((uint8_t)rxValue[0]) { case FitnessMachineControlPointProcedure::RequestControl: returnValue[2] = FitnessMachineControlPointResultCode::Success; // 0x01; - rtConfig->watts.setTarget(0); - rtConfig->setSimTargetWatts(false); logBufLength += snprintf(logBuf + logBufLength, kLogBufCapacity - logBufLength, "-> Control Request"); ftmsTrainingStatus[1] = FitnessMachineTrainingStatus::Idle; // 0x01; fitnessMachineTrainingStatus->setValue(ftmsTrainingStatus, 2); ftmsStatus = {FitnessMachineStatus::StartedOrResumedByUser}; pCharacteristic->setValue(returnValue, 3); + rtConfig->watts.setTarget(0); + rtConfig->setSimTargetWatts(false); break; case FitnessMachineControlPointProcedure::Reset: { @@ -147,6 +147,8 @@ void BLE_Fitness_Machine_Service::processFTMSWrite() { ftmsStatus = {FitnessMachineStatus::Reset}; fitnessMachineTrainingStatus->setValue(ftmsTrainingStatus, 2); pCharacteristic->setValue(returnValue, 3); + rtConfig->watts.setTarget(0); + rtConfig->setSimTargetWatts(false); } break; case FitnessMachineControlPointProcedure::SetTargetInclination: { @@ -233,6 +235,8 @@ void BLE_Fitness_Machine_Service::processFTMSWrite() { ftmsStatus = {FitnessMachineStatus::StoppedOrPausedByUser}; ftmsTrainingStatus[1] = FitnessMachineTrainingStatus::Other; // 0x00; fitnessMachineTrainingStatus->setValue(ftmsTrainingStatus, 2); + rtConfig->watts.setTarget(0); + rtConfig->setSimTargetWatts(false); } break;