Skip to content

Commit

Permalink
calculated resistance when homed
Browse files Browse the repository at this point in the history
  • Loading branch information
benv12 committed Nov 9, 2024
1 parent 2d2ce2b commit b5410d3
Show file tree
Hide file tree
Showing 5 changed files with 46 additions and 13 deletions.
1 change: 1 addition & 0 deletions include/BLE_Custom_Characteristic.h
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
12 changes: 8 additions & 4 deletions include/SmartSpin_parameters.h
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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();
};

Expand All @@ -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;
Expand Down Expand Up @@ -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; }

Expand Down
37 changes: 30 additions & 7 deletions src/BLE_Custom_Characteristic.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

Expand Down Expand Up @@ -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;
}
}
5 changes: 3 additions & 2 deletions src/Main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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());
Expand Down
4 changes: 4 additions & 0 deletions src/SensorCollector.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down

0 comments on commit b5410d3

Please sign in to comment.