WARNING: THIS SITE IS A MIRROR OF GITHUB.COM / IT CANNOT LOGIN OR REGISTER ACCOUNTS / THE CONTENTS ARE PROVIDED AS-IS / THIS SITE ASSUMES NO RESPONSIBILITY FOR ANY DISPLAYED CONTENT OR LINKS / IF YOU FOUND SOMETHING MAY NOT GOOD FOR EVERYONE, CONTACT ADMIN AT ilovescratch@foxmail.com
Skip to content

Commit 969b486

Browse files
committed
Hw testing
1 parent 52fb7a9 commit 969b486

File tree

5 files changed

+234
-208
lines changed

5 files changed

+234
-208
lines changed

hw/hw.cpp

Lines changed: 40 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -23,20 +23,14 @@ SOFTWARE.
2323
*/
2424
#include <display.hpp>
2525
#include <kegconfig.hpp>
26-
#include <kegpush.hpp>
27-
#include <kegwebhandler.hpp>
2826
#include <main.hpp>
29-
#include <ota.hpp>
30-
#include <perf.hpp>
3127
#include <scale.hpp>
32-
#include <serialws.hpp>
3328
#include <temp_mgr.hpp>
3429
#include <utils.hpp>
35-
#include <wificonnection.hpp>
3630

37-
SerialDebug mySerial(115200L);
31+
// SerialDebug mySerial(115200L);
32+
SerialDebug mySerial(9600L);
3833
KegConfig myConfig(CFG_MDNSNAME, CFG_FILENAME);
39-
WifiConnection myWifi(&myConfig, CFG_APPNAME, "password", CFG_MDNSNAME);
4034
Scale myScale;
4135
LevelDetection myLevelDetection;
4236
TempSensorManager myTemp;
@@ -46,6 +40,9 @@ int loopCounter = 0;
4640
uint32_t loopMillis = 0;
4741

4842
void setup() {
43+
44+
delay(4000);
45+
4946
char cbuf[20];
5047
uint32_t chipId = 0;
5148
for (int i = 0; i < 17; i = i + 8) {
@@ -57,24 +54,29 @@ void setup() {
5754
Log.notice(F("Main: Build options: %s (%s) LOGLEVEL %d " CR), CFG_APPVER,
5855
CFG_GITREV, LOG_LEVEL);
5956

60-
PERF_BEGIN("setup-display");
6157
// myDisplay.setup();
62-
PERF_END("setup-display");
6358
myConfig.checkFileSystem();
64-
6559
myConfig.loadFile();
6660

6761
// Set a scale factor so we get values
6862
myConfig.setScaleFactor(UnitIndex::U1, 1);
6963
myConfig.setScaleFactor(UnitIndex::U2, 1);
64+
// myConfig.setScaleFactor(UnitIndex::U3, 1);
65+
// myConfig.setScaleFactor(UnitIndex::U4, 1);
66+
7067
myConfig.setScaleOffset(UnitIndex::U1, 1);
7168
myConfig.setScaleOffset(UnitIndex::U2, 1);
69+
// myConfig.setScaleOffset(UnitIndex::U3, 1);
70+
// myConfig.setScaleOffset(UnitIndex::U4, 1);
7271

7372
myScale.setup();
74-
myTemp.setup();
73+
// myTemp.setup();
74+
75+
Log.notice(F("Main: Scale 1 connected=%d ready=%d." CR), myScale.isConnected(UnitIndex::U1), myScale.isReady(UnitIndex::U1));
76+
Log.notice(F("Main: Scale 2 connected=%d ready=%d." CR), myScale.isConnected(UnitIndex::U2), myScale.isReady(UnitIndex::U2));
7577

7678
Log.notice(F("Main: Setup completed." CR));
77-
myTemp.read();
79+
// myTemp.read();
7880
}
7981

8082
// void draw(UnitIndex idx, float temp, float scale1, float scale2, int32_t raw1, int32_t raw2) {
@@ -101,47 +103,47 @@ void setup() {
101103

102104

103105
void loop() {
104-
myScale.loop(UnitIndex::U1);
105-
myScale.loop(UnitIndex::U2);
106-
myScale.loop(UnitIndex::U3);
107-
myScale.loop(UnitIndex::U4);
106+
myScale.loop();
108107

109108
if (abs((int32_t)(millis() - loopMillis)) >
110109
loopInterval) { // 2 seconds loop interval
111110
loopMillis = millis();
112111
loopCounter++;
113112

114113
// Try to reconnect to scales if they are missing (6 seconds)
115-
if (!(loopCounter % 3)) {
116-
if (!myScale.isConnected(UnitIndex::U1) ||
117-
!myScale.isConnected(UnitIndex::U2) ||
118-
!myScale.isConnected(UnitIndex::U3) ||
119-
!myScale.isConnected(UnitIndex::U4)) {
120-
myScale.setup(); // Try to reconnect to scale
121-
}
122-
}
114+
// if (!(loopCounter % 3)) {
115+
// if (!myScale.isConnected(UnitIndex::U1) ||
116+
// !myScale.isConnected(UnitIndex::U2) ||
117+
// !myScale.isConnected(UnitIndex::U3) ||
118+
// !myScale.isConnected(UnitIndex::U4)) {
119+
// myScale.setup(); // Try to reconnect to scale
120+
// }
121+
// }
123122

124123
// The temp sensor should not be read too often. Reading every 4 seconds.
125-
if (!(loopCounter % 1)) {
126-
myTemp.read();
127-
}
124+
// if (!(loopCounter % 1)) {
125+
// myTemp.read();
126+
// }
128127

129128
// Check if the temp sensor exist and try to reinitialize
130-
if (!(loopCounter % 3)) {
131-
if (!myTemp.hasSensor()) {
132-
myTemp.setup();
133-
}
134-
}
129+
// if (!(loopCounter % 3)) {
130+
// if (!myTemp.hasSensor()) {
131+
// myTemp.setup();
132+
// }
133+
// }
135134

136-
float t = myTemp.getLastTempC();
135+
// float t = myTemp.getLastTempC();
137136
float s1 = myScale.read(UnitIndex::U1, true);
138137
float s2 = myScale.read(UnitIndex::U2, true);
139-
float s3 = myScale.read(UnitIndex::U3, true);
140-
float s4 = myScale.read(UnitIndex::U4, true);
138+
// float s3 = myScale.read(UnitIndex::U3, true);
139+
// float s4 = myScale.read(UnitIndex::U4, true);
141140
int32_t l1 = myScale.readLastRaw(UnitIndex::U1);
142141
int32_t l2 = myScale.readLastRaw(UnitIndex::U2);
143-
int32_t l3 = myScale.readLastRaw(UnitIndex::U3);
144-
int32_t l4 = myScale.readLastRaw(UnitIndex::U4);
142+
// int32_t l3 = myScale.readLastRaw(UnitIndex::U3);
143+
// int32_t l4 = myScale.readLastRaw(UnitIndex::U4);
144+
145+
Log.notice(F("Main: Readings scale 1 weight=%F raw=%d" CR), s1, l1);
146+
Log.notice(F("Main: Readings scale 2 weight=%F raw=%d" CR), s2, l2);
145147

146148
// draw(UnitIndex::U1, t, s1, s2, l1, l2);
147149
// draw(UnitIndex::U2, t, s1, s2, l1, l2);

platformio.ini

Lines changed: 22 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,8 @@ include_dir = lib
1414

1515
[common_env_data]
1616
upload_speed = 921600
17-
monitor_speed = 115200
17+
; monitor_speed = 115200
18+
monitor_speed = 9600
1819
platform = espressif32 @ 6.11.0
1920
framework = arduino
2021
build_unflags =
@@ -63,6 +64,7 @@ extra_scripts =
6364
script/copy_firmware.py
6465
build_unflags = ${common_env_data.build_unflags}
6566
build_flags =
67+
-Wl,-Map,output.map
6668
${common_env_data.build_flags}
6769
-D LOG_LEVEL=6
6870
-D ESP32S3=1
@@ -115,20 +117,22 @@ board_build.embed_txtfiles = ${common_env_data.html_files}
115117
; build_src_filter = +<*> -<main.cpp> +<../raw/*.cpp>
116118
; board_build.embed_txtfiles = ${common_env_data.html_files}
117119

118-
; [env:kegmon-esp32s3-pro-hardware]
119-
; upload_speed = ${common_env_data.upload_speed}
120-
; monitor_speed = ${common_env_data.monitor_speed}
121-
; framework = ${common_env_data.framework}
122-
; platform = ${common_env_data.platform}
123-
; build_unflags = ${common_env_data.build_unflags}
124-
; build_flags =
125-
; ${common_env_data.build_flags}
126-
; -D LOG_LEVEL=6
127-
; lib_deps =
128-
; ${common_env_data.lib_deps}
129-
; board = lolin_s3_pro
130-
; board_build.variants_dir = boards
131-
; build_type = release
132-
; board_build.filesystem = littlefs
133-
; build_src_filter = +<*> -<main.cpp> +<../hw/*.cpp>
134-
; board_build.embed_txtfiles = ${common_env_data.html_files}
120+
[env:kegmon-esp32s3-pro-hardware]
121+
upload_speed = ${common_env_data.upload_speed}
122+
monitor_speed = ${common_env_data.monitor_speed}
123+
framework = ${common_env_data.framework}
124+
platform = ${common_env_data.platform}
125+
build_unflags = ${common_env_data.build_unflags}
126+
build_flags =
127+
${common_env_data.build_flags}
128+
-D LOG_LEVEL=6
129+
-D ESP32S3=1
130+
lib_deps =
131+
${common_env_data.lib_deps}
132+
board = lolin_s3_pro
133+
board_build.variants_dir = boards
134+
build_type = debug
135+
board_build.filesystem = littlefs
136+
board_build.partitions = part32_16mb_coredump.csv
137+
build_src_filter = +<*> -<main.cpp> +<../hw/*.cpp>
138+
board_build.embed_txtfiles = ${common_env_data.html_files}

src/kegwebhandler.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -442,7 +442,7 @@ void KegWebHandler::webStatus(AsyncWebServerRequest *request) {
442442
#if defined(ESP32S3)
443443
obj[PARAM_PLATFORM] = "esp32s3";
444444
#else
445-
#error "Undefined target"
445+
#error "Undefined target"
446446
#endif
447447
obj[PARAM_APP_VER] = CFG_APPVER;
448448
obj[PARAM_APP_BUILD] = CFG_GITREV;

src/scale.cpp

Lines changed: 159 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,119 @@ SOFTWARE.
2525
#include <perf.hpp>
2626
#include <scale.hpp>
2727

28-
void Scale::loop(UnitIndex idx) {
28+
bool Scale::isReady(UnitIndex idx) const {
29+
if(!_hxScale[idx]) return false;
30+
return _hxScale[idx]->wait_ready_retry(3, 500);
31+
}
32+
33+
void Scale::tare(UnitIndex idx) {
34+
if (!_hxScale[idx]) return;
35+
36+
Log.notice(
37+
F("SCAL: HX711 set scale to zero, prepare for calibration %d [%d]." CR),
38+
myConfig.getScaleReadCountCalibration(), idx);
39+
40+
_hxScale[idx]->set_scale(1.0);
41+
_hxScale[idx]->tare(myConfig.getScaleReadCountCalibration());
42+
int32_t l = _hxScale[idx]->get_offset();
43+
Log.verbose(F("SCAL: HX711 New scale offset found %l [%d]." CR), l, idx);
44+
myConfig.setScaleOffset(idx, l);
45+
myConfig.saveFile();
46+
}
47+
48+
void Scale::findFactor(UnitIndex idx, float weight) {
49+
if (!_hxScale[idx]) return;
50+
51+
float l = _hxScale[idx]->get_units(myConfig.getScaleReadCountCalibration());
52+
float f = l / weight;
53+
Log.notice(
54+
F("SCAL: HX711 Detecting factor for weight %F, raw %l %F [%d]." CR),
55+
weight, l, f, idx);
56+
57+
if (isinf(f) || isnan(f)) f = 0.0;
58+
59+
myConfig.setScaleFactor(idx, f);
60+
myConfig.saveFile(); // save the factor to file
61+
62+
setScaleFactor(idx); // apply the factor after it has been saved
63+
read(idx, true);
64+
}
65+
66+
int32_t Scale::readRaw(UnitIndex idx) {
67+
#if defined(DEBUG_LINK_SCALES)
68+
idx = UnitIndex::U1;
69+
#endif
70+
#if LOG_LEVEL == 6
71+
Log.verbose(F("SCAL: HX711 Reading raw scale for [%d]." CR), idx);
72+
#endif
73+
if (!_hxScale[idx]) return 0;
74+
PERF_BEGIN("scale-readraw");
75+
int32_t l = _hxScale[idx]->read_average(
76+
myConfig.getScaleReadCountCalibration()); // get the raw value without
77+
// applying scaling factor
78+
_lastRaw[idx] = l;
79+
#if LOG_LEVEL == 6
80+
Log.verbose(F("SCAL: HX711 Reading scale raw weight=%d [%d]" CR), l, idx);
81+
#endif
82+
PERF_END("scale-readraw");
83+
return l;
84+
}
85+
86+
void Scale::setupScale(UnitIndex idx, bool force, int pinData, int pinClock) {
87+
if (!_hxScale[idx] || force) {
88+
if (_hxScale[idx]) delete _hxScale[idx];
89+
90+
#if LOG_LEVEL == 6
91+
Log.verbose(F("SCAL: HX711 initializing scale [0], using offset %l." CR),
92+
myConfig.getScaleOffset(idx));
93+
#endif
94+
_hxScale[idx] = new HX711();
95+
Log.notice(
96+
F("SCAL: Initializing HX711 bus #%d on pins Data=%d,Clock=%d" CR),
97+
idx + 1, pinData, pinClock);
98+
_hxScale[idx]->begin(pinData, pinClock);
99+
_hxScale[idx]->set_offset(myConfig.getScaleOffset(idx));
100+
101+
if (_hxScale[idx]->wait_ready_timeout(500)) {
102+
Log.notice(F("SCAL: HX711 scale [%d] found." CR), idx);
103+
_hxScale[idx]->get_units(1);
104+
} else {
105+
Log.error(
106+
F("SCAL: HX711 scale [%d] not responding, disabling interface." CR),
107+
idx);
108+
delete _hxScale[idx];
109+
_hxScale[idx] = nullptr;
110+
}
111+
}
112+
113+
setScaleFactor(idx);
114+
}
115+
116+
void Scale::setScaleFactor(UnitIndex idx) {
117+
if (!_hxScale[idx]) return;
118+
119+
float fs = myConfig.getScaleFactor(idx);
120+
121+
if (fs == 0.0) fs = 1.0;
122+
123+
_hxScale[idx]->set_scale(fs);
124+
}
125+
126+
void Scale::setup(bool force) {
127+
setupScale(UnitIndex::U1, force, PIN_SCALE_SDA1, PIN_SCALE_SCK1);
128+
// setupScale(UnitIndex::U2, force, PIN_SCALE_SDA2, PIN_SCALE_SCK2);
129+
// setupScale(UnitIndex::U3, force, PIN_SCALE_SDA3, PIN_SCALE_SCK3);
130+
// setupScale(UnitIndex::U4, force, PIN_SCALE_SDA4, PIN_SCALE_SCK4);
131+
}
132+
133+
void Scale::loop() {
134+
loopScale(UnitIndex::U1);
135+
// loopScale(UnitIndex::U2);
136+
// loopScale(UnitIndex::U3);
137+
// loopScale(UnitIndex::U4);
138+
}
139+
140+
void Scale::loopScale(UnitIndex idx) {
29141
if (_sched[idx].tare) {
30142
#if LOG_LEVEL == 6
31143
Log.verbose(F("SCAL: Tare triggered [%d]." CR), idx);
@@ -45,4 +157,50 @@ void Scale::loop(UnitIndex idx) {
45157
}
46158
}
47159

160+
float Scale::read(UnitIndex idx, bool skipValidation) {
161+
#if defined(DEBUG_LINK_SCALES)
162+
idx = UnitIndex::U1;
163+
#endif
164+
165+
if (!_hxScale[idx]) return 0;
166+
167+
if (myConfig.getScaleFactor(idx) == 0 ||
168+
myConfig.getScaleOffset(idx) == 0) { // Not initialized, just return zero
169+
Log.verbose(F("SCAL: HX711 scale not initialized [%d]." CR), idx);
170+
return 0;
171+
}
172+
173+
#if LOG_LEVEL == 6
174+
Log.verbose(F("SCAL: HX711 reading scale for [%d]." CR), idx);
175+
#endif
176+
177+
PERF_BEGIN("scale-read");
178+
float raw = _hxScale[idx]->get_units(myConfig.getScaleReadCount());
179+
#if LOG_LEVEL == 6
180+
Log.verbose(F("SCAL: HX711 Reading weight=%F [%d]" CR), raw, idx);
181+
#endif
182+
183+
if (!skipValidation) {
184+
// If the value is higher/lower than 100 kb/lbs then the reading is
185+
// proably wrong, just ignore the reading
186+
if (raw > 100) {
187+
Log.error(F("SCAL: HX711 Ignoring value since it's higher than 100kg, %F "
188+
"[%d]." CR),
189+
raw, idx);
190+
PERF_END("scale-read");
191+
return NAN;
192+
}
193+
194+
if (raw < -100) {
195+
Log.error(F("SCAL: HX711 Ignoring value since it's less than -100kg %F "
196+
"[%d]." CR),
197+
raw, idx);
198+
PERF_END("scale-read");
199+
return NAN;
200+
}
201+
}
202+
203+
PERF_END("scale-read");
204+
return raw;
205+
}
48206
// EOF

0 commit comments

Comments
 (0)