Skip to content

Commit

Permalink
Merge pull request #602 from doudar/Homing_Sensitivity
Browse files Browse the repository at this point in the history
Added Homing Sensitivity
  • Loading branch information
doudar authored Dec 7, 2024
2 parents 1dc4a1b + c6ca432 commit 8dd4f2d
Show file tree
Hide file tree
Showing 8 changed files with 57 additions and 11 deletions.
9 changes: 9 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,15 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

### Added

### Changed
- Added "Homing Sensitivity" so that the homing force value can be adjusted.
### Hardware


## [24.11.25]

### Added

### Changed

### Hardware
Expand Down
Binary file not shown.
1 change: 1 addition & 0 deletions include/BLE_Custom_Characteristic.h
Original file line number Diff line number Diff line change
Expand Up @@ -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_homingSensitivity = 0x2C; // Homing sensitivity value

class BLE_ss2kCustomCharacteristic {
public:
Expand Down
11 changes: 7 additions & 4 deletions include/SmartSpin_parameters.h
Original file line number Diff line number Diff line change
Expand Up @@ -123,11 +123,11 @@ class userParameters {
int stepperSpeed;
bool stepperDir;
bool shifterDir;
bool udpLogEnabled = false;
int32_t hMin = INT32_MIN;
int32_t hMax = INT32_MIN;

bool udpLogEnabled = false;
int32_t hMin = INT32_MIN;
int32_t hMax = INT32_MIN;
bool FTMSControlPointWrite = false;
int homingSensitivity = DEFAULT_HOMING_SENSITIVITY; // Use default from settings.h
String ssid;
String password;
String connectedPowerMeter = CONNECTED_POWER_METER;
Expand Down Expand Up @@ -208,6 +208,9 @@ class userParameters {
void setHMax(int32_t max) { hMax = max; }
int32_t getHMax() { return hMax; }

void setHomingSensitivity(int sensitivity) { homingSensitivity = sensitivity; }
int getHomingSensitivity() { return homingSensitivity; }

void setDefaults();
String returnJSON();
void saveToLittleFS();
Expand Down
9 changes: 6 additions & 3 deletions include/settings.h
Original file line number Diff line number Diff line change
Expand Up @@ -249,9 +249,9 @@ const char* const DEFAULT_PASSWORD = "password";
#endif

// Max size of userconfig
#define USERCONFIG_JSON_SIZE 1524 + DEBUG_LOG_BUFFER_SIZE
#define USERCONFIG_JSON_SIZE 2000 + DEBUG_LOG_BUFFER_SIZE

#define RUNTIMECONFIG_JSON_SIZE 512 + DEBUG_LOG_BUFFER_SIZE
#define RUNTIMECONFIG_JSON_SIZE 1000 + DEBUG_LOG_BUFFER_SIZE

// PowerTable Version
#define TABLE_VERSION 5
Expand Down Expand Up @@ -308,11 +308,14 @@ const char* const DEFAULT_PASSWORD = "password";
// Initial and web scan duration.
#define DEFAULT_SCAN_DURATION 5

// Default homing sensitivity value
#define DEFAULT_HOMING_SENSITIVITY 50

// BLE automatic reconnect duration. Set this low to avoid interruption.
#define BLE_RECONNECT_SCAN_DURATION 5

// Task Stack Sizes
#define MAIN_STACK 6500
#define MAIN_STACK 6500
#define BLE_CLIENT_STACK 6000

// Uncomment to enable stack size debugging info
Expand Down
28 changes: 26 additions & 2 deletions src/BLE_Custom_Characteristic.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -229,7 +229,7 @@ void BLE_ss2kCustomCharacteristic::process(std::string rxValue) {
} break;

case BLE_deviceName: // 0x07
logBufLength = snprintf(logBuf + logBufLength, kLogBufCapacity - logBufLength, "<-deviceName");
logBufLength += snprintf(logBuf + logBufLength, kLogBufCapacity - logBufLength, "<-deviceName");
if (rxValue[0] == cc_read) {
returnValue[0] = cc_success;
returnString = userConfig->getDeviceName();
Expand Down Expand Up @@ -740,6 +740,26 @@ void BLE_ss2kCustomCharacteristic::process(std::string rxValue) {
logBufLength += snprintf(logBuf + logBufLength, kLogBufCapacity - logBufLength, " (%f)", userConfig->getHMax());
}
break;

case BLE_homingSensitivity: // 0x2C
logBufLength += snprintf(logBuf + logBufLength, kLogBufCapacity - logBufLength, "<-homingSensitivity");
if (rxValue[0] == cc_read) {
returnValue[0] = cc_success;
returnValue[2] = (uint8_t)(userConfig->getHomingSensitivity() & 0xff);
returnValue[3] = (uint8_t)(userConfig->getHomingSensitivity() >> 8);
returnLength += 2;
}
if (rxValue[0] == cc_write) {
returnValue[0] = cc_success;
userConfig->setHomingSensitivity(bytes_to_u16(rxValue[3], rxValue[2]));
logBufLength += snprintf(logBuf + logBufLength, kLogBufCapacity - logBufLength, "(%d)", userConfig->getHomingSensitivity());
}
break;

default:
logBufLength += snprintf(logBuf + logBufLength, kLogBufCapacity - logBufLength, "<-Unknown Characteristic");
returnValue[0] = cc_error;
break;
}

SS2K_LOG(CUSTOM_CHAR_LOG_TAG, "%s", logBuf);
Expand Down Expand Up @@ -895,10 +915,14 @@ void BLE_ss2kCustomCharacteristic::parseNemit() {
BLE_ss2kCustomCharacteristic::notify(BLE_hMin);
return;
}

if (userConfig->getHMax() != _oldParams.getHMax()) {
_oldParams.setHMax(userConfig->getHMax());
BLE_ss2kCustomCharacteristic::notify(BLE_hMax);
return;
}
if (userConfig->getHomingSensitivity() != _oldParams.getHomingSensitivity()) {
_oldParams.setHomingSensitivity(userConfig->getHomingSensitivity());
BLE_ss2kCustomCharacteristic::notify(BLE_homingSensitivity);
return;
}
}
4 changes: 2 additions & 2 deletions src/Main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -598,7 +598,7 @@ void SS2K::goHome(bool bothDirections) {
userConfig->setHMax(INT32_MIN);
return;
}
stalled = (driver.SG_RESULT() < threshold - 50);
stalled = (driver.SG_RESULT() < threshold - userConfig->getHomingSensitivity());
}
stepper->forceStop();
vTaskDelay(100 / portTICK_PERIOD_MS);
Expand Down Expand Up @@ -627,7 +627,7 @@ void SS2K::goHome(bool bothDirections) {
userConfig->setHMax(INT32_MIN);
return;
}
stalled = (driver.SG_RESULT() < threshold - 100);
stalled = (driver.SG_RESULT() < threshold - userConfig->getHomingSensitivity());
}
stepper->forceStop();
fitnessMachineService.spinDown(0x02);
Expand Down
6 changes: 6 additions & 0 deletions src/SmartSpin_parameters.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@ void userParameters::setDefaults() {
udpLogEnabled = false;
hMin = INT32_MIN;
hMax = INT32_MIN;
homingSensitivity = DEFAULT_HOMING_SENSITIVITY;
}

//---------------------------------------------------------------------------------
Expand Down Expand Up @@ -104,6 +105,7 @@ String userParameters::returnJSON() {
doc["udpLogEnabled"] = udpLogEnabled;
doc["hMin"] = hMin;
doc["hMax"] = hMax;
doc["homingSensitivity"] = homingSensitivity;

String output;
serializeJson(doc, output);
Expand Down Expand Up @@ -154,6 +156,7 @@ void userParameters::saveToLittleFS() {
doc["udpLogEnabled"] = udpLogEnabled;
doc["hMin"] = hMin;
doc["hMax"] = hMax;
doc["homingSensitivity"] = homingSensitivity;

// Serialize JSON to file
if (serializeJson(doc, file) == 0) {
Expand Down Expand Up @@ -238,6 +241,9 @@ void userParameters::loadFromLittleFS() {
if (!doc["hMax"].isNull()) {
setHMax(doc["hMax"]);
}
if (!doc["homingSensitivity"].isNull()) {
setHomingSensitivity(doc["homingSensitivity"]);
}

SS2K_LOG(CONFIG_LOG_TAG, "Config File Loaded: %s", configFILENAME);
file.close();
Expand Down

0 comments on commit 8dd4f2d

Please sign in to comment.