Skip to content

Commit 08b2043

Browse files
committed
automatic calibration data collection
1 parent b4d5f4f commit 08b2043

File tree

1 file changed

+111
-28
lines changed

1 file changed

+111
-28
lines changed

manual_tests/motors_calibration_test/motors_calibration_test.c

Lines changed: 111 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -10,62 +10,72 @@
1010
#define LED1 1
1111

1212
void calibration();
13-
void sensor_reading();
1413
void simultaneous();
14+
void sensor_reading();
15+
void speed_test();
16+
void turn_test();
17+
void adjustment();
18+
1519
//slower stronger torque
20+
//shaft rotates exactly one circle with this period and times
1621
uint8_t period = 100;
17-
uint8_t times = 12; //shaft rotates exactly one circle with this period and times
22+
uint8_t times = 12;
1823
double sum = 0.0;
19-
// TO-DO: keeping track of how many "steps" down
2024
uint8_t step = 0;
2125

22-
// (channels 10 and 11 are something else - motor positioning sensors)
23-
// PHT1 is channel 11, PHT2 is channel 10
26+
// PHT1 is channel 11 (ADC pin 7), PHT2 is channel 10 (ADC pin 8)
2427
uint8_t adc_channels[] = {10, 11};
2528
uint8_t calibration_channel_num = sizeof(adc_channels) / sizeof(adc_channels[0]);
29+
float readings[4] = {0.0, 0.0, 0.0, 0.0};
30+
uint8_t sample_size = 100;
2631

2732
uint8_t key_pressed(const uint8_t* buf, uint8_t len) {
2833
if (len == 0) {
2934
return 0;
3035
}
3136
uint8_t count = 0;
3237
switch (buf[0]) {
33-
case 'w':
34-
while (count < 5){
38+
case 'd':
39+
while (count < 1){
3540
print("cycle %d, step %d\n",count+1, step+1);
36-
calibration();
41+
actuate_motors(period, times, false);
3742
count += 1;
3843
}
3944
break;
40-
case 's':
45+
case 'u':
4146
while (count < 5){
4247
actuate_motors(period, times, true);
43-
print("Moving up");
4448
count += 1;
4549
step -= 1; //step now underflow here
4650
}
51+
print("Moving up\n");
52+
break;
53+
case 'c':
54+
calibration();
4755
break;
4856
case 'r':
4957
//reset set point and step count
58+
print("Moving up\n");
59+
while (step > 0){
60+
actuate_motors(period, times, true);
61+
step -= 1;
62+
}
5063
sum = 0.0;
5164
step = 0;
5265
print("System reset\n");
5366
break;
54-
case 't':
55-
for (uint8_t i = 0; i < calibration_channel_num; i++){
56-
fetch_all_adc_channels(&adc);
57-
uint8_t channel = adc_channels[i];
58-
uint16_t raw_data = read_adc_channel(&adc, channel);
59-
print("Channel %d Raw Data: %d\n", channel, raw_data);
60-
sum += raw_data;
61-
}
67+
case 's':
6268
simultaneous();
6369
break;
6470
case 'e':
6571
//reset set point
6672
sensor_reading();
67-
disable_motors();
68-
print("motors disabled\n");
73+
break;
74+
case 't':
75+
turn_test();
76+
break;
77+
case 'k':
78+
speed_test();
6979
break;
7080
default:
7181
print("Invalid command\n");
@@ -74,20 +84,41 @@ uint8_t key_pressed(const uint8_t* buf, uint8_t len) {
7484
return 1;
7585
}
7686

77-
// this function runs the motor down continuously until user press stop
78-
// it counts the "step" the motors need to go from top to MF chip
79-
// forward, true -> up
80-
// backward, false -> down
87+
/*
88+
this function runs the motor down for one "step". A "step" refers to one full turn of the shaft.
89+
*/
8190
void calibration(){
8291
print("Actuating: %dms, %d times, going down\n", period, times);
83-
actuate_motors(period, times, false);
84-
step += 1;
85-
sensor_reading();
92+
// for actuate_motors(), forward -> true -> up, backward -> false -> down
93+
print("Step Raw1 Volt1 Raw2 Volt2\n");
94+
while(step < 30){
95+
actuate_motors(period, times, false);
96+
step += 1;
97+
_delay_ms(1000);
98+
for (uint8_t i = 0; i < calibration_channel_num; i++){
99+
fetch_all_adc_channels(&adc);
100+
uint8_t channel = adc_channels[i];
101+
for (uint8_t j = 0; j < sample_size; j++){
102+
uint16_t raw_data = read_adc_channel(&adc, channel);
103+
readings[2*i] = readings[2*i] + raw_data;
104+
double voltage = adc_raw_data_to_raw_vol(raw_data);
105+
readings[2*i+1] = readings[2*i+1] + voltage;
106+
}
107+
}
108+
print("%d %f %f %f %f\n",step,(readings[0]/sample_size),(readings[1]/sample_size),(readings[2]/sample_size),(readings[3]/sample_size));
109+
for (uint8_t j = 0; j < 4; j++){
110+
readings[j] = 0.0;
111+
}
112+
}
86113
}
87114

115+
/*
116+
This function runs the motor down for until the distance set-point.
117+
Currently the set-point is calculated by averaging the raw data reading from two calibration ADC channels. In the future the set-point might be mapped to a physical distance by some internal mapping function so it's more human-readable and verifiable.
118+
*/
88119
void simultaneous(){
89120
sum = 0.0;
90-
// sum is the set-point
121+
// sum is the set-point for now
91122
while (sum < 4.3){
92123
//reset after each step
93124
sum = 0.0;
@@ -107,17 +138,69 @@ void simultaneous(){
107138
print("motors disabled\n");
108139
}
109140

141+
/*
142+
This function reads the two sensor ADC channels and print out the raw data and mapped voltage. It then returns an array of readings (in raw data format, uint16_t).
143+
*/
110144
void sensor_reading(){
111145
for (uint8_t i = 0; i < calibration_channel_num; i++){
112146
fetch_all_adc_channels(&adc);
113147
uint8_t channel = adc_channels[i];
114148
print("Channel %d\n", channel);
115149
uint16_t raw_data = read_adc_channel(&adc, channel);
150+
readings[i] = raw_data;
116151
double voltage = adc_raw_data_to_raw_vol(raw_data);
117152
print("Raw Data: %d, Voltage: %f V\n", raw_data, voltage);
118153
}
119154
}
120155

156+
/*
157+
This function runs the motor for a range of periods (from 50~200 with a increment of 10) but the same times. This is for calibration purposes.
158+
*/
159+
void speed_test(){
160+
uint8_t period = 50;
161+
print("testing with cycles_num %d", times);
162+
for(uint8_t i = 0; i < 15; i = i + 10){
163+
period = period + i;
164+
print("iter %d, period %d\n", (i/10), period);
165+
actuate_motors(period, times, true);
166+
_delay_ms(1000);
167+
}
168+
}
169+
170+
/*
171+
This function runs the motor for a range of times (from 5~100 with a increment of 5) but the same period. This is for calibration purposes.
172+
*/
173+
void turn_test(){
174+
uint8_t cycles = 5;
175+
print("testing with period %d\n", period);
176+
for(uint8_t i = 0; i < 19; i = i + 5){
177+
cycles = cycles + i;
178+
print("iter %d, period %d", (i/5), cycles);
179+
actuate_motors(period, cycles, true);
180+
_delay_ms(1000);
181+
}
182+
}
183+
184+
/*
185+
When calling this function, it can adjust the platform to ensure the actuation plate is level.
186+
This is done by comparing the two sensors on each side and correct the error with a P(ID) feedback loop.
187+
*/
188+
void adjustment(){
189+
// pre-calibrated control parameters
190+
/*uint8_t Kp = 10;
191+
uint8_t Ki = 0;
192+
uint8_t Kd = 0;
193+
uint8_t error = 0;
194+
uint16_t last_error = 0;
195+
uint16_t integral = 0;
196+
uint8_t integral_cap = 2;*/
197+
198+
// assume two sensors on the side greater +/- 1 is consistent
199+
//uint8_t desired = (sensor1 + sensor2) >> 1;
200+
//error = desired - actual;
201+
202+
}
203+
121204
int main(void){
122205
init_uart();
123206
print("\n\nUART initialized\n");

0 commit comments

Comments
 (0)