satnogs-rotator-firmware
Loading...
Searching...
No Matches
dc_motor_controller.ino
Go to the documentation of this file.
1
25#define SAMPLE_TIME 0.1
26#define RATIO 54
27#define MAX_PWM 180
28#define MIN_PWM 5
29#define POSITION_DEADZONE 0.2
30#define PCA9540_ID 0x70
31#define PCA9540_CH0 0x04
32#define PCA9540_CH1 0x05
33#define TC74_ID 0x48
34#define OVER_TEMP 60
35#define ENC_RATIO 2
36#define MIN_M1_ANGLE 0
37#define MAX_M1_ANGLE 360
38#define MIN_M2_ANGLE 0
39#define MAX_M2_ANGLE 180
40#define DEFAULT_HOME_STATE HIGH
41#define HOME_SPEED 100
42
43#include <Arduino.h>
44#include <PID_v1.h>
45#include <Wire.h>
52#include "../libraries/tc74.h"
53#include "../libraries/motor.h"
54#include "../libraries/as5601.h"
55
56uint32_t t_run = 0; // run time of uC
60PID pid_az(&control_az.input, &control_az.u, &control_az.setpoint, control_az.p,
61 control_az.i, control_az.d, P_ON_E, DIRECT);
62PID pid_el(&control_el.input, &control_el.u, &control_el.setpoint, control_el.p,
63 control_el.i, control_el.d, P_ON_E, DIRECT);
69
71
72void setup() {
73 // Homing switch
76
77 // Serial Communication
79
80 // Initialize DC motors
82 motor_az.init_timer(1, 8);
85 motor_el.init_timer(2, 8);
87
88 // Initialize I2C MUX
89 pca9540.init();
90 // Initialize rotary encoders
93
94 // Initialize control parameters
95 pid_az.SetSampleTime(SAMPLE_TIME);
96 pid_az.SetOutputLimits(-MAX_PWM, MAX_PWM );
97 pid_az.SetMode(AUTOMATIC);
98 pid_el.SetSampleTime(SAMPLE_TIME);
99 pid_el.SetOutputLimits(-MAX_PWM, MAX_PWM);
100 pid_el.SetMode(AUTOMATIC);
101
102 // Initialize WDT
104}
105
106void loop() {
107 // Update WDT
109
110 // Get end stop status
113
114 // Run easycomm implementation
116
117 // Get Motor driver status
120 if (rotator.fault_az == LOW || rotator.fault_el == LOW) {
123 }
124
125 // Get inside Temperature
133 }
134 // Get position of both axis
139
140 // Check rotator status
142 if (rotator.homing_flag == false) {
143 // Check home flag
145 // Homing
148 // No error
150 rotator.homing_flag = true;
151 } else {
152 // Error
155 }
156 } else {
157 // Control Loop
158 if (millis() - t_run > SAMPLE_TIME * 1000) {
159 // Update control gains
160 pid_az.SetTunings(control_az.p, control_az.i, control_az.d);
161 pid_el.SetTunings(control_el.p, control_el.i, control_el.d);
162 if (rotator.control_mode == speed) {
163 control_az.setpoint += control_az.setpoint_speed
164 * SAMPLE_TIME;
165 control_el.setpoint += control_el.setpoint_speed
166 * SAMPLE_TIME;
168 } else {
170 }
171 // Move azimuth and elevation motors
172 pid_az.Compute();
174 pid_el.Compute();
176 // Calculate the speeds of both axis
177 control_az.speed = (control_az.input - control_az.input_prv)
178 / SAMPLE_TIME;
179 control_az.input_prv = control_az.input;
180 control_el.speed = (control_el.input - control_el.input_prv)
181 / SAMPLE_TIME;
182 control_el.input_prv = control_el.input;
183 // Update the run time
184 t_run = millis();
185 // Idle rotator, dead-band
186 if ((abs(control_az.setpoint - control_az.input) <=
187 POSITION_DEADZONE || (control_az.speed == 0)) &&
188 (abs(control_el.setpoint - control_el.input) <=
189 POSITION_DEADZONE || (control_el.speed == 0))) {
191 }
192 }
193 }
194 } else {
195 // Error handler, stop motors and disable the motor driver
196 motor_az.stop();
198 motor_el.stop();
201 // Reset error according to error value
204 }
205 }
206}
207
208/**************************************************************************/
214/**************************************************************************/
216 bool isHome_az = false;
217 bool isHome_el = false;
218
219 // Reses position
224
225 // Move motors with ~constant speed
228
229 // Homing loop
230 while (isHome_az == false || isHome_el == false) {
231 // Update WDT
233 if (switch_az.get_state() == true && !isHome_az) {
234 // Find azimuth home
235 motor_az.stop();
236 isHome_az = true;
237 }
238 if (switch_el.get_state() == true && !isHome_el) {
239 // Find elevation home
240 motor_el.stop();
241 isHome_el = true;
242 }
243 // Get current position
248 // Check if the rotator goes out of limits or something goes wrong (in
249 // mechanical)
250 if ((abs(control_az.input) > MAX_M1_ANGLE && !isHome_az)
251 || (abs(control_el.input) > MAX_M2_ANGLE && !isHome_el)) {
252 return homing_error;
253 }
254 }
255
256 // Set the home position and reset all critical control variables
260 control_az.setpoint = 0;
264 control_el.setpoint = 0;
265
266 return no_error;
267}
Class that functions for interacting with AS5601 magnetic rotary position sensor.
Definition as5601.h:36
void init_zero()
Reset zero position set the offset to zero.
Definition as5601.h:142
uint8_t get_pos(double *new_pos)
Calculate an unwrap the position.
Definition as5601.h:60
void set_gear_ratio(uint8_t enc_ratio)
Set the gear ratio between encoder and measure axis.
Definition as5601.h:153
uint8_t set_zero()
Set zero by setting offset angle.
Definition as5601.h:130
Class that functions for easycomm 3 implementation.
Definition easycomm.h:33
void easycomm_proc()
Get the commands from RS485 and response to the client.
Definition easycomm.h:50
void easycomm_init()
Initialize the RS485 bus.
Definition easycomm.h:41
Class that functions for interacting with end-stop.
Definition endstop.h:24
bool get_state()
Get the state of end-stop.
Definition endstop.h:47
void init()
Initialize the Input pin for end-stop.
Definition endstop.h:37
Class that functions for interacting with I2C 1-of-2 multiplexer.
Definition i2c_mux.h:29
void init()
Initialize the I2C bus.
Definition i2c_mux.h:43
void set_channel(uint8_t ch)
Change the channel.
Definition i2c_mux.h:55
Class that functions for interacting with a Motor Driver Carrier.
Definition motor.h:37
void move(int16_t speed)
Move the DC motor with constant voltage (~speed)
Definition motor.h:190
void init_timer(uint8_t timer, uint16_t divisor)
Set timer frequency, for timers 0, 1, 2.
Definition motor.h:97
uint8_t get_fault()
Get the status flag of motor driver.
Definition motor.h:178
void init_pin()
Initialize pins of DC motor driver.
Definition motor.h:57
void stop()
Stop moving the DC motor.
Definition motor.h:216
void enable()
Enable motor driver.
Definition motor.h:148
void disenable()
Disable motor driver.
Definition motor.h:157
Class that functions for interacting with a TC74 Temperature sensor.
Definition tc74.h:37
int8_t sleep()
Sleep device request to the sensor on the specified address.
Definition tc74.h:111
int8_t get_temp()
Reads the int8_t in temperature measurement register.
Definition tc74.h:60
int8_t wake_up()
Wake up request to the sensor on the specified address.
Definition tc74.h:95
Class that functions for interacting with a watchdog timer.
Definition watchdog.h:26
void watchdog_reset()
Reset the watchdog timer.
Definition watchdog.h:52
void watchdog_init()
Initialize watchdog timer to 2sec time out and to set up interrupt routine.
Definition watchdog.h:35
endstop switch_el(SW2, DEFAULT_HOME_STATE)
endstop switch_az(SW1, DEFAULT_HOME_STATE)
#define MAX_PWM
Set max Speed.
tc74 temp_sensor(TC74_ID)
AS5601 encoder_el
#define PCA9540_ID
I2C Multiplexer ID.
easycomm comm
#define PCA9540_CH1
I2C Multiplexer CH1.
void setup()
motor motor_el(M2IN1, M2IN2, M2FB, MOTOR_EN, M2SF, MAX_PWM, MIN_PWM)
#define SAMPLE_TIME
Control loop in s.
i2c_mux pca9540(PCA9540_ID, PCA9540_CH0, PCA9540_CH1)
#define PCA9540_CH0
I2C Multiplexer CHO.
PID pid_az & control_az
#define HOME_SPEED
Set speed to find home, duty cycle of 8-bit timer.
#define MIN_PWM
Set min Speed.
motor motor_az(M1IN1, M1IN2, M1FB, MOTOR_EN, M1SF, MAX_PWM, MIN_PWM)
PID pid_el & control_el
#define POSITION_DEADZONE
Control dead zone.
wdt_timer wdt
#define DEFAULT_HOME_STATE
Change to LOW according to Home sensor.
uint32_t t_run
#define ENC_RATIO
Encoder AS5601 gear ratio.
#define MAX_M1_ANGLE
Maximum angle of azimuth.
#define MAX_M2_ANGLE
Maximum angle of elevation.
enum _rotator_error homing()
Move both axis with one direction in order to find home position, end-stop switches.
AS5601 encoder_az
#define OVER_TEMP
Over temperature limit.
#define TC74_ID
Temperature Sensor ID.
void loop()
@ position
Definition globals.h:26
@ speed
Definition globals.h:26
_rotator_error
Definition globals.h:20
@ no_error
Definition globals.h:21
@ over_temperature
Definition globals.h:22
@ homing_error
Definition globals.h:21
@ motor_error
Definition globals.h:21
_rotator rotator
Definition globals.h:57
@ idle
Definition globals.h:17
@ r_error
Definition globals.h:17
@ moving
Definition globals.h:17
@ pointing
Definition globals.h:17
#define M1IN2
Motor 1 PWM pin.
#define M1SF
Motor 1 digital input, status flag.
#define M2IN2
Motor 2 PWM pin.
#define SW2
Digital input, to read the status of end-stop for motor 2.
#define MOTOR_EN
Digital output, to enable the motors.
#define M1FB
Motor 1 analog input, current/load feedback.
#define M1IN1
Motor 1 PWM pin.
#define M2FB
Motor 2 analog input, current/load feedback.
#define M2SF
Motor 2 digital input, status flag.
#define M2IN1
Motor 2 PWM pin.
#define SW1
Digital input, to read the status of end-stop for motor 1.
enum _rotator_error rotator_error
Rotator error.
Definition globals.h:42
int8_t inside_temperature
Inside Temperature.
Definition globals.h:45
bool switch_az
Definition globals.h:48
uint8_t fault_az
Definition globals.h:47
enum _control_mode control_mode
Control mode.
Definition globals.h:43
bool switch_el
End-stop vales.
Definition globals.h:48
uint8_t fault_el
Motor drivers fault flag.
Definition globals.h:47
enum _rotator_status rotator_status
Rotator status.
Definition globals.h:41
bool homing_flag
Homing flag.
Definition globals.h:44