Skip to content

Commit

Permalink
Merge pull request #386 from doudar/serial_test
Browse files Browse the repository at this point in the history
  • Loading branch information
doudar authored Nov 14, 2022
2 parents ef52e85 + 5e3c0cc commit af30aac
Show file tree
Hide file tree
Showing 14 changed files with 363 additions and 111 deletions.
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,19 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
## [Unreleased]

### Added
- Added blocking for shifts above or below min/max setpoints.
- Added Peloton serial decoder to sensor factory.
- Added blocking for shifts above or below min/max set points.
- Added power scaler for new board.
- Added Main Index link to develop.html.

### Changed
- PowerTable values are now adjusted to 90 RPM cad on input.
- PowerTable entries are now validated against previous entries.
- Changes to default settings for better ride-ability. raised incline multiplier and erg sensitivity, increased incline multiplier and max brake watts.
- Fixed a bug in the new cadence compensation where an int should have been a float.
- Moved serial checking to own function.
- Reduced verbosity of ERG logging.

### Hardware
- removed duplicate directory in direct mount folder
Expand Down
32 changes: 18 additions & 14 deletions data/develop.html
Original file line number Diff line number Diff line change
Expand Up @@ -3,24 +3,28 @@

<head>
<style type="text/css">
html {
background-color: #03245c;
}
</style>
html {
background-color: #03245c;
}
</style>

<title>SmartSpin2K Web Server</title>
<meta name="viewport" content="width=device-width, initial-scale=1">
</head>
<fieldset><legend><a href="http://github.com/doudar/SmartSpin2k">http://github.com/doudar/SmartSpin2k</a></legend>
<body>
<h1 style="text-align: center;"><strong>Developer Tools</strong></h1>
<h2>
<p style="text-align: center;"><strong><a href="streamfit.html">Replay Fit File Over BLE</a></strong></p>
<p style="text-align: center;"><strong><a href="btsimulator.html">Manual BLE Simulator</a></strong></p>
<p style="text-align: center;"><strong><a href="status.html">SmartSpin Debugging Info</a></strong></p>
<p style="text-align: center;"><strong><a href="login">Update Firmware</a></strong></p>
<p><a href=https://github.com/doudar/SmartSpin2k/wiki>SS2K Help</a></p>
</h2>
<fieldset>
<legend><a href="http://github.com/doudar/SmartSpin2k">http://github.com/doudar/SmartSpin2k</a></legend>

<body>
<p style="text-align: left;"><strong><a href="index.html">Main Index</a></strong></p>
<h1>
<h1 style="text-align: center;"><strong>Developer Tools</strong></h1>
<h2>
<p style="text-align: center;"><strong><a href="streamfit.html">Replay Fit File Over BLE</a></strong></p>
<p style="text-align: center;"><strong><a href="btsimulator.html">Manual BLE Simulator</a></strong></p>
<p style="text-align: center;"><strong><a href="status.html">SmartSpin Debugging Info</a></strong></p>
<p style="text-align: center;"><strong><a href="login">Update Firmware</a></strong></p>
<p><a href=https://github.com/doudar/SmartSpin2k/wiki>SS2K Help</a></p>
</h2>
</fieldset>

<script>
Expand Down
45 changes: 33 additions & 12 deletions include/Main.h
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
#include "BLE_Common.h"
#include "LittleFS_Upgrade.h"
#include "boards.h"
#include "SensorCollector.h"

#define MAIN_LOG_TAG "Main"

Expand Down Expand Up @@ -49,19 +50,33 @@ class SS2K {
void updateStealthchop();
void checkDriverTemperature();
void motorStop(bool releaseTension = false);
void checkSerial();

SS2K() {
targetPosition = 0;
currentPosition = 0;
stepperIsRunning = false;
externalControl = false;
syncMode = false;
lastDebounceTime = 0;
debounceDelay = DEBOUNCE_DELAY;
lastShifterPosition = 0;
shiftersHoldForScan = SHIFTERS_HOLD_FOR_SCAN;
scanDelayTime = 10000;
scanDelayStart = 0;
targetPosition = 0;
currentPosition = 0;
stepperIsRunning = false;
externalControl = false;
syncMode = false;
lastDebounceTime = 0;
debounceDelay = DEBOUNCE_DELAY;
lastShifterPosition = 0;
shiftersHoldForScan = SHIFTERS_HOLD_FOR_SCAN;
scanDelayTime = 10000;
scanDelayStart = 0;
}
};

class AuxSerialBuffer {
public:
uint8_t data[20];
size_t len;

AuxSerialBuffer() {
for (int i = 0; i < AUX_BUF_SIZE; i++) {
this->data[i] = 0;
}
this->len = 0;
}
};

Expand All @@ -72,4 +87,10 @@ extern SS2K ss2k;

// Main program variable that stores most everything
extern userParameters userConfig;
extern RuntimeParameters rtConfig;
extern RuntimeParameters rtConfig;

//Peloton Specific Parameters
#define PELOTON_RQ_SIZE 4
const uint8_t peloton_rq_watts[]{0xF5, 0x44, 0x39, 0xF6};
const uint8_t peloton_rq_cad[]{0xF5, 0x41, 0x36, 0xF6};
const uint8_t peloton_rq_res[]{0xF5, 0x4A, 0x3F, 0xF6};
14 changes: 14 additions & 0 deletions include/SensorCollector.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
/*
* Copyright (C) 2020 Anthony Doud & Joel Baranick
* All rights reserved
*
* SPDX-License-Identifier: GPL-2.0-only
*/
#include <NimBLEDevice.h>
#include <Arduino.h>
#include <Main.h>

#pragma once


void collectAndSet(NimBLEUUID charUUID, NimBLEUUID serviceUUID, NimBLEAddress address, uint8_t *pData, size_t length);
3 changes: 3 additions & 0 deletions include/boards.h
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ class Board {
int stepperSerialRxPin;
int auxSerialTxPin;
int auxSerialRxPin;
int pwrScaler;
};

class Boards {
Expand All @@ -47,6 +48,7 @@ class Boards {
rev1.stepperSerialRxPin = r1_STEPPERSERIAL_RX;
rev1.auxSerialTxPin = 0;
rev1.auxSerialRxPin = 0;
rev1.pwrScaler = r1_PWR_SCALER;
// Rev 2
rev2.name = r2_NAME;
rev2.versionVoltage = r2_VERSION_VOLTAGE;
Expand All @@ -59,5 +61,6 @@ class Boards {
rev2.stepperSerialRxPin = r2_STEPPERSERIAL_RX;
rev2.auxSerialTxPin = r2_AUX_SERIAL_TX;
rev2.auxSerialRxPin = r2_AUX_SERIAL_RX;
rev2.pwrScaler = r2_PWR_SCALER;
}
};
22 changes: 17 additions & 5 deletions include/settings.h
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,9 @@

// TMC2208/TMC2224 SoftwareSerial transmit pin
#define r1_STEPPERSERIAL_TX 12

// Reduce current setting by this divisor (0-31)
#define r1_PWR_SCALER 31
////////////////////////////////////////////////////////
//////////// Defines for hardware Revision 2 ////////////

Expand Down Expand Up @@ -153,10 +156,13 @@
#define r2_STEPPERSERIAL_TX 19

// TMC2209 SoftwareSerial receive pin
#define r2_AUX_SERIAL_RX 21
#define r2_AUX_SERIAL_RX 22

// TMC2209 SoftwareSerial transmit pin
#define r2_AUX_SERIAL_TX 22
#define r2_AUX_SERIAL_TX 21

// Reduce current setting by this divisor (0-31)
#define r2_PWR_SCALER 12
////////////////////////////////////////////////////////

// TMC2208/TMC2224 HardwareSerial port
Expand All @@ -165,9 +171,6 @@
// Match to your driver
#define R_SENSE 0.11f

// Maximum scaling of the driver output power 0-31
#define DRIVER_MAX_PWR_SCALER 31

// Hardware pin for indicator LED *note* internal LED on esp32 Dev board is pin
// 2
#define LED_PIN 2
Expand Down Expand Up @@ -233,6 +236,15 @@
// Temperature of the ESP32 at which to start reducing the power output of the stepper motor driver.
#define THROTTLE_TEMP 85

// Size of the Aux Serial Buffer for Peloton
#define AUX_BUF_SIZE 20

// Interrogate Peloton bike for data?
#define PELOTON_TX true

// If not receiving Peleton Messages, how long to wait before next TX attempt is
#define TX_CHECK_INTERVAL 20

// Uncomment to enable sending Telegram debug messages back to the chat
// specified in telegram_token.h
// #define USE_TELEGRAM
Expand Down
10 changes: 10 additions & 0 deletions lib/SS2K/include/Constants.h
Original file line number Diff line number Diff line change
Expand Up @@ -47,3 +47,13 @@
#define ECHELON_SERVICE_UUID NimBLEUUID("0bf669f1-45f2-11e7-9598-0800200c9a66")
#define ECHELON_WRITE_UUID NimBLEUUID("0bf669f2-45f2-11e7-9598-0800200c9a66")
#define ECHELON_DATA_UUID NimBLEUUID("0bf669f4-45f2-11e7-9598-0800200c9a66")

// Dummy UUID for Peloton Serial Data Interface
#define PELOTON_DATA_UUID NimBLEUUID("00000000-0000-0000-0000-0000000000321")
#define PELOTON_ADDRESS NimBLEAddress("00:00:00:00:00:00:00")
// peloton Serial
#define HEADER 0xF1
#define FOOTER 0xF6
#define CAD_ID 0x41
#define RES_ID 0x4A
#define POW_ID 0x44
30 changes: 30 additions & 0 deletions lib/SS2K/include/sensors/PelotonData.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
/*
* Copyright (C) 2020 Anthony Doud & Joel Baranick
* All rights reserved
*
* SPDX-License-Identifier: GPL-2.0-only
*/

#pragma once

#include "SensorData.h"

class PelotonData : public SensorData {
public:
PelotonData() : SensorData("PTON") {}

bool hasHeartRate();
bool hasCadence();
bool hasPower();
bool hasSpeed();
int getHeartRate();
float getCadence();
int getPower();
float getSpeed();
void decode(uint8_t *data, size_t length);

private:
bool hasData = false;
float cadence = nanf("");
int power = INT_MIN;
};
65 changes: 65 additions & 0 deletions lib/SS2K/src/sensors/PelotonData.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
/*
* Copyright (C) 2020 Anthony Doud & Joel Baranick
* All rights reserved
*
* SPDX-License-Identifier: GPL-2.0-only
*/

#include "endian.h"
#include "sensors/PelotonData.h"
#include "Constants.h"

bool PelotonData::hasHeartRate() { return false; }

bool PelotonData::hasCadence() { return this->hasData; }

bool PelotonData::hasPower() { return this->hasData; }

bool PelotonData::hasSpeed() { return false; }

int PelotonData::getHeartRate() { return INT_MIN; }

float PelotonData::getCadence() { return this->cadence; }

int PelotonData::getPower() { return this->power; }

float PelotonData::getSpeed() { return nanf(""); }

void PelotonData::decode(uint8_t *data, size_t length) {
float value = 0.0;
const uint8_t payload_length = data[2];
for (uint8_t i = 2 + payload_length; i > 2; i--) {
// -30 = Convert from ASCII to numeric
uint8_t next_digit = data[i] - 0x30;
// Check for overflow
if (value > 6553 || (value == 6553 && next_digit > 5)) {
return;
}
value = value * 10 + next_digit;
}
hasData = true;
switch (data[1]) {
case POW_ID:
if (value >= 0) {
power = value / 10;
} else {
power = 0;
}

break;

case CAD_ID:
cadence = value;
break;

case RES_ID:
// we don't use resistance
break;

default:
break;
// cadence = nanf("");
// power = INT_MIN;
// hasData = false;
}
}
3 changes: 3 additions & 0 deletions lib/SS2K/src/sensors/SensorDataFactory.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
#include "sensors/FitnessMachineIndoorBikeData.h"
#include "sensors/HeartRateData.h"
#include "sensors/EchelonData.h"
#include "sensors/PelotonData.h"

std::shared_ptr<SensorData> SensorDataFactory::getSensorData(const NimBLEUUID characteristicUUID, const uint64_t peerAddress, uint8_t *data, size_t length) {
for (auto &it : SensorDataFactory::knownDevices) {
Expand All @@ -32,6 +33,8 @@ std::shared_ptr<SensorData> SensorDataFactory::getSensorData(const NimBLEUUID ch
sensorData = std::shared_ptr<SensorData>(new FlywheelData());
} else if (characteristicUUID == ECHELON_DATA_UUID) {
sensorData = std::shared_ptr<SensorData>(new EchelonData());
} else if (characteristicUUID == PELOTON_DATA_UUID) {
sensorData = std::shared_ptr<SensorData>(new PelotonData());
} else {
return NULL_SENSOR_DATA;
}
Expand Down
Loading

0 comments on commit af30aac

Please sign in to comment.