/
0_hardware.cpp
468 lines (385 loc) · 13.9 KB
/
0_hardware.cpp
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
/*
* hardware.cpp - general hardware support functions
* For: /board/gQuintic
* This file is part of the g2core project
*
* Copyright (c) 2010 - 2015 Alden S. Hart, Jr.
*
* This file ("the software") is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License, version 2 as published by the
* Free Software Foundation. You should have received a copy of the GNU General Public
* License, version 2 along with the software. If not, see <http://www.gnu.org/licenses/>.
*
* As a special exception, you may use this file as part of a software library without
* restriction. Specifically, if other files instantiate templates or use macros or
* inline functions from this file, or you compile this file and link it with other
* files to produce an executable, this file does not by itself cause the resulting
* executable to be covered by the GNU General Public License. This exception does not
* however invalidate any other reasons why the executable file might be covered by the
* GNU General Public License.
*
* THE SOFTWARE IS DISTRIBUTED IN THE HOPE THAT IT WILL BE USEFUL, BUT WITHOUT ANY
* 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 "g2core.h" // #1
#include "config.h" // #2
#include "hardware.h"
#include "controller.h"
#include "text_parser.h"
#include "board_xio.h"
#include "board_gpio.h"
#include "safety_manager.h"
#include "kinematics.h" // for get_flow_volume
#include "MotateUtilities.h"
#include "MotateUniqueID.h"
#include "MotatePower.h"
#include "settings.h"
//Motate::ClockOutputPin<Motate::kExternalClock1_PinNumber> external_clk_pin {16000000}; // 16MHz optimally
Motate::OutputPin<Motate::kExternalClock1_PinNumber> external_clk_pin {Motate::kStartLow};
HOT_DATA SPI_CS_PinMux_used_t spiCSPinMux;
HOT_DATA SPIBus_used_t spiBus;
HOT_DATA TWIBus_used_t twiBus;
// // Define Multiplexers
// HOT_DATA plex0_t plex0{twiBus, 0x0070L};
// HOT_DATA plex1_t plex1{twiBus, 0x0071L};
// #include "i2c_eeprom.h"
// HOT_DATA I2C_EEPROM eeprom{twiBus, 0b01010000};
// alignas(4) uint8_t eeprom_buffer[128] HOT_DATA = "TestinglyABCDEFGHIJKLmnop";
// alignas(4) uint8_t eeprom_in_buffer[128] HOT_DATA = "";
SafetyManager sm{};
SafetyManager *safety_manager = &sm;
#ifndef SPINDLE_ENABLE_OUTPUT_NUMBER
#warning SPINDLE_ENABLE_OUTPUT_NUMBER is defaulted to 4!
#warning SPINDLE_ENABLE_OUTPUT_NUMBER should be defined in settings or a board file!
#define SPINDLE_ENABLE_OUTPUT_NUMBER 4
#endif
#ifndef SPINDLE_DIRECTION_OUTPUT_NUMBER
#warning SPINDLE_DIRECTION_OUTPUT_NUMBER is defaulted to 5!
#warning SPINDLE_DIRECTION_OUTPUT_NUMBER should be defined in settings or a board file!
#define SPINDLE_DIRECTION_OUTPUT_NUMBER 5
#endif
#ifndef SPINDLE_PWM_NUMBER
#warning SPINDLE_PWM_NUMBER is defaulted to 6!
#warning SPINDLE_PWM_NUMBER should be defined in settings or a board file!
#define SPINDLE_PWM_NUMBER 6
#endif
#ifndef SPINDLE_SPEED_CHANGE_PER_MS
#warning SPINDLE_SPEED_CHANGE_PER_MS is defaulted to 5!
#warning SPINDLE_SPEED_CHANGE_PER_MS should be defined in settings or a board file!
#define SPINDLE_SPEED_CHANGE_PER_MS 5
#endif
#include "esc_spindle.h"
ESCSpindle esc_spindle {SPINDLE_PWM_NUMBER, SPINDLE_ENABLE_OUTPUT_NUMBER, SPINDLE_DIRECTION_OUTPUT_NUMBER, SPINDLE_SPEED_CHANGE_PER_MS};
#if HAS_LASER
#ifndef LASER_ENABLE_OUTPUT_NUMBER
#error LASER_ENABLE_OUTPUT_NUMBER should be defined in settings or a board file!
#endif
#ifndef LASER_FIRE_PIN_NUMBER
#error LASER_FIRE_PIN_NUMBER should be defined in settings or a board file!
#endif
#include "laser_toolhead.h"
HOT_DATA LaserTool_used_t laser_tool {LASER_ENABLE_OUTPUT_NUMBER, MOTOR_6};
// CartesianKinematics<AXES, MOTORS> cartesian_kinematics;
KinematicsBase<AXES, MOTORS> *kn = &laser_tool;
#endif
ToolHead *toolhead_for_tool(uint8_t tool) {
#if !HAS_LASER
return &esc_spindle;
#else
if (tool != LASER_TOOL) {
return &esc_spindle;
} else {
return &laser_tool;
}
#endif
}
/*
* hardware_init() - lowest level hardware init
*/
void hardware_init()
{
spiBus.init();
twiBus.init();
board_hardware_init();
external_clk_pin = 0; // Force external clock to 0 for now.
esc_spindle.init();
#if HAS_LASER
laser_tool.init();
#endif
spindle_set_toolhead(toolhead_for_tool(1));
}
/*
* hardware_periodic() - callback from the controller loop - TIME CRITICAL.
*/
// previous values of analog voltages
#if TEMPERATURE_OUTPUT_ON
float ai_vv[A_IN_CHANNELS];
const float analog_change_threshold = 0.01;
#endif
float angle_0 = 0.0;
float angle_1 = 0.0;
#if HAS_PRESSURE
float pressure = 0;
float pressure_threshold = 0.01;
float flow = 0;
float flow_threshold = 0.01;
#endif
// void read_encoder_0(bool worked /* = false*/, float angle /* = 0.0*/) {
// if (worked) {
// angle_0 = angle;
// }
// encoder_0.getAngleFraction();
// }
// void read_encoder_1(bool worked /* = false*/, float angle /* = 0.0*/) {
// if (worked) {
// angle_1 = angle;
// }
// encoder_1.getAngleFraction();
// }
// Following is for testing internally
// void first_part(bool worked = false);
// void second_part(bool worked = false);
// void third_part(bool worked = false);
// void first_part (bool worked /* = false*/ ) {
// if (!worked) {
// eeprom.store(0, eeprom_buffer, 4, first_part);
// } else {
// second_part();
// }
// }
// void second_part (bool worked /* = false*/ ) {
// if (!worked) {
// eeprom.store(4, eeprom_buffer + 4, 5, second_part);
// } else {
// third_part();
// }
// }
// void third_part(bool worked /* = false*/ ) {
// if (!worked) {
// eeprom.load(0, eeprom_in_buffer, 9, third_part);
// } else {
// // read_encoder_0();
// }
// }
stat_t hardware_periodic()
{
// for all of the analog inputs that are enabled, request status reports
// when they change beyond the threshold
#if TEMPERATURE_OUTPUT_ON
for (uint8_t a = 0; a < A_IN_CHANNELS; a++) {
if (a_in[a]->getEnabled() == IO_ENABLED) {
float new_vv = a_in[a]->getValue();
if (std::abs(ai_vv[a] - new_vv) >= analog_change_threshold) {
ai_vv[a] = new_vv; // only record if goes past threshold!
sr_request_status_report(SR_REQUEST_TIMED);
}
}
}
#endif
#if HAS_PRESSURE
float new_pressure = pressure_sensor1.getPressure(PressureUnits::cmH2O);
if (std::abs(pressure - new_pressure) >= pressure_threshold) {
pressure = new_pressure; // only record if goes past threshold!
sr_request_status_report(SR_REQUEST_TIMED);
}
float new_flow = flow_sensor1.getFlow(FlowUnits::SLM);
if (std::abs(flow - new_flow) >= flow_threshold) {
flow = new_flow; // only record if goes past threshold!
sr_request_status_report(SR_REQUEST_TIMED);
}
#endif
// static uint8_t sent = 0;
// if (!sent) {
// first_part(false);
// sent = 2;
// }
return STAT_OK;
}
/*
* hw_hard_reset() - reset system now
* hw_flash_loader() - enter flash loader to reflash board
*/
void hw_hard_reset(void)
{
Motate::System::reset(/*boootloader: */ false); // arg=0 resets the system
}
void hw_flash_loader(void)
{
Motate::System::reset(/*boootloader: */ true); // arg=1 erases FLASH and enters FLASH loader
}
/*
* _get_id() - get a human readable signature
*
* Produce a unique deviceID based on the factory calibration data.
* Truncate to SYS_ID_DIGITS length
*/
void _get_id(char *id)
{
char *p = id;
const char *uuid = Motate::UUID;
Motate::strncpy(p, uuid, strlen(uuid));
}
/***** END OF SYSTEM FUNCTIONS *****/
/***********************************************************************************
* CONFIGURATION AND INTERFACE FUNCTIONS
* Functions to get and set variables from the cfgArray table
***********************************************************************************/
/*
* hw_get_fb() - get firmware build number
* hw_get_fv() - get firmware version number
* hw_get_hp() - get hardware platform string
* hw_get_hv() - get hardware version string
* hw_get_fbs() - get firmware build string
*/
stat_t hw_get_fb(nvObj_t *nv) { return (get_float(nv, cs.fw_build)); }
stat_t hw_get_fv(nvObj_t *nv) { return (get_float(nv, cs.fw_version)); }
stat_t hw_get_hp(nvObj_t *nv) { return (get_string(nv, G2CORE_HARDWARE_PLATFORM)); }
stat_t hw_get_hv(nvObj_t *nv) { return (get_string(nv, G2CORE_HARDWARE_VERSION)); }
stat_t hw_get_fbs(nvObj_t *nv) { return (get_string(nv, G2CORE_FIRMWARE_BUILD_STRING)); }
/*
* hw_get_fbc() - get configuration settings file
*/
stat_t hw_get_fbc(nvObj_t *nv)
{
nv->valuetype = TYPE_STRING;
#ifdef SETTINGS_FILE
#define settings_file_string1(s) #s
#define settings_file_string2(s) settings_file_string1(s)
ritorno(nv_copy_string(nv, settings_file_string2(SETTINGS_FILE)));
#undef settings_file_string1
#undef settings_file_string2
#else
ritorno(nv_copy_string(nv, "<default-settings>"));
#endif
return (STAT_OK);
}
/*
* hw_get_id() - get device ID (signature)
*/
stat_t hw_get_id(nvObj_t *nv)
{
char tmp[SYS_ID_LEN];
_get_id(tmp);
nv->valuetype = TYPE_STRING;
ritorno(nv_copy_string(nv, tmp));
return (STAT_OK);
}
/*
* hw_flash() - invoke FLASH loader from command input
*/
stat_t hw_flash(nvObj_t *nv)
{
hw_flash_loader();
return(STAT_OK);
}
#if HAS_PRESSURE
stat_t get_pressure(nvObj_t *nv)
{
nv->value_flt = pressure_sensor1.getPressure(PressureUnits::cmH2O);
nv->valuetype = TYPE_FLOAT;
return (STAT_OK);
}
stat_t get_flow(nvObj_t *nv)
{
nv->value_flt = flow_sensor1.getFlow(FlowUnits::SLM);
nv->valuetype = TYPE_FLOAT;
return (STAT_OK);
}
stat_t get_flow_pressure(nvObj_t *nv)
{
nv->value_flt = flow_sensor1.getPressure(PressureUnits::cmH2O);
nv->valuetype = TYPE_FLOAT;
return (STAT_OK);
}
constexpr cfgItem_t sys_config_items_3[] = {
{ "prs","prs1", _f0, 5, tx_print_nul, get_pressure, set_nul, nullptr, 0 },
{ "flow1","flow1prs", _f0, 5, tx_print_nul, get_flow_pressure, set_nul, nullptr, 0 },
{ "flow1","flow1slm", _f0, 5, tx_print_nul, get_flow, set_nul, nullptr, 0 },
{ "flow1","flow1vol", _f0, 5, tx_print_nul, get_flow_volume, set_nul, nullptr, 0 },
};
constexpr cfgSubtableFromStaticArray sys_config_3{sys_config_items_3};
#elif HAS_LASER
stat_t set_pulse_duration(nvObj_t *nv)
{
laser_tool.set_pulse_duration_us(nv->valuetype == TYPE_FLOAT ? nv->value_flt : nv->value_int);
return (STAT_OK);
}
stat_t get_pulse_duration(nvObj_t *nv)
{
nv->value_int = laser_tool.get_pulse_duration_us();
nv->valuetype = TYPE_INTEGER;
return (STAT_OK);
}
stat_t get_min_s(nvObj_t *nv) {
nv->value_flt = laser_tool.get_min_s();
nv->valuetype = TYPE_FLOAT;
return (STAT_OK);
}
stat_t set_min_s(nvObj_t *nv) {
laser_tool.set_min_s(nv->value_flt);
return (STAT_OK);
}
stat_t get_max_s(nvObj_t *nv) {
nv->value_flt = laser_tool.get_max_s();
nv->valuetype = TYPE_FLOAT;
return (STAT_OK);
}
stat_t set_max_s(nvObj_t *nv) {
laser_tool.set_max_s(nv->value_flt);
return (STAT_OK);
}
stat_t get_min_ppm(nvObj_t *nv) {
nv->value_flt = laser_tool.get_min_ppm();
nv->valuetype = TYPE_FLOAT;
return (STAT_OK);
}
stat_t set_min_ppm(nvObj_t *nv) {
laser_tool.set_min_ppm(nv->value_flt);
return (STAT_OK);
}
stat_t get_max_ppm(nvObj_t *nv) {
nv->value_flt = laser_tool.get_max_ppm();
nv->valuetype = TYPE_FLOAT;
return (STAT_OK);
}
stat_t set_max_ppm(nvObj_t *nv) {
laser_tool.set_max_ppm(nv->value_flt);
return (STAT_OK);
}
constexpr cfgItem_t sys_config_items_3[] = {
{ "th2","th2pd", _iip, 0, tx_print_nul, get_pulse_duration, set_pulse_duration, nullptr, LASER_PULSE_DURATION },
{ "th2","th2mns", _fip, 0, tx_print_nul, get_min_s, set_min_s, nullptr, LASER_MIN_S },
{ "th2","th2mxs", _fip, 0, tx_print_nul, get_max_s, set_max_s, nullptr, LASER_MAX_S },
{ "th2","th2mnp", _fip, 0, tx_print_nul, get_min_ppm, set_min_ppm, nullptr, LASER_MIN_PPM },
{ "th2","th2mxp", _fip, 0, tx_print_nul, get_max_ppm, set_max_ppm, nullptr, LASER_MAX_PPM },
};
constexpr cfgSubtableFromStaticArray sys_config_3{sys_config_items_3};
#else // !HAS_LASER && !HAS_PRESSURE
// Stub in getSysConfig_3
constexpr cfgSubtableFromStaticArray sys_config_3{};
#endif
const configSubtable * const getSysConfig_3() { return &sys_config_3; }
/***********************************************************************************
* TEXT MODE SUPPORT
* Functions to print variables from the cfgArray table
***********************************************************************************/
#ifdef __TEXT_MODE
static const char fmt_fb[] = "[fb] firmware build%18.2f\n";
static const char fmt_fv[] = "[fv] firmware version%16.2f\n";
static const char fmt_fbs[] = "[fbs] firmware build%34s\n";
static const char fmt_fbc[] = "[fbc] firmware config%33s\n";
static const char fmt_hp[] = "[hp] hardware platform%15s\n";
static const char fmt_hv[] = "[hv] hardware version%13s\n";
static const char fmt_id[] = "[id] g2core ID%37s\n";
void hw_print_fb(nvObj_t *nv) { text_print(nv, fmt_fb);} // TYPE_FLOAT
void hw_print_fv(nvObj_t *nv) { text_print(nv, fmt_fv);} // TYPE_FLOAT
void hw_print_fbs(nvObj_t *nv) { text_print(nv, fmt_fbs);} // TYPE_STRING
void hw_print_fbc(nvObj_t *nv) { text_print(nv, fmt_fbc);} // TYPE_STRING
void hw_print_hp(nvObj_t *nv) { text_print(nv, fmt_hp);} // TYPE_STRING
void hw_print_hv(nvObj_t *nv) { text_print(nv, fmt_hv);} // TYPE_STRING
void hw_print_id(nvObj_t *nv) { text_print(nv, fmt_id);} // TYPE_STRING
#endif //__TEXT_MODE