-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathMonitorMCB.cpp
More file actions
621 lines (541 loc) · 25.9 KB
/
MonitorMCB.cpp
File metadata and controls
621 lines (541 loc) · 25.9 KB
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
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
/*
* Monitor.cpp
* File implementing the MCB monitor
* Author: Alex St. Clair
* October 2018
*/
#include "MonitorMCB.h"
#include "Serialize.h"
MonitorMCB::MonitorMCB(SafeBuffer * monitor_q, SafeBuffer * action_q, Reel * reel_in, LevelWind * lw_in, InternalSerialDriverMCB * dibdriver, ConfigManagerMCB * cfgManager)
: ltcManager(LTC_TEMP_CS_PIN, LTC_TEMP_RESET_PIN, THERM_SENSE_CH, RTD_SENSE_CH)
, storageManager()
{
monitor_low_power = false;
monitor_reel = false;
monitor_levelwind = false;
monitor_queue = monitor_q;
action_queue = action_q;
reel = reel_in;
levelWind = lw_in;
dibDriver = dibdriver;
configManager = cfgManager;
}
void MonitorMCB::InitializeSensors(void)
{
analogReadRes(12);
for (int i = 0; i < NUM_TEMP_SENSORS; i++) {
ltcManager.channel_assignments[temp_sensors[i].channel_number] = temp_sensors[i].sensor_type;
temp_sensors[i].sensor_error = true; // true until proven otherwise (affects TM averaging)
}
ltcManager.InitializeAndConfigure();
}
void MonitorMCB::UpdateLimits(void)
{
// temp sensor limits
temp_sensors[MTR1_THERM].limit_hi = configManager->mtr1_temp_lim.Read().hi;
temp_sensors[MTR1_THERM].limit_lo = configManager->mtr1_temp_lim.Read().lo;
temp_sensors[MTR2_THERM].limit_hi = configManager->mtr2_temp_lim.Read().hi;
temp_sensors[MTR2_THERM].limit_lo = configManager->mtr2_temp_lim.Read().lo;
temp_sensors[MC1_THERM].limit_hi = configManager->mc1_temp_lim.Read().hi;
temp_sensors[MC1_THERM].limit_lo = configManager->mc1_temp_lim.Read().lo;
temp_sensors[MC2_THERM].limit_hi = configManager->mc2_temp_lim.Read().hi;
temp_sensors[MC2_THERM].limit_lo = configManager->mc2_temp_lim.Read().lo;
temp_sensors[DCDC_THERM].limit_hi = configManager->dcdc_temp_lim.Read().hi;
temp_sensors[DCDC_THERM].limit_lo = configManager->dcdc_temp_lim.Read().lo;
temp_sensors[SPARE_THERM].limit_hi = configManager->spare_therm_lim.Read().hi;
temp_sensors[SPARE_THERM].limit_lo = configManager->spare_therm_lim.Read().lo;
// vmon limits
vmon_channels[VMON_3V3].limit_hi = configManager->vmon_3v3_lim.Read().hi;
vmon_channels[VMON_3V3].limit_lo = configManager->vmon_3v3_lim.Read().lo;
vmon_channels[VMON_15V].limit_hi = configManager->vmon_15v_lim.Read().hi;
vmon_channels[VMON_15V].limit_lo = configManager->vmon_15v_lim.Read().lo;
vmon_channels[VMON_20V].limit_hi = configManager->vmon_20v_lim.Read().hi;
vmon_channels[VMON_20V].limit_lo = configManager->vmon_20v_lim.Read().lo;
vmon_channels[VMON_SPOOL].limit_hi = configManager->vmon_spool_lim.Read().hi;
vmon_channels[VMON_SPOOL].limit_lo = configManager->vmon_spool_lim.Read().lo;
// imon limits
imon_channels[IMON_BRK].limit_hi = configManager->imon_brake_lim.Read().hi;
imon_channels[IMON_BRK].limit_lo = configManager->imon_brake_lim.Read().lo;
imon_channels[IMON_MC].limit_hi = configManager->imon_mc_lim.Read().hi;
imon_channels[IMON_MC].limit_lo = configManager->imon_mc_lim.Read().lo;
imon_channels[IMON_MTR1].limit_hi = configManager->imon_mtr1_lim.Read().hi;
imon_channels[IMON_MTR1].limit_lo = configManager->imon_mtr1_lim.Read().lo;
imon_channels[IMON_MTR2].limit_hi = configManager->imon_mtr2_lim.Read().hi;
imon_channels[IMON_MTR2].limit_lo = configManager->imon_mtr2_lim.Read().lo;
// torque limits
motor_torques[REEL_INDEX].limit_hi = configManager->reel_torque_lim.Read().hi;
motor_torques[REEL_INDEX].limit_lo = configManager->reel_torque_lim.Read().lo;
motor_torques[LEVEL_WIND_INDEX].limit_hi = configManager->lw_torque_lim.Read().hi;
motor_torques[LEVEL_WIND_INDEX].limit_lo = configManager->lw_torque_lim.Read().lo;
}
void MonitorMCB::Monitor(void)
{
bool limits_ok = true;
HandleCommands();
CheckVoltages(); // no active voltage limits
limits_ok &= CheckCurrents();
if (!monitor_low_power) {
limits_ok &= CheckTemperatures();
}
if (monitor_reel || monitor_levelwind) {
limits_ok &= CheckTorques();
UpdatePositions();
if (AggregateMotionData()) { // returns true if ready to send
SendMotionData();
}
}
if (!limits_ok) {
action_queue->Push(ACT_LIMIT_EXCEEDED); // notify state manager
}
}
void MonitorMCB::HandleCommands(void)
{
uint8_t command;
while (!monitor_queue->IsEmpty()) {
command = UNUSED_COMMAND;
if (!monitor_queue->Pop(&command)) {
return;
}
switch (command) {
case MONITOR_BOTH_MOTORS_ON:
monitor_reel = true;
monitor_levelwind = true;
if (monitor_low_power) {
ltcManager.WakeUp();
monitor_low_power = false;
}
break;
case MONITOR_REEL_ON:
monitor_reel = true;
monitor_levelwind = false;
if (monitor_low_power) {
ltcManager.WakeUp();
monitor_low_power = false;
}
break;
case MONITOR_MOTORS_OFF:
monitor_reel = false;
monitor_levelwind = false;
if (monitor_low_power) {
ltcManager.WakeUp();
monitor_low_power = false;
}
break;
case MONITOR_LOW_POWER:
ltcManager.Sleep();
monitor_low_power = true;
monitor_reel = false;
monitor_levelwind = false;
break;
case MONITOR_SEND_TEMPS:
if (monitor_low_power) {
dibDriver->dibComm.TX_Temperatures(LTC_POWERED_OFF, LTC_POWERED_OFF, LTC_POWERED_OFF, LTC_POWERED_OFF, LTC_POWERED_OFF, LTC_POWERED_OFF);
} else {
dibDriver->dibComm.TX_Temperatures(temp_sensors[0].last_temperature, temp_sensors[1].last_temperature, temp_sensors[2].last_temperature,
temp_sensors[3].last_temperature, temp_sensors[4].last_temperature, temp_sensors[5].last_temperature);
}
Serial.print("Temps: ");
Serial.print(temp_sensors[0].last_temperature); Serial.print(",");
Serial.print(temp_sensors[1].last_temperature); Serial.print(",");
Serial.print(temp_sensors[2].last_temperature); Serial.print(",");
Serial.print(temp_sensors[3].last_temperature); Serial.print(",");
Serial.print(temp_sensors[4].last_temperature); Serial.print(",");
Serial.println(temp_sensors[5].last_temperature);
break;
case MONITOR_SEND_VOLTS:
dibDriver->dibComm.TX_Voltages(vmon_channels[0].last_voltage, vmon_channels[1].last_voltage, vmon_channels[2].last_voltage, vmon_channels[3].last_voltage);
break;
case MONITOR_SEND_CURRS:
dibDriver->dibComm.TX_Currents(imon_channels[0].last_current, imon_channels[1].last_current, imon_channels[2].last_current, imon_channels[3].last_current);
break;
case UNUSED_COMMAND:
default:
storageManager.LogSD("Unknown monitor queue error", ERR_DATA);
break;
}
}
}
bool MonitorMCB::VerifyDeployVoltage(void)
{
return vmon_channels[VMON_15V].last_voltage > MIN_DEPLOY_VOLTAGE;
}
// measures one sensor at a time in a non-blocking fashion
bool MonitorMCB::CheckTemperatures(void)
{
static String temperature_string = "";
static uint8_t curr_sensor = 0;
static bool measurement_ongoing = false;
static uint32_t last_temp_log = 0;
bool limits_ok = true;
float temp = 0.0f;
if (!measurement_ongoing) {
ltcManager.StartMeasurement(temp_sensors[curr_sensor].channel_number);
measurement_ongoing = true;
} else if (ltcManager.FinishedMeasurement()) {
measurement_ongoing = false;
temp = ltcManager.ReadMeasurementResult(temp_sensors[curr_sensor].channel_number);
// validate reading, check limits if valid
if (temp != TEMPERATURE_ERROR && temp != LTC_SENSOR_ERROR) {
temp_sensors[curr_sensor].last_temperature = temp;
temp_sensors[curr_sensor].sensor_error = false;
if (temp > temp_sensors[curr_sensor].limit_hi) {
if (!temp_sensors[curr_sensor].over_temp) { // if newly over
storageManager.LogSD("Over temperature", ERR_DATA);
temp_sensors[curr_sensor].over_temp = true;
temp_sensors[curr_sensor].under_temp = false;
}
snprintf(limit_error, 100, "MCB Temp Limit, ch %u, t=%f", curr_sensor, temp);
limits_ok = false;
} else if (temp < temp_sensors[curr_sensor].limit_lo) {
if (!temp_sensors[curr_sensor].under_temp) { // if newly under
storageManager.LogSD("Under temperature", ERR_DATA);
temp_sensors[curr_sensor].over_temp = false;
temp_sensors[curr_sensor].under_temp = true;
}
snprintf(limit_error, 100, "MCB Temp Limit, ch %u, t=%f", curr_sensor, temp);
limits_ok = false;
} else {
temp_sensors[curr_sensor].over_temp = false;
temp_sensors[curr_sensor].under_temp = false;
}
} else {
temp_sensors[curr_sensor].sensor_error = true;
}
temperature_string += String(temp) + ",";
if (++curr_sensor == NUM_TEMP_SENSORS) {
curr_sensor = 0;
if (millis() > last_temp_log + TEMP_LOG_PERIOD) {
storageManager.LogSD(temperature_string, TEMP_DATA);
last_temp_log = millis();
}
temperature_string = "";
}
}
return limits_ok;
}
bool MonitorMCB::CheckVoltages(void)
{
static String voltage_string = "";
static uint8_t curr_channel = 0;
static uint32_t last_volt_log = 0;
bool limits_ok = true;
float raw = 0.0f;
// read channel
vmon_channels[curr_channel].last_raw = analogRead(vmon_channels[curr_channel].channel_pin);
raw = vmon_channels[curr_channel].last_raw;
// calculate voltage given the resistor divider network
vmon_channels[curr_channel].last_voltage = VREF * (raw / MAX_ADC_READ) / vmon_channels[curr_channel].voltage_divider;
// check limits
if (vmon_channels[curr_channel].last_voltage > vmon_channels[curr_channel].limit_hi) {
if (!vmon_channels[curr_channel].over_voltage) { // if newly over
storageManager.LogSD("Over voltage", ERR_DATA);
vmon_channels[curr_channel].over_voltage = true;
vmon_channels[curr_channel].under_voltage = false;
}
snprintf(limit_error, 100, "MCB Volt Limit, ch %u, V=%f", curr_channel, vmon_channels[curr_channel].last_voltage);
limits_ok = false;
} else if (vmon_channels[curr_channel].last_voltage < vmon_channels[curr_channel].limit_lo) {
if (!vmon_channels[curr_channel].under_voltage) { // if newly under
storageManager.LogSD("Under voltage", ERR_DATA);
vmon_channels[curr_channel].over_voltage = false;
vmon_channels[curr_channel].under_voltage = true;
}
snprintf(limit_error, 100, "MCB Volt Limit, ch %u, V=%f", curr_channel, vmon_channels[curr_channel].last_voltage);
limits_ok = false;
} else {
vmon_channels[curr_channel].over_voltage = false;
vmon_channels[curr_channel].under_voltage = false;
}
voltage_string += String(vmon_channels[curr_channel].last_voltage) + ",";
if (++curr_channel == NUM_VMON_CHANNELS) {
curr_channel = 0;
if (millis() > last_volt_log + VOLT_LOG_PERIOD) {
storageManager.LogSD(voltage_string, VMON_DATA);
last_volt_log = millis();
}
voltage_string = "";
}
return limits_ok;
}
bool MonitorMCB::CheckCurrents(void)
{
static String current_string = "";
static uint8_t curr_channel = 0;
static uint32_t last_curr_log = 0;
bool limits_ok = true;
float raw = 0.0f;
// read channel
imon_channels[curr_channel].last_raw = analogRead(imon_channels[curr_channel].channel_pin);
raw = imon_channels[curr_channel].last_raw;
// calculate load current from sense current pin voltage (very sensitive to constants)
imon_channels[curr_channel].last_current = SENSE_CURR_SLOPE *
((VREF / imon_channels[curr_channel].pulldown_res) * (raw / MAX_ADC_READ) - I_OFFSET);
// check limits
if (imon_channels[curr_channel].last_current > imon_channels[curr_channel].limit_hi) {
if (!imon_channels[curr_channel].over_current) { // if newly over
storageManager.LogSD("Over current", ERR_DATA);
Serial.print("OC channel: "); Serial.println(curr_channel);
Serial.println(imon_channels[curr_channel].last_current);
imon_channels[curr_channel].over_current = true;
imon_channels[curr_channel].under_current = false;
}
snprintf(limit_error, 100, "MCB Curr Limit, ch %u, I=%f", curr_channel, imon_channels[curr_channel].last_current);
limits_ok = false;
} else if (imon_channels[curr_channel].last_current < imon_channels[curr_channel].limit_lo) {
if (!imon_channels[curr_channel].under_current) { // if newly under
storageManager.LogSD("Under current", ERR_DATA);
imon_channels[curr_channel].over_current = false;
imon_channels[curr_channel].under_current = true;
}
snprintf(limit_error, 100, "MCB Curr Limit, ch %u, I=%f", curr_channel, imon_channels[curr_channel].last_current);
limits_ok = false;
} else {
imon_channels[curr_channel].over_current = false;
imon_channels[curr_channel].under_current = false;
}
current_string += String(imon_channels[curr_channel].last_current) + ",";
if (++curr_channel == NUM_IMON_CHANNELS) {
curr_channel = 0;
if (millis() > last_curr_log + CURR_LOG_PERIOD) {
storageManager.LogSD(current_string, IMON_DATA);
last_curr_log = millis();
}
current_string = "";
}
return limits_ok;
}
bool MonitorMCB::CheckTorques(void)
{
bool limits_ok = true;
String current_string = "";
if (monitor_reel) {
if (reel->ReadTorqueCurrent()) {
motor_torques[REEL_INDEX].read_error = false;
motor_torques[REEL_INDEX].last_torque = reel->torque_current / motor_torques[REEL_INDEX].conversion;
if (motor_torques[REEL_INDEX].last_torque > motor_torques[REEL_INDEX].limit_hi) {
if (!motor_torques[REEL_INDEX].over_torque) { // if newly over
storageManager.LogSD("Over torque, reel", ERR_DATA);
motor_torques[REEL_INDEX].over_torque = true;
motor_torques[REEL_INDEX].under_torque = false;
}
snprintf(limit_error, 100, "MCB Reel Torque Limit, t=%f", motor_torques[REEL_INDEX].last_torque);
limits_ok = false;
} else if (motor_torques[REEL_INDEX].last_torque < motor_torques[REEL_INDEX].limit_lo) {
if (!motor_torques[REEL_INDEX].under_torque) { // if newly under
storageManager.LogSD("Under torque, reel", ERR_DATA);
motor_torques[REEL_INDEX].over_torque = false;
motor_torques[REEL_INDEX].under_torque = true;
}
snprintf(limit_error, 100, "MCB Reel Torque Limit, t=%f", motor_torques[REEL_INDEX].last_torque);
limits_ok = false;
} else {
motor_torques[REEL_INDEX].over_torque = false;
motor_torques[REEL_INDEX].under_torque = false;
}
} else {
// TODO: error handling
motor_torques[REEL_INDEX].read_error = true;
}
}
if (monitor_levelwind) {
if (levelWind->ReadTorqueCurrent()) {
motor_torques[LEVEL_WIND_INDEX].read_error = false;
motor_torques[LEVEL_WIND_INDEX].last_torque = levelWind->torque_current / motor_torques[LEVEL_WIND_INDEX].conversion;
if (motor_torques[LEVEL_WIND_INDEX].last_torque > motor_torques[LEVEL_WIND_INDEX].limit_hi) {
if (!motor_torques[LEVEL_WIND_INDEX].over_torque) { // if newly over
storageManager.LogSD("Over torque, level wind", ERR_DATA);
motor_torques[LEVEL_WIND_INDEX].over_torque = true;
motor_torques[LEVEL_WIND_INDEX].under_torque = false;
}
snprintf(limit_error, 100, "MCB LW Torque Limit, t=%f", motor_torques[LEVEL_WIND_INDEX].last_torque);
limits_ok = false;
} else if (motor_torques[LEVEL_WIND_INDEX].last_torque < motor_torques[LEVEL_WIND_INDEX].limit_lo) {
if (!motor_torques[LEVEL_WIND_INDEX].under_torque) { // if newly under
storageManager.LogSD("Under torque, level wind", ERR_DATA);
motor_torques[LEVEL_WIND_INDEX].over_torque = false;
motor_torques[LEVEL_WIND_INDEX].under_torque = true;
}
snprintf(limit_error, 100, "MCB LW Torque Limit, t=%f", motor_torques[LEVEL_WIND_INDEX].last_torque);
limits_ok = false;
} else {
motor_torques[LEVEL_WIND_INDEX].over_torque = false;
motor_torques[LEVEL_WIND_INDEX].under_torque = false;
}
} else {
// TODO: error handling
motor_torques[LEVEL_WIND_INDEX].read_error = true;
}
}
return limits_ok;
}
void MonitorMCB::UpdatePositions(void)
{
if (monitor_reel && !reel->UpdatePosition()) {
storageManager.LogSD("Error updating reel position", ERR_DATA);
}
if (monitor_levelwind && !levelWind->UpdatePosition()) {
storageManager.LogSD("Error updating level wind position", ERR_DATA);
}
}
void MonitorMCB::PrintMotorData(void)
{
String data_string = "#data,";
data_string += String(reel->absolute_position / REEL_UNITS_PER_REV);
data_string += String(",");
data_string += String(levelWind->absolute_position);
data_string += String(",");
data_string += String(motor_torques[REEL_INDEX].last_torque);
data_string += String(",");
if (monitor_levelwind) {
data_string += String(motor_torques[LEVEL_WIND_INDEX].last_torque);
} else {
data_string += String("0");
}
data_string += String(",");
data_string += String(temp_sensors[1].last_temperature); // stepper motor temp
data_string += String(",");
data_string += String(temp_sensors[0].last_temperature); // brushless dc motor temp
Serial.println(data_string);
storageManager.LogSD(data_string, MOTION_DATA);
}
// called at about 1 Hz during motion, return true if data ready to send
bool MonitorMCB::AggregateMotionData(void)
{
// add the torques read from the MCs as long as they were valid
if (!motor_torques[REEL_INDEX].read_error) {
AddFastTM(&reel_torques, TorqueToUInt16(motor_torques[REEL_INDEX].last_torque));
}
if (!motor_torques[LEVEL_WIND_INDEX].read_error) {
AddFastTM(&lw_torques, TorqueToUInt16(motor_torques[LEVEL_WIND_INDEX].last_torque));
}
// add the motor currents (ADC read can't fail in software)
AddFastTM(&reel_currents, imon_channels[IMON_MTR1].last_raw);
AddFastTM(&lw_currents, imon_channels[IMON_MTR2].last_raw);
// add the slow param temperatures as long as they were valid
if (!temp_sensors[MTR1_THERM].sensor_error) {
AddSlowTM(PARAM_REEL_TEMP, TempToUInt16(temp_sensors[MTR1_THERM].last_temperature));
}
if (!temp_sensors[MTR2_THERM].sensor_error) {
AddSlowTM(PARAM_LW_TEMP, TempToUInt16(temp_sensors[MTR2_THERM].last_temperature));
}
if (!temp_sensors[MC1_THERM].sensor_error) {
AddSlowTM(PARAM_MC1_TEMP, TempToUInt16(temp_sensors[MC1_THERM].last_temperature));
}
if (!temp_sensors[MC2_THERM].sensor_error) {
AddSlowTM(PARAM_MC2_TEMP, TempToUInt16(temp_sensors[MC2_THERM].last_temperature));
}
// add the brake current and supply voltage (ADC read can't fail in software)
AddSlowTM(PARAM_BRAKE_CURR, imon_channels[IMON_BRK].last_raw);
AddSlowTM(PARAM_SUPPLY_VOLT, vmon_channels[VMON_15V].last_raw);
// check if ready to send
if (millis() > last_send_millis + 9500) {
last_send_millis = millis();
return true;
} else {
return false;
}
}
void MonitorMCB::SendMotionData(void)
{
uint16_t buffer_index = 0;
bool buffer_success = true;
// add the rotating parameter ID
buffer_success &= BufferAddUInt8(rotating_parameter, tm_buffer, MOTION_TM_SIZE, &buffer_index);
// add the rotating parameter values
buffer_success &= BufferAddUInt16(AverageResetSlowTM((RotatingParam_t) rotating_parameter), tm_buffer, MOTION_TM_SIZE, &buffer_index);
buffer_success &= BufferAddUInt16(slow_tm[rotating_parameter].running_max, tm_buffer, MOTION_TM_SIZE, &buffer_index);
// reset the rotating_parameter maximum and switch to the next parameter
slow_tm[rotating_parameter].running_max = 0;
rotating_parameter = (NUM_ROTATING_PARAMS == rotating_parameter + 1) ? FIRST_ROTATING_PARAM : rotating_parameter + 1;
// add the reel and level wind torques and currents
buffer_success &= BufferAddUInt16(AverageResetFastTM(&reel_torques), tm_buffer, MOTION_TM_SIZE, &buffer_index);
buffer_success &= BufferAddUInt16(reel_torques.running_max, tm_buffer, MOTION_TM_SIZE, &buffer_index);
buffer_success &= BufferAddUInt16(AverageResetFastTM(&lw_torques), tm_buffer, MOTION_TM_SIZE, &buffer_index);
buffer_success &= BufferAddUInt16(lw_torques.running_max, tm_buffer, MOTION_TM_SIZE, &buffer_index);
buffer_success &= BufferAddUInt16(AverageResetFastTM(&reel_currents), tm_buffer, MOTION_TM_SIZE, &buffer_index);
buffer_success &= BufferAddUInt16(reel_currents.running_max, tm_buffer, MOTION_TM_SIZE, &buffer_index);
buffer_success &= BufferAddUInt16(AverageResetFastTM(&lw_currents), tm_buffer, MOTION_TM_SIZE, &buffer_index);
buffer_success &= BufferAddUInt16(lw_currents.running_max, tm_buffer, MOTION_TM_SIZE, &buffer_index);
// reset the reel and level wind torque and current maxes as well as reel speed
reel_torques.running_max = 0;
lw_torques.running_max = 0;
reel_currents.running_max = 0;
lw_currents.running_max = 0;
// add the reel and lw positions
buffer_success &= BufferAddFloat(reel->absolute_position / REEL_UNITS_PER_REV, tm_buffer, MOTION_TM_SIZE, &buffer_index);
buffer_success &= BufferAddFloat(levelWind->absolute_position, tm_buffer, MOTION_TM_SIZE, &buffer_index);
// check for a good buffer and send it off
if (buffer_success && MOTION_TM_SIZE == buffer_index) {
dibDriver->dibComm.AssignBinaryTXBuffer(tm_buffer, MOTION_TM_SIZE, MOTION_TM_SIZE);
dibDriver->dibComm.TX_Bin(MCB_MOTION_TM);
Serial.println("Sent motion TM packet");
} else {
storageManager.LogSD("Error creating TM buffer", ERR_DATA);
}
}
void MonitorMCB::AddFastTM(Fast_TM_t * tm_type, uint16_t data)
{
// check the data vs the running maximum
if (data > tm_type->running_max) tm_type->running_max = data;
// make sure the averaging array isn't full
if (FAST_TM_MAX_SAMPLES == tm_type->curr_index) return;
// add the data to the array (index incremented externally for all fast TM)
tm_type->data[tm_type->curr_index++] = data;
}
void MonitorMCB::AddSlowTM(RotatingParam_t tm_index, uint16_t data)
{
// check the data vs the running maximum
if (data > slow_tm[tm_index].running_max) slow_tm[tm_index].running_max = data;
// make sure the averaging array isn't already full
if (SLOW_TM_MAX_SAMPLES == slow_tm[tm_index].curr_index) return;
// add the data to the array and increment the index
slow_tm[tm_index].data[slow_tm[tm_index].curr_index++] = data;
}
uint16_t MonitorMCB::AverageResetFastTM(Fast_TM_t * tm_type)
{
float result = 0;
// if no data points have been collected, return 0
if (0 == tm_type->curr_index) return 0;
// calculate the average
for (uint8_t i = 0; i < tm_type->curr_index; i++) {
result += tm_type->data[i];
}
result = result / (float) tm_type->curr_index;
// reset the data array
tm_type->curr_index = 0;
return (uint16_t) result;
}
uint16_t MonitorMCB::AverageResetSlowTM(RotatingParam_t tm_index)
{
float result = 0;
// if no data points have been collected, return 0
if (0 == slow_tm[tm_index].curr_index) return 0;
// calculate the average
for (uint8_t i = 0; i < slow_tm[tm_index].curr_index; i++) {
result += slow_tm[tm_index].data[i];
}
result = result / (float) slow_tm[tm_index].curr_index;
// reset the data array
slow_tm[tm_index].curr_index = 0;
return (uint16_t) result;
}
// turn float to uint16_t with resolution 0.1 N
uint16_t MonitorMCB::TorqueToUInt16(float torque)
{
torque *= 10.0f; // move the decimal to the right by one to capture tenths
torque += 30000; // add an offset to deal with negative torques
// ensure we're within the range of a uint16_t
if (UINT16_MAX <= torque) return UINT16_MAX;
if (0 >= torque) return 0;
return (uint16_t) torque;
}
// turn float into uint16_t with resolution 0.1 C
uint16_t MonitorMCB::TempToUInt16(float temp)
{
temp *= 10.0f; // move the decimal to the right by one to capture tenths
temp += 30000; // add an offset to deal with negative temperatures
// ensure we're within the range of a uint16_t
if (UINT16_MAX <= temp) return UINT16_MAX;
if (0 >= temp) return 0;
return (uint16_t) temp;
}