-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathhardware.cpp
401 lines (327 loc) · 9.88 KB
/
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
#include "hardware.h"
// extern
bool _is_lcd_present(){
pinModeOutput(LCD_PINS_RS);
digitalWriteExt(LCD_PINS_RS, LOW);
pinModeInput(LCD_PINS_RS);
bool _lcd_present = digitalReadExt(LCD_PINS_RS);
pinModeOutput(LCD_PINS_RS);
digitalWriteExt(LCD_PINS_RS, LOW);
return _lcd_present;
}
int8_t enc_diff = 0;
uint8_t enc_click = 0;
volatile uint32_t beeper_off_at = 0;
bool read_temperature = true;
uint8_t temperature_samples_collected = 0;
float temperature_raw[THERMISTOR_CNT] = {0};
float temperature[THERMISTOR_CNT] = {0};
bool read_voltage = true;
uint8_t voltage_samples_collected = 0;
float voltage_raw[VOLTAGE_ADC_CNT] = {0};
float voltage[VOLTAGE_ADC_CNT] = {0};
uint32_t last_lcd_reinit = 0;
const bool lcd_present = _is_lcd_present();
const uint8_t temperature_samples_total = 64;
const uint8_t voltage_samples_total = 16;
#define INIT_MOTOR(i, d, n, j, a) Motor(d##_STEP_PIN, d##_DIR_PIN, d##_ENABLE_PIN, d##_TMC2130_CS, d##_TMC2130_DIAG, &PORTC, PINC##i, &PORTL, PINL##j, &OCR##n##A, &TCNT##n, &TIMSK##n, OCIE##n##A, a)
Motor motors[] = {
INIT_MOTOR(0, X, 1, 0, 'x'),
INIT_MOTOR(1, Y, 3, 1, 'y'),
INIT_MOTOR(2, Z, 4, 2, 'z'),
INIT_MOTOR(3, E0, 5, 6, 'e'),
};
#undef INIT_MOTOR
const uint8_t Back[8] PROGMEM = {
B00100,
B01110,
B11111,
B00100,
B11100,
B00000,
B00000,
B00000
};
const uint8_t Right[8] PROGMEM = {
B00000,
B00100,
B00010,
B11111,
B00010,
B00100,
B00000,
B00000
};
const uint8_t Backslash[8] PROGMEM = {
B00000,
B10000,
B01000,
B00100,
B00010,
B00001,
B00000,
B00000
};
const uint8_t Play[8] PROGMEM = {
B00000,
B01000,
B01100,
B01110,
B01100,
B01000,
B00000,
B00000
};
const uint8_t Stop[8] PROGMEM = {
B00000,
B10001,
B01010,
B00100,
B01010,
B10001,
B00000,
B00000
};
void setupPins(){
// encoder
pinModeInput(BTN_EN1, true);
pinModeInput(BTN_EN2, true);
pinModeInput(BTN_ENC, true);
// thermistors
pinModeInput(TEMP_0_PIN);
pinModeInput(TEMP_1_PIN);
pinModeInput(TEMP_2_PIN);
pinModeInput(TEMP_PINDA_PIN);
pinModeInput(TEMP_AMBIENT_PIN);
// power
pinModeOutput(HEATER_BED_PIN);
digitalWriteExt(HEATER_BED_PIN, LOW);
pinModeOutput(HEATER_0_PIN);
digitalWriteExt(HEATER_0_PIN, LOW);
// misc
pinModeInput(MISO, true);
pinModeOutput(BEEPER);
}
void setupLcd(){
lcd.setBrightness(128);
lcd.clear();
lcd.createChar(0, Backslash);
lcd.createChar(1, Back);
lcd.createChar(2, Right);
lcd.createChar(3, Play);
lcd.createChar(4, Stop);
}
void setupMotors(){
for(size_t i = 0; i < MOTORS_MAX; i++) {
motors[i].driver.begin();
motors[i].driver.toff(2);
motors[i].driver.blank_time(24);
motors[i].driver.rms_current(300); // mA
// motors[i].driver.TCOOLTHRS(0xFFFFF); // 20bit max
motors[i].driver.THIGH(0);
motors[i].driver.semin(5);
motors[i].driver.semax(2);
motors[i].driver.sedn(0b01);
motors[i].driver.sgt(2);
motors[i].driver.en_pwm_mode(1); // Enable extremely quiet stepping
motors[i].driver.pwm_autoscale(1);
motors[i].driver.intpol(true);
motors[i].driver.diag0_stall(true);
motors[i].driver.diag1_stall(true);
motors[i].driver.diag0_int_pushpull(true);
motors[i].driver.diag1_pushpull(true);
// motors[i].driver.en_pwm_mode(false);
motors[i].driver.TCOOLTHRS(460);
motors[i].microsteps(motors[i].usteps);
motors[i].dir(false);
motors[i].rpm(60.0);
}
setupMotorTimers();
}
// shamelessly stolen from CW1 FW
void readEncoder() {
static int8_t rotary_diff = 0;
static uint8_t lcd_encoder_bits = 0;
static uint8_t prev_click_state = 0;
static uint32_t last_click = 0;
uint8_t enc = 0;
uint8_t click = !digitalReadExt(BTN_ENC);
if (digitalReadExt(BTN_EN1) == HIGH) {
enc |= B01;
}
if (digitalReadExt(BTN_EN2) == HIGH) {
enc |= B10;
}
if (enc != lcd_encoder_bits) {
switch (enc) {
case 0:
if (lcd_encoder_bits == 1) {
rotary_diff++;
} else if (lcd_encoder_bits == 2) {
rotary_diff--;
}
break;
case 2:
if (lcd_encoder_bits == 0) {
rotary_diff++;
} else if (lcd_encoder_bits == 3) {
rotary_diff--;
}
break;
case 3:
if (lcd_encoder_bits == 2) {
rotary_diff++;
} else if (lcd_encoder_bits == 1) {
rotary_diff--;
}
break;
case 1:
if (lcd_encoder_bits == 3) {
rotary_diff++;
} else if (lcd_encoder_bits == 0) {
rotary_diff--;
}
break;
}
lcd_encoder_bits = enc;
if (rotary_diff > 124)
rotary_diff = 124;
else if (rotary_diff < -124)
rotary_diff = -124;
if (rotary_diff > 3) {
rotary_diff -= 4;
enc_diff += 1;
} else if (rotary_diff < -3) {
rotary_diff += 4;
enc_diff -= 1;
}
}
if(click != prev_click_state){
uint32_t _millis = millis();
if(!click) enc_click = _millis - last_click > 350 ? 2 : 1;
last_click = _millis;
prev_click_state = click;
}
}
float _analog_to_temp(int raw){
float celsius = 0;
byte i;
for (i=1; i<temperature_table_einsy_len; i++){
if ((short)pgm_read_word(&temperature_table_einsy[i][0]) > raw){
celsius = (short)pgm_read_word(&temperature_table_einsy[i-1][1]) +
(raw - (short)pgm_read_word(&temperature_table_einsy[i-1][0])) *
(float)((short)pgm_read_word(&temperature_table_einsy[i][1]) - (short)pgm_read_word(&temperature_table_einsy[i-1][1])) /
(float)((short)pgm_read_word(&temperature_table_einsy[i][0]) - (short)pgm_read_word(&temperature_table_einsy[i-1][0]));
break;
}
}
if (i == temperature_table_einsy_len) celsius = (short)pgm_read_word(&temperature_table_einsy[i-1][1]);
const float _offset = 10;
const float _offset_center = 50;
const float _offset_start = 40;
const float _first_koef = (_offset / 2) / (_offset_center - _offset_start);
const float _second_koef = (_offset / 2) / (100 - _offset_center);
if (celsius >= _offset_start && celsius <= _offset_center){
celsius = celsius + (_first_koef * (celsius - _offset_start));
}else if (celsius > _offset_center && celsius <= 100){
celsius = celsius + (_first_koef * (_offset_center - _offset_start)) + (_second_koef * (celsius - (100 - _offset_center)));
}else if (celsius > 100){
celsius = celsius + _offset;
}
return celsius;
}
float _analog_to_temp_ambient(int raw){
float celsius = 0;
byte i;
for (i=1; i<temperature_table_einsy_ambient_len; i++){
if ((short)pgm_read_word(&temperature_table_einsy_ambient[i][0]) > raw){
celsius = (short)pgm_read_word(&temperature_table_einsy_ambient[i-1][1]) +
(raw - (short)pgm_read_word(&temperature_table_einsy_ambient[i-1][0])) *
(float)((short)pgm_read_word(&temperature_table_einsy_ambient[i][1]) - (short)pgm_read_word(&temperature_table_einsy_ambient[i-1][1])) /
(float)((short)pgm_read_word(&temperature_table_einsy_ambient[i][0]) - (short)pgm_read_word(&temperature_table_einsy_ambient[i-1][0]));
break;
}
}
if (i == temperature_table_einsy_ambient_len) celsius = (short)pgm_read_word(&temperature_table_einsy_ambient[i-1][1]);
return celsius;
}
void readThermistors(){
for (size_t i = 0; i < THERMISTOR_CNT; i++) {
uint16_t uval;
switch (i) {
case 0: uval = analogRead(TEMP_0_PIN); break;
case 1: uval = analogRead(TEMP_1_PIN); break;
case 2: uval = analogRead(TEMP_2_PIN); break;
case 3: uval = analogRead(TEMP_PINDA_PIN); break;
case 4: uval = analogRead(TEMP_AMBIENT_PIN); break;
}
if(temperature_samples_collected < temperature_samples_total){
temperature_raw[i] = ((temperature_raw[i] * temperature_samples_collected) + (float)uval) / (temperature_samples_collected + 1);
}else{
temperature_raw[i] = ((temperature_raw[i] * (temperature_samples_collected - 1)) + (float)uval) / temperature_samples_collected;
}
if(i == 4) temperature[i] = _analog_to_temp_ambient(temperature_raw[i]);
else temperature[i] = _analog_to_temp(temperature_raw[i]);
}
if(temperature_samples_collected < temperature_samples_total) temperature_samples_collected++;
}
#define VOLT_DIV_REF 5
#define VOLT_DIV_R1 10000
#define VOLT_DIV_R2 2370
#define VOLT_DIV_FAC ((float)VOLT_DIV_R2 / (VOLT_DIV_R2 + VOLT_DIV_R1))
void readVoltages(){
for (size_t i = 0; i < VOLTAGE_ADC_CNT; i++) {
uint16_t uval;
switch (i) {
case 0: uval = analogRead(VOLT_PWR_PIN); break;
case 1: uval = analogRead(VOLT_BED_PIN); break;
case 2: uval = analogRead(VOLT_IR_PIN); break;
}
if(voltage_samples_collected < voltage_samples_total){
voltage_raw[i] = ((voltage_raw[i] * voltage_samples_collected) + (float)uval) / (voltage_samples_collected + 1);
}else{
voltage_raw[i] = ((voltage_raw[i] * (voltage_samples_collected - 1)) + (float)uval) / voltage_samples_collected;
}
if(i == 2) voltage[i] = VOLT_DIV_REF * voltage_raw[i] / 1023;
else voltage[i] = VOLT_DIV_REF * voltage_raw[i] / 1023 / VOLT_DIV_FAC;
}
if(voltage_samples_collected < voltage_samples_total) voltage_samples_collected++;
}
void beep(uint16_t duration){
digitalWriteExt(BEEPER, HIGH);
beeper_off_at = millis() + duration;
}
uint32_t float_as_uint32(float value){
return *reinterpret_cast<uint32_t*>(&value);
}
float uint32_as_float(uint32_t value){
return *reinterpret_cast<float*>(&value);
}
void store_float_to_uint32(uint32_t *target, const float value){
float* view = reinterpret_cast<float*>(*&target);
*view = value;
}
float read_float_from_uint32(uint32_t *source){
float* view = reinterpret_cast<float*>(*&source);
return *view;
}
const char* read_pgm_string(const char* ptr){
const uint8_t buf_len = 21;
static char buffer[buf_len];
size_t len = 0;
memset(buffer, 0, buf_len);
while(1){
if((buffer[len++] = pgm_read_byte(ptr++)) == 0) break;
else if(len >= buf_len - 1) break;
}
return buffer;
}
uint8_t read_pgm_string(const char* ptr, char* buffer, uint8_t buf_len){
size_t len = 0;
memset(buffer, 0, buf_len);
while(1){
if((buffer[len++] = pgm_read_byte(ptr++)) == 0) break;
else if(len >= buf_len - 1) break;
}
return len;
}