-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.cpp
148 lines (121 loc) · 4.27 KB
/
main.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
/**
* @file ilet-103-motor-controller.ino
* @author Fern Lane
* @brief Digital speed controller for ILET 103 reel-to-reel deck
*
* @copyright Copyright (c) 2024 Fern Lane
*
* This file is part of the ilet-103-motor-controller distribution.
* See <https://github.com/F33RNI/ilet-103-motor-controller> for more info.
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* long with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include <Arduino.h>
// For official Arduino IDE compatibility. Don't need to include .cpp for PlatformIO
#ifndef PLATFORMIO_STYLE_IMPORTS
#include "lib/PetalPID/src/PetalPID.cpp"
#endif
#include "lib/PetalPID/src/PetalPID.h"
#include "include/config.h"
#include "include/pins.h"
#include "include/encoder.h"
#include "include/motor.h"
#include "include/rotary_switch.h"
boolean motor_enabled_prev;
PetalPID pid = PetalPID();
void setup() {
// Setup encoder
encoder.init();
// Setup rotary switch as pullup pins
for (uint8_t i = 0; i < sizeof(SPEED_SELECTOR_PINS) / sizeof(uint8_t); ++i)
pinMode(SPEED_SELECTOR_PINS[i], INPUT_PULLUP);
// Setup enable
pinMode(MOTOR_ENABLE_PIN, INPUT_PULLUP);
// Setup command 19
pinMode(COMMAND_19_PIN, OUTPUT);
#ifdef COMMAND_19_INVERTED
digitalWrite(COMMAND_19_PIN, HIGH);
#endif
// Setup PID motor
pid.set_min_max_output(PID_MIN_OUT, PID_MAX_OUT);
pid.set_min_max_integral(PID_MIN_INTEGRAL, PID_MAX_INTEGRAL);
pid.set_gains(PID_P_GAIN, PID_I_GAIN, PID_D_GAIN);
Motor::init();
// Setup serial port for debugging
#ifdef DEBUG
Serial.begin(115200);
#endif
// Wait a bit to make sure everything is settled
delay(100);
}
void loop() {
// Read rotary switch
float rpm_setpoint_raw = rotarySwitch.read_setpoint();
// Enable / disable command 19 according to RPM
boolean command_19 = rpm_setpoint_raw > RPM_SETPOINTS[sizeof(RPM_SETPOINTS) / sizeof(float) / 2];
#ifdef COMMAND_19_INVERTED
digitalWrite(COMMAND_19_PIN, !command_19);
#else
digitalWrite(COMMAND_19_PIN, command_19);
#endif
// Check if we're stalled
boolean is_stalled = encoder.stall_check();
// Check if we need to enable or disable motor
#ifdef MOTOR_ENABLED_INVERTED
boolean motor_enabled = !digitalRead(MOTOR_ENABLE_PIN);
#else
boolean motor_enabled = digitalRead(MOTOR_ENABLE_PIN);
#endif
// Something changed -> reset setpoint and PID
if (motor_enabled != motor_enabled_prev) {
rotarySwitch.reset();
pid.reset();
Motor::write(0.f);
// Stop or resume encoder
if (motor_enabled)
encoder.resume();
else
encoder.stop();
}
// Save for the next cycle
motor_enabled_prev = motor_enabled;
// We don't need other encoder and pid actions now
if (!motor_enabled)
return;
// If we have encoder callback or stalled
if (encoder.get_triggered() || is_stalled) {
// Clear triggered flag
encoder.clear_triggered();
// Filter setpoint
float rpm_setpoint_filtered = rotarySwitch.filter_setpoint();
// Calculate PID only if motor is enabled
if (motor_enabled)
Motor::write(pid.calculate(encoder.get_rpm_filtered(), rpm_setpoint_filtered, micros()));
// Reset PID if motor is disabled
else
pid.reset();
// Plot debug info (only if motor is spinning or stalled)
#ifdef DEBUG
Serial.print(rpm_setpoint_filtered);
Serial.print("\t");
Serial.println(encoder.get_rpm_filtered());
#endif
}
// Simulate encoder cycle if stalled
if (is_stalled) {
uint64_t period_min = (1.f / (ENCODER_MIN_RPM / 60.f * ENCODER_DIVIDER)) * 1.e6;
delay(period_min / 1000);
delayMicroseconds(period_min % 1000);
}
}