Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ERG Mode first try #277

Merged
merged 45 commits into from
Dec 24, 2021
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
45 commits
Select commit Hold shift + click to select a range
5c86403
Moved ERG to seperate task
Dec 4, 2021
6b35e29
Added TargetWatts and ERG Mode simulation
Dec 4, 2021
1bfcd56
skip erg comp if stepper is running
Dec 4, 2021
707cdd2
splitted settings to userSettings and RuntimeSettings
Dec 4, 2021
b1a8537
ERG Mode class variables and fix of Measurment access
Dec 4, 2021
50ffd60
Enabled FW update
Dec 4, 2021
65312ab
update rtSetting: added currentIncline.
Dec 5, 2021
7f5d4a1
Added ERG Mode to stepper driver.
Dec 5, 2021
992d26c
reordered parameters
Dec 6, 2021
ea71333
changed BLE Client delay, refactored ERG
Dec 6, 2021
e4020ba
ERG_Mode: added two steps
Dec 10, 2021
9345f35
added csv logging to erg_mode
Dec 12, 2021
9617ef4
reduced minor step to 1/4
Dec 13, 2021
27d461c
Merge branch 'doudar:develop' into develop
MarkusSchneider Dec 17, 2021
d4a93b5
changed minore steps to ergSensitivity/2
Dec 17, 2021
a9739c2
Merge branch 'ERG-Respin' into develop
doudar Dec 19, 2021
80adc83
Integrated Shifting WebPage and small testing fixes.
doudar Dec 19, 2021
77ca1c4
Inserted Licence for pre-commit checks
doudar Dec 19, 2021
98b0c87
Created framework for a power table
doudar Dec 19, 2021
683cb04
Added comments on suggestions for ergPowerTable
doudar Dec 19, 2021
32a2197
Initialized PowerEntry with 0's
doudar Dec 19, 2021
312ff57
Removed millis in powerEntry
doudar Dec 20, 2021
bfc183f
Expanded the PowerTable Class (untested)
doudar Dec 20, 2021
0a83fc9
Added more iteration to powertable lookup
doudar Dec 20, 2021
32be88e
PowerTable::lookup complete. Not tested.
doudar Dec 21, 2021
d165d68
1st Tests.
doudar Dec 21, 2021
5324e2d
Adjusted loop timing and logs for actual testing.
doudar Dec 21, 2021
fa9b423
Added Logging...This might actually work...
doudar Dec 21, 2021
dd0025f
moved isSpinning test earlier in erg
doudar Dec 22, 2021
cbfdae0
added comments
doudar Dec 22, 2021
6f85362
fixed typo
doudar Dec 22, 2021
e859191
fixed a bug
doudar Dec 22, 2021
3e0e5f8
added comment
doudar Dec 22, 2021
8cb092a
fixed bug in newEntry()
Dec 22, 2021
cbafef6
fixed buffer overflow in PowerTable::toLog()
Dec 22, 2021
f2a48e6
Cleaned up the formatting of the powerTable log.
doudar Dec 22, 2021
1e13f04
removed obsolete comments
doudar Dec 22, 2021
6e832df
fixed missing semi in test container
doudar Dec 22, 2021
99212ac
Added ArduinoFake for native unit testing
Dec 23, 2021
e88e053
ERG waits for knob to stop after powertab lookup
doudar Dec 23, 2021
8daf131
Merge branch 'develop' of https://github.com/markusschneider/smartspi…
doudar Dec 23, 2021
b82f2cc
added option to return log in rtConfig.returnJSON
doudar Dec 24, 2021
b0a754b
updates smartspin_parameters.h
doudar Dec 24, 2021
04f3403
Fixed erronious logging.
doudar Dec 24, 2021
6ee7192
Added additional cadence checks for delta correct
doudar Dec 24, 2021
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 6 additions & 4 deletions include/ERG_Mode.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,8 @@

#pragma once

#include <Arduino.h>
#include "SmartSpin_parameters.h"
#include "settings.h"
#include "SmartSpin_parameters.h"

#define ERG_MODE_LOG_TAG "ERG_Mode"
#define ERG_MODE_LOG_CSV_TAG "ERG_Mode_CSV"
Expand Down Expand Up @@ -63,16 +62,19 @@ class PowerBuffer {
class PowerTable {
public:
PowerEntry powerEntry[POWERTABLE_SIZE];

// Catalogs a new entry into the power table.
void newEntry(PowerBuffer powerBuffer);

// returns incline for wattTarget. Null if not found.
int32_t lookup(int watts, int cad);

// load power table from spiffs
bool load();

// save powertable from spiffs
bool save();

// Display power table in log
void toLog();
};


34 changes: 24 additions & 10 deletions src/ERG_Mode.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@ void ergTaskLoop(void* pvParameters) {
// SS2K_LOG(ERG_MODE_LOG_TAG, "powerBuffer.reset();");
}
}

if (isInErgMode && (hasConnectedPowerMeter || simulationRunning)) {
ergMode.computErg(newSetPoint);
} else {
Expand Down Expand Up @@ -109,17 +110,28 @@ void PowerBuffer::reset() {

// Accepts new data into the table and averages input by number of readings in the power entry.
void PowerTable::newEntry(PowerBuffer powerBuffer) {
float tWatts, tCad = 0;
int32_t tTargetPosition = 0;
int watts = 0;
int cad = 0;
int32_t targetPosition = 0;

for (int i = 0; i < POWER_SAMPLES; i++) {
tWatts += powerBuffer.powerEntry[i].watts;
tTargetPosition += powerBuffer.powerEntry[i].targetPosition;
tCad += powerBuffer.powerEntry[i].cad;
if (powerBuffer.powerEntry[i].readings == 0) {
// break if powerEntry is not set
break;
}

if (i == 0) { // first loop -> assign values
watts = powerBuffer.powerEntry[i].watts;
targetPosition = powerBuffer.powerEntry[i].targetPosition;
cad = powerBuffer.powerEntry[i].cad;
continue;
}

// calculate average
watts = (watts + powerBuffer.powerEntry[i].watts) / 2;
targetPosition = (targetPosition + powerBuffer.powerEntry[i].targetPosition) / 2;
cad = (cad + powerBuffer.powerEntry[i].cad) / 2;
}
int watts = tWatts / POWER_SAMPLES;
int cad = tCad / POWER_SAMPLES;
int32_t targetPosition = tTargetPosition / POWER_SAMPLES;

int i = round(watts / POWERTABLE_INCREMENT);

Expand Down Expand Up @@ -148,6 +160,7 @@ int32_t PowerTable::lookup(int watts, int cad) {
int32_t targetPosition;
float cad;
};

int i = round(watts / POWERTABLE_INCREMENT); // find the closest entry
float scale = watts / POWERTABLE_INCREMENT - i; // Should we look at the next higher or next lower index for comparison?
int indexPair = -1; // The next closes index with data for interpolation
Expand Down Expand Up @@ -264,9 +277,10 @@ bool PowerTable::save() {
// save powertable from spiffs
return false; // return unsuccessful
}

// Display power table in log
void PowerTable::toLog() {
char buffer[6]; // crashes when targetPosition gets over 9999
char buffer[6]; // crashes when targetPosition gets over 9999
String oString = "|";
int n;
for (int i = 0; i < POWERTABLE_SIZE; i++) {
Expand All @@ -283,7 +297,7 @@ void PowerTable::toLog() {
SS2K_LOG(POWERTABLE_LOG_TAG, "|%s", oString.c_str());
oString = "|";
for (int i = 0; i < POWERTABLE_SIZE; i++) {
n = sprintf(buffer, "%4d|", this->powerEntry[i].targetPosition);
n = sprintf(buffer, "%8d|", this->powerEntry[i].targetPosition);
oString += buffer;
}
SS2K_LOG(POWERTABLE_LOG_TAG, "|%s", oString.c_str());
Expand Down
9 changes: 8 additions & 1 deletion test/native/test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,14 +18,21 @@ void process() {
RUN_TEST(test.test_parses_cadence);
RUN_TEST(test.test_parses_power);
}

// Cycle Power Tests
test_cyclePowerData test;
{
test_cyclePowerData test;
RUN_TEST(test.test_parses_power);
RUN_TEST(test.test_parses_cadence);
RUN_TEST(test.test_parses_heartrate);
RUN_TEST(test.test_parses_speed);
}

// ERG Mode
{
TestPowerBuffer testPowerBuffer;
RUN_TEST(testPowerBuffer.set__should_set_values__expect_values_added_to_correct_index);
}
UNITY_END();
}

Expand Down
7 changes: 6 additions & 1 deletion test/native/test.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
* SPDX-License-Identifier: GPL-2.0-only
*/

#pragma once
#pragma once

class test_fitnessMachineIndoorBikeData {
public:
Expand All @@ -21,3 +21,8 @@ class test_cyclePowerData {
static void test_parses_heartrate(void);
static void test_parses_speed(void);
};

class TestPowerBuffer {
public:
static void set__should_set_values__expect_values_added_to_correct_index(void);
};
29 changes: 29 additions & 0 deletions test/native/test_PowerBuffer.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
/*
* Copyright (C) 2020 Anthony Doud & Joel Baranick
* All rights reserved
*
* SPDX-License-Identifier: GPL-2.0-only
*/

#include <unity.h>
#include "test.h"
#include "ERG_Mode.h"

void TestPowerBuffer::set__should_set_values__expect_values_added_to_correct_index(void) {
const PowerBuffer powerBuffer
// // Pre parse state
// TEST_ASSERT_FALSE(sensor.hasPower());
// TEST_ASSERT_EQUAL_INT(INT_MIN, sensor.getPower());

// // Power is instantaneous, not much calculation to do so skipping some samples
// sensor.decode(t0, sizeof(t0));
// TEST_ASSERT_TRUE(sensor.hasPower());
// TEST_ASSERT_EQUAL_INT(0, sensor.getPower());

// sensor.decode(t3, sizeof(t0));
// TEST_ASSERT_TRUE(sensor.hasPower());
// TEST_ASSERT_EQUAL_INT(45, sensor.getPower());

// sensor.decode(t7, sizeof(t0));
// TEST_ASSERT_EQUAL_INT(57, sensor.getPower());
}