-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathcalibration.c
180 lines (142 loc) · 4.73 KB
/
calibration.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
// MIT License
//
// Copyright (c) 2023 Daniel Robertson
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in all
// copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
// SOFTWARE.
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <tusb.h>
#include "pico/stdio.h"
#include "../include/scale.h"
#include "../extern/hx711-pico-c/include/hx711_noblock.pio.h"
size_t getchars(char* arr, const size_t len) {
memset(arr, 0, len);
size_t i = 0;
for(; i < len;) {
int c = getchar();
if(c == 0) {
continue;
}
else if(c == '\r' || c == '\n') {
putchar('\n');
break;
}
putchar(c); //echo
arr[i] = (char)c;
++i;
}
return i + 1;
}
int main() {
stdio_init_all();
//1. Change these two lines to the pins used to connect to
//the HX711's clock pin and data pin, respectively.
//pinout reference: https://learn.adafruit.com/assets/99339
const uint clkPin = 14; // GP14, PAD19
const uint datPin = 15; // GP15, PAD20
//2. Change the line below to the rate used by the HX711.
//If you're unsure, it's probably 10
const hx711_rate_t hxRate = hx711_rate_10;
char buff[32];
char unit[10];
double knownWeight;
int32_t zeroValue;
double raw;
double refUnitFloat;
int32_t refUnit;
hx711_t hx;
scale_t sc;
scale_options_t opt = SCALE_DEFAULT_OPTIONS;
hx711_init(
&hx,
clkPin,
datPin,
pio0,
&hx711_noblock_program,
&hx711_noblock_program_init);
hx711_power_up(&hx, hx711_gain_128);
hx711_wait_settle(hxRate);
scale_init(&sc, &hx, mass_ug, 1, 0);
//wait for serial connection
while(!tud_cdc_connected()) {
sleep_ms(10);
}
//clear the screen
printf("\x1B[2J\x1B[H");
printf(
"========================================\n\
HX711 Calibration\n\
========================================\n\
\n\
Find an object you know the weight of. If you can't find anything, \n\
try searching Google for your phone's specifications to find its weight. \n\
You can then use your phone to calibrate your scale.\n\
\n"
);
printf("1. Enter the unit you want to measure the object in (eg. g, kg, lb, oz): ");
getchars(unit, sizeof(unit));
printf("\n2. Enter the weight of the object in the unit you chose (eg. \
if you chose 'g', enter the weight of the object in grams): ");
getchars(buff, sizeof(buff));
knownWeight = atof(buff);
printf("\n3. Enter the number of samples to take from the HX711 chip (eg. 1000): ");
getchars(buff, sizeof(buff));
opt.samples = (size_t)atol(buff);
printf("\n4. Remove all objects from the scale and then press enter.");
getchar();
printf("\nWorking...");
if(!scale_read(&sc, &raw, &opt)) {
printf("ERROR: failed to read from scale");
return EXIT_FAILURE;
}
zeroValue = (int32_t)round(raw);
printf("\n\n5. Place object on the scale and then press enter.");
getchar();
printf("\nWorking...");
if(!scale_read(&sc, &raw, &opt)) {
printf("ERROR: failed to read from scale");
}
refUnitFloat = (raw - zeroValue) / knownWeight;
refUnit = (int32_t)round(refUnitFloat);
hx711_close(&hx);
if(refUnit == 0) {
refUnit = 1;
}
//cppcheck-suppress invalidPrintfArgType_sint
printf("\
\n\n\
Known weight (your object): %f %s\n\
Raw value over %zu samples: %li\n\
\n\
-> REFERENCE UNIT: %li\n\
-> ZERO VALUE: %li\n\
\n\
You can provide these values to the scale_init() function. For example: \n\
\n\
scale_init(&sc, &hx, /* your chosen mass_unit_t */, %li, %li);\
\n",
knownWeight, unit,
opt.samples, (int32_t)raw,
refUnit,
zeroValue,
refUnit, zeroValue);
getchar();
return EXIT_SUCCESS;
}