Skip to content

Commit

Permalink
Merge pull request #582 from doudar/targetPosition
Browse files Browse the repository at this point in the history
Cleaned up targetPosition to make it easier to understand.
  • Loading branch information
doudar authored Nov 2, 2024
2 parents 3bb2479 + 205a3ff commit bf69931
Show file tree
Hide file tree
Showing 10 changed files with 101 additions and 34 deletions.
63 changes: 63 additions & 0 deletions .github/workflows/update-changelog.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
name: Update Changelog

on:
pull_request:
branches:
- develop
types: [opened, synchronize, reopened]

jobs:
update-changelog:
runs-on: ubuntu-latest
permissions:
contents: write
pull-requests: write

steps:
- uses: actions/checkout@v3
with:
fetch-depth: 0
ref: ${{ github.event.pull_request.head.ref }}

- name: Set up Python
uses: actions/setup-python@v4
with:
python-version: '3.x'

- name: Update Changelog
run: |
LATEST_TAG=$(git describe --tags --abbrev=0 2>/dev/null || echo "0.0.0")
echo "Latest tag: $LATEST_TAG"
if ! grep -q "## \[Unreleased\]" CHANGELOG.md; then
echo "No [Unreleased] section found"
exit 0
fi
FIRST_VERSION=$(grep -oP "## \[\K[0-9]+\.[0-9]+\.[0-9]+" CHANGELOG.md | head -1 || echo "0.0.0")
if [ "$LATEST_TAG" = "$FIRST_VERSION" ]; then
echo "Latest tag matches first version in changelog"
exit 0
fi
# Create temporary files
echo -e "# Changelog\n\nAll notable changes to this project will be documented in this file.\n\nThe format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/)\nand this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).\n\n## [Unreleased]\n\n### Added\n\n### Changed\n\n### Hardware\n" > header.tmp
# Update the old unreleased section
sed "0,/## \[Unreleased\]/s/## \[Unreleased\]/## [$LATEST_TAG]/" CHANGELOG.md | tail -n +6 > content.tmp
# Combine files
cat header.tmp content.tmp > CHANGELOG.md
rm header.tmp content.tmp
if git diff --quiet CHANGELOG.md; then
echo "No changes to commit"
exit 0
fi
git config --local user.email "[email protected]"
git config --local user.name "GitHub Action"
git add CHANGELOG.md
git commit -m "Update changelog for version $LATEST_TAG"
git push
4 changes: 3 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
- Added CSC Service to BLE server.
- Added Yosuda-007C.
- Updated wiki banner.
- Added automatic update of Changelog sections on pull request to develop.
- Added support for the Zwift gear display.

### Changed
Expand Down Expand Up @@ -56,7 +57,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
- Added a final test to check if ERG mode has commanded a move in the proper direction.
- Aligned the values between the config app and web interface.
- Added ability to send target watts through the custom characteristic.
- Added a final test to check if ERG mode has commanded a move in the proper direction.
- Added a final test to check if ERG mode has commanded a move in the proper direction.
- Cleaned up targetPosition to make it easier to understand.

### Hardware

Expand Down
13 changes: 11 additions & 2 deletions include/Main.h
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,10 @@ class SS2K {
int shiftersHoldForScan;
uint64_t scanDelayTime;
uint64_t scanDelayStart;

public:
int32_t targetPosition;
int32_t currentPosition;

public:
bool stepperIsRunning;
bool externalControl;
bool syncMode;
Expand All @@ -47,6 +47,15 @@ class SS2K {
static void IRAM_ATTR shiftUp();
static void IRAM_ATTR shiftDown();
static void moveStepper();

//the position the stepper motor will move to
int32_t getTargetPosition() { return targetPosition; }
void setTargetPosition(int32_t tp) { targetPosition = tp; }

//the position the stepper motor is currently at
int32_t getCurrentPosition() { return currentPosition; }
void setCurrentPosition(int32_t cp) { currentPosition = cp; }

void resetIfShiftersHeld();
void startTasks();
void stopTasks();
Expand Down
4 changes: 0 additions & 4 deletions include/SmartSpin_parameters.h
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,6 @@ class Measurement {
class RuntimeParameters {
private:
double targetIncline = 0.0;
double currentIncline = 0.0;
float simulatedSpeed = 0.0;
uint8_t FTMSMode = 0x00;
int shifterPosition = 0;
Expand All @@ -77,9 +76,6 @@ class RuntimeParameters {
void setTargetIncline(float inc) { targetIncline = inc; }
float getTargetIncline() { return targetIncline; }

void setCurrentIncline(float inc) { currentIncline = inc; }
float getCurrentIncline() { return currentIncline; }

void setSimulatedSpeed(float spd) { simulatedSpeed = spd; }
float getSimulatedSpeed() { return simulatedSpeed; }

Expand Down
2 changes: 1 addition & 1 deletion src/BLE_Client.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -595,7 +595,7 @@ void SpinBLEClient::FTMSControlPointWrite(const uint8_t *pData, int length) {
const int kLogBufCapacity = length + 40;
char logBuf[kLogBufCapacity];
if (modData[0] == FitnessMachineControlPointProcedure::SetIndoorBikeSimulationParameters) { // use virtual Shifting
int incline = ss2k->targetPosition / userConfig->getInclineMultiplier();
int incline = ss2k->getTargetPosition() / userConfig->getInclineMultiplier();
modData[3] = (uint8_t)(incline & 0xff);
modData[4] = (uint8_t)(incline >> 8);
writeCharacteristic->writeValue(modData, length);
Expand Down
12 changes: 6 additions & 6 deletions src/BLE_Custom_Characteristic.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -491,16 +491,16 @@ void BLE_ss2kCustomCharacteristic::process(std::string rxValue) {
logBufLength += snprintf(logBuf + logBufLength, kLogBufCapacity - logBufLength, "<-targetPosition");
if (rxValue[0] == cc_read) {
returnValue[0] = cc_success;
returnValue[2] = (uint8_t)(ss2k->targetPosition & 0xff);
returnValue[3] = (uint8_t)(ss2k->targetPosition >> 8);
returnValue[4] = (uint8_t)(ss2k->targetPosition >> 16);
returnValue[5] = (uint8_t)(ss2k->targetPosition >> 24);
returnValue[2] = (uint8_t)(ss2k->getTargetPosition() & 0xff);
returnValue[3] = (uint8_t)(ss2k->getTargetPosition() >> 8);
returnValue[4] = (uint8_t)(ss2k->getTargetPosition() >> 16);
returnValue[5] = (uint8_t)(ss2k->getTargetPosition() >> 24);
returnLength += 4;
}
if (rxValue[0] == cc_write) {
returnValue[0] = cc_success;
ss2k->targetPosition = (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)", ss2k->targetPosition);
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)", ss2k->getTargetPosition());
}
break;

Expand Down
32 changes: 16 additions & 16 deletions src/ERG_Mode.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ void PowerBuffer::set(int i) {
this->powerEntry[i].readings++;
this->powerEntry[i].watts = rtConfig->watts.getValue();
this->powerEntry[i].cad = rtConfig->cad.getValue();
this->powerEntry[i].targetPosition = rtConfig->getCurrentIncline() / 100; // dividing by 100 to save memory.
this->powerEntry[i].targetPosition = ss2k->getCurrentPosition() / 100; // dividing by 100 to save memory.
}

void PowerBuffer::reset() {
Expand Down Expand Up @@ -144,13 +144,13 @@ void PowerTable::setStepperMinMax() {
_return = this->lookup(minBreakWatts, NORMAL_CAD);
if (_return != RETURN_ERROR) {
// never set less than one shift below current incline.
if ((_return >= rtConfig->getCurrentIncline()) && (rtConfig->watts.getValue() > userConfig->getMinWatts())) {
_return = rtConfig->getCurrentIncline() - userConfig->getShiftStep();
if ((_return >= ss2k->getCurrentPosition()) && (rtConfig->watts.getValue() > userConfig->getMinWatts())) {
_return = ss2k->getCurrentPosition() - userConfig->getShiftStep();
SS2K_LOG(ERG_MODE_LOG_TAG, "Min Position too close to current incline: %d", _return);
}
// never set above max step.
if (_return >= rtConfig->getMaxStep()) {
_return = rtConfig->getCurrentIncline() - userConfig->getShiftStep() * 2;
_return = ss2k->getCurrentPosition() - userConfig->getShiftStep() * 2;
SS2K_LOG(ERG_MODE_LOG_TAG, "Min Position above max!: %d", _return);
}
rtConfig->setMinStep(_return);
Expand All @@ -163,13 +163,13 @@ void PowerTable::setStepperMinMax() {
_return = this->lookup(maxBreakWatts, NORMAL_CAD);
if (_return != RETURN_ERROR) {
// never set less than one shift above current incline.
if ((_return <= rtConfig->getCurrentIncline()) && (rtConfig->watts.getValue() < userConfig->getMaxWatts())) {
_return = rtConfig->getCurrentIncline() + userConfig->getShiftStep();
if ((_return <= ss2k->getCurrentPosition()) && (rtConfig->watts.getValue() < userConfig->getMaxWatts())) {
_return = ss2k->getCurrentPosition() + userConfig->getShiftStep();
SS2K_LOG(ERG_MODE_LOG_TAG, "Max Position too close to current incline: %d", _return);
}
// never set below min step.
if (_return <= rtConfig->getMinStep()) {
_return = rtConfig->getCurrentIncline() + userConfig->getShiftStep() * 2;
_return = ss2k->getCurrentPosition() + userConfig->getShiftStep() * 2;
SS2K_LOG(ERG_MODE_LOG_TAG, "Max Position below min!: %d", _return);
}
rtConfig->setMaxStep(_return);
Expand Down Expand Up @@ -1063,7 +1063,7 @@ void ErgMode::computeResistance() {
if (actualDelta != 0) {
rtConfig->setTargetIncline(rtConfig->getTargetIncline() + (100 * actualDelta));
} else {
rtConfig->setTargetIncline(rtConfig->getCurrentIncline());
rtConfig->setTargetIncline(ss2k->getCurrentPosition());
}
oldResistance = rtConfig->resistance;
}
Expand All @@ -1085,7 +1085,7 @@ void ErgMode::computeErg() {
newWatts.setTarget(userConfig->getMinWatts());
}

bool isUserSpinning = this->_userIsSpinning(newCadence, rtConfig->getCurrentIncline());
bool isUserSpinning = this->_userIsSpinning(newCadence, ss2k->getCurrentPosition());
if (!isUserSpinning) {
SS2K_LOG(ERG_MODE_LOG_TAG, "ERG Mode but no User Spin");
return;
Expand All @@ -1106,11 +1106,11 @@ void ErgMode::_setPointChangeState(int newCadence, Measurement& newWatts) {

// Sanity check for targets
if (tableResult != RETURN_ERROR) {
if (rtConfig->watts.getValue() > newWatts.getTarget() && tableResult > rtConfig->getCurrentIncline()) {
if (rtConfig->watts.getValue() > newWatts.getTarget() && tableResult > ss2k->getCurrentPosition()) {
SS2K_LOG(ERG_MODE_LOG_TAG, "Table Result Failed High Test: %d", tableResult);
tableResult = RETURN_ERROR;
}
if (rtConfig->watts.getValue() < newWatts.getTarget() && tableResult < rtConfig->getCurrentIncline()) {
if (rtConfig->watts.getValue() < newWatts.getTarget() && tableResult < ss2k->getCurrentPosition()) {
SS2K_LOG(ERG_MODE_LOG_TAG, "Table Result Failed Low Test: %d", tableResult);
tableResult = RETURN_ERROR;
}
Expand All @@ -1121,14 +1121,14 @@ void ErgMode::_setPointChangeState(int newCadence, Measurement& newWatts) {
int wattChange = newWatts.getTarget() - newWatts.getValue();
float deviation = ((float)wattChange * 100.0) / ((float)newWatts.getTarget());
float factor = abs(deviation) > 10 ? userConfig->getERGSensitivity() * 2 : userConfig->getERGSensitivity() / 2;
tableResult = rtConfig->getCurrentIncline() + (wattChange * factor);
tableResult = ss2k->getCurrentPosition() + (wattChange * factor);
}

SS2K_LOG(ERG_MODE_LOG_TAG, "SetPoint changed:%dw PowerTable Result: %d", newWatts.getTarget(), tableResult);
_updateValues(newCadence, newWatts, tableResult);

if (rtConfig->getTargetIncline() != rtConfig->getCurrentIncline()) { // add some time to wait while the knob moves to target position.
int timeToAdd = abs(rtConfig->getCurrentIncline() - rtConfig->getTargetIncline());
if (rtConfig->getTargetIncline() != ss2k->getCurrentPosition()) { // add some time to wait while the knob moves to target position.
int timeToAdd = abs(ss2k->getCurrentPosition() - rtConfig->getTargetIncline());
if (timeToAdd > 5000) { // 5 seconds
SS2K_LOG(ERG_MODE_LOG_TAG, "Capping ERG seek time to 5 seconds");
timeToAdd = 5000;
Expand All @@ -1145,14 +1145,14 @@ void ErgMode::_inSetpointState(int newCadence, Measurement& newWatts) {
float deviation = ((float)wattChange * 100.0) / ((float)newWatts.getTarget());

float factor = abs(deviation) > 10 ? userConfig->getERGSensitivity() : userConfig->getERGSensitivity() / 2;
float newIncline = rtConfig->getCurrentIncline() + (wattChange * factor);
float newIncline = ss2k->getCurrentPosition() + (wattChange * factor);

_updateValues(newCadence, newWatts, newIncline);
}

void ErgMode::_updateValues(int newCadence, Measurement& newWatts, float newIncline) {
rtConfig->setTargetIncline(newIncline);
_writeLog(rtConfig->getCurrentIncline(), newIncline, this->setPoint, newWatts.getTarget(), this->watts.getValue(), newWatts.getValue(), this->cadence, newCadence);
_writeLog(ss2k->getCurrentPosition(), newIncline, this->setPoint, newWatts.getTarget(), this->watts.getValue(), newWatts.getValue(), this->cadence, newCadence);

this->watts = newWatts;
this->setPoint = newWatts.getTarget();
Expand Down
2 changes: 0 additions & 2 deletions src/Main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -444,8 +444,6 @@ void SS2K::moveStepper() {
stepper->moveTo(rtConfig->getMaxStep() - 1);
}
}
// Sync external object
rtConfig->setCurrentIncline((float)stepper->getCurrentPosition());

if (connectedClientCount() > 0) {
stepper->setAutoEnable(false); // Keep the stepper from rolling back due to head tube slack. Motor Driver still lowers power between moves
Expand Down
2 changes: 1 addition & 1 deletion src/SensorCollector.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ void collectAndSet(NimBLEUUID charUUID, NimBLEUUID serviceUUID, NimBLEAddress ad
}

//////adding incline so that i can plot it
logBufLength += snprintf(logBuf + logBufLength, kLogBufMaxLength - logBufLength, " POS(%d)", ss2k->currentPosition);
logBufLength += snprintf(logBuf + logBufLength, kLogBufMaxLength - logBufLength, " POS(%d)", ss2k->getCurrentPosition());
strncat(logBuf + logBufLength, " ]", kLogBufMaxLength - logBufLength);

SS2K_LOG(BLE_COMMON_LOG_TAG, "%s", logBuf);
Expand Down
1 change: 0 additions & 1 deletion src/SmartSpin_parameters.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@ String RuntimeParameters::returnJSON() {
doc["resistance"] = this->resistance.getValue();
doc["targetResistance"] = this->resistance.getTarget();
doc["targetIncline"] = targetIncline;
doc["currentIncline"] = currentIncline;
doc["speed"] = simulatedSpeed;
doc["simTargetWatts"] = simTargetWatts;
doc["FTMSMode"] = FTMSMode;
Expand Down

0 comments on commit bf69931

Please sign in to comment.