-
Notifications
You must be signed in to change notification settings - Fork 1
/
einsy_control.ino
131 lines (103 loc) · 3.15 KB
/
einsy_control.ino
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
#include "pins.h"
#include "hardware.h"
#include "serial.h"
#include "permanent_storage.h"
#include "menu_system_derivates.h"
#include "menus.h"
#include "custom.h"
uint16_t print_stallguard_to_serial_interval = 30;
uint32_t last_stallguard_print_to_serial = 0;
void printBinary(byte inByte) { for (int b = 7; b >= 0; b--) Serial.print(bitRead(inByte, b)); Serial.println(); }
#define _PRINT_BIN(v) #v
#define PRINT_BIN(v) Serial.print(F(#v "\t= ")); printBinary(v);
void setup() {
Serial.begin(74880);
Serial.setTimeout(100);
Serial.println(F("init..."));
#ifdef CUSTOM
Serial.println(F("custom_variant: " CUSTOM));
#endif
setupPins();
SPI.begin();
storage.load();
setupLcd();
setupMotors();
current_menu = &main_menu;
current_menu->draw();
menu_motor_stallguard_value.redraw_interval = 50;
menu_motor_position_speed.redraw_interval = 50;
menu_motor_position_direction.redraw_interval = 50;
#ifdef CUSTOM_SETUP
setupCustom();
#endif
#ifdef DEBUG_SERIAL
Serial1.begin(115200);
#endif
#ifdef DEBUG_PRINT
Serial2.begin(115200);
#endif
Serial.println(F("ready!"));
}
void loop() {
uint32_t _millis = millis();
current_menu->loop();
if(enc_diff){
current_menu->move(enc_diff);
enc_diff = 0;
}
if(enc_click){
current_menu->on_press(enc_click);
enc_click = 0;
}
if(current_menu->redraw_interval > 0 && _millis > last_menu_redraw + current_menu->redraw_interval) current_menu->draw(false);
handleSerial();
if(_millis >= last_stallguard_print_to_serial + print_stallguard_to_serial_interval){
bool printed_something = false;
for(size_t i = 0; i < MOTORS_MAX; i++){
if(motors[i].print_stallguard_to_serial){
printed_something = true;
MotorStallguardInfo stallguard_info = motors[i].get_stallguard_info();
// Serial.print("M");
// Serial.print(i);
// Serial.print("\t");
Serial.print(stallguard_info.sg_result);
Serial.print("\t");
// Serial.print(stallguard_info.fsactive);
// Serial.print("\t");
// Serial.print(stallguard_info.cs_actual);
// Serial.print("\t");
Serial.print(stallguard_info.rms);
Serial.print("mA\t");
// Serial.println();
}
}
if(printed_something) Serial.println();
last_stallguard_print_to_serial = _millis;
}
#ifndef DISABLE_STALLGUARD_TRIGGERED_PRINT_TO_SERIAL
for(size_t i = 0; i < MOTORS_MAX; i++){
if(motors[i].stallguard_triggered){
motors[i].stallguard_triggered = false;
const float rpm = motors[i].rpm();
const uint32_t lost_steps = motors[i].driver.LOST_STEPS();
Serial.print(F("Motor "));
Serial.print("XYZE"[i]);
Serial.print(F(" stalled at "));
Serial.print(rpm);
Serial.print(F(" RPM with "));
Serial.print(lost_steps);
Serial.println(F(" lost steps!"));
}
}
#endif // DISABLE_STALLGUARD_TRIGGERED_PRINT_TO_SERIAL
if(_millis > last_lcd_reinit + 5000){
lcd.reinit();
current_menu->draw();
last_lcd_reinit = _millis;
}
if(read_temperature) readThermistors();
if(read_voltage) readVoltages();
#ifdef CUSTOM_LOOP
loopCustom();
#endif
}