satnogs-rotator-firmware
Loading...
Searching...
No Matches
stepper_motor_controller.ino
Go to the documentation of this file.
1
25#define SAMPLE_TIME 0.1
26#define RATIO 54
27#define MICROSTEP 8
28#define MIN_PULSE_WIDTH 20
29#define MAX_SPEED 3200
30#define MAX_ACCELERATION 1600
31#define SPR 1600L
32#define MIN_M1_ANGLE 0
33#define MAX_M1_ANGLE 360
34#define MIN_M2_ANGLE 0
35#define MAX_M2_ANGLE 180
36#define DEFAULT_HOME_STATE HIGH
37#define HOME_DELAY 12000
38
39#include <Arduino.h>
40#include <AccelStepper.h>
41#include <Wire.h>
45#include "../libraries/rs485.h"
48
49uint32_t t_run = 0; // run time of uC
51AccelStepper stepper_az(1, M1IN1, M1IN2);
52AccelStepper stepper_el(1, M2IN1, M2IN2);
55
56enum _rotator_error homing(int32_t seek_az, int32_t seek_el);
57int32_t deg2step(float deg);
58float step2deg(int32_t step);
59
60void setup() {
61 // Homing switch
64
65 // Serial Communication
67
68 // Stepper Motor setup
69 stepper_az.setEnablePin(MOTOR_EN);
70 stepper_az.setPinsInverted(false, false, true);
71 stepper_az.enableOutputs();
72 stepper_az.setMaxSpeed(MAX_SPEED);
73 stepper_az.setAcceleration(MAX_ACCELERATION);
74 stepper_az.setMinPulseWidth(MIN_PULSE_WIDTH);
75 stepper_el.setPinsInverted(false, false, true);
76 stepper_el.enableOutputs();
77 stepper_el.setMaxSpeed(MAX_SPEED);
78 stepper_el.setAcceleration(MAX_ACCELERATION);
79 stepper_el.setMinPulseWidth(MIN_PULSE_WIDTH);
80
81 // Initialize WDT
83}
84
85void loop() {
86 // Update WDT
88
89 // Get end stop status
92
93 // Run easycomm implementation
95
96 // Get position of both axis
97 control_az.input = step2deg(stepper_az.currentPosition());
98 control_el.input = step2deg(stepper_el.currentPosition());
99
100 // Check rotator status
102 if (rotator.homing_flag == false) {
103 // Check home flag
105 // Homing
109 // No error
111 rotator.homing_flag = true;
112 } else {
113 // Error
116 }
117 } else {
118 // Control Loop
122 // Move azimuth and elevation motors
123 stepper_az.run();
124 stepper_el.run();
125 // Idle rotator
126 if (stepper_az.distanceToGo() == 0 && stepper_el.distanceToGo() == 0) {
128 }
129 }
130 } else {
131 // Error handler, stop motors and disable the motor driver
132 stepper_az.stop();
133 stepper_az.disableOutputs();
134 stepper_el.stop();
135 stepper_el.disableOutputs();
137 // Reset error according to error value
140 }
141 }
142}
143
144/**************************************************************************/
154/**************************************************************************/
155enum _rotator_error homing(int32_t seek_az, int32_t seek_el) {
156 bool isHome_az = false;
157 bool isHome_el = false;
158
159 // Move motors to "seek" position
160 stepper_az.moveTo(seek_az);
161 stepper_el.moveTo(seek_el);
162
163 // Homing loop
164 while (isHome_az == false || isHome_el == false) {
165 // Update WDT
167 if (switch_az.get_state() == true && !isHome_az) {
168 // Find azimuth home
169 stepper_az.moveTo(stepper_az.currentPosition());
170 isHome_az = true;
171 }
172 if (switch_el.get_state() == true && !isHome_el) {
173 // Find elevation home
174 stepper_el.moveTo(stepper_el.currentPosition());
175 isHome_el = true;
176 }
177 // Check if the rotator goes out of limits or something goes wrong (in
178 // mechanical)
179 if ((stepper_az.distanceToGo() == 0 && !isHome_az) ||
180 (stepper_el.distanceToGo() == 0 && !isHome_el)){
181 return homing_error;
182 }
183 // Move motors to "seek" position
184 stepper_az.run();
185 stepper_el.run();
186 }
187 // Delay to Deccelerate and homing, to complete the movements
188 uint32_t time = millis();
189 while (millis() - time < HOME_DELAY) {
191 stepper_az.run();
192 stepper_el.run();
193 }
194 // Set the home position and reset all critical control variables
195 stepper_az.setCurrentPosition(0);
196 stepper_el.setCurrentPosition(0);
199
200 return no_error;
201}
202
203/**************************************************************************/
211/**************************************************************************/
212int32_t deg2step(float deg) {
213 return (RATIO * SPR * deg / 360);
214}
215
216/**************************************************************************/
224/**************************************************************************/
225float step2deg(int32_t step) {
226 return (360.00 * step / (SPR * RATIO));
227}
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 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
enum _rotator_error homing()
Move both axis with one direction in order to find home position, end-stop switches.
@ position
Definition globals.h:26
_rotator_error
Definition globals.h:20
@ no_error
Definition globals.h:21
@ homing_error
Definition globals.h:21
_control control_az
Definition globals.h:51
_rotator rotator
Definition globals.h:57
_control control_el
Definition globals.h:54
@ idle
Definition globals.h:17
@ r_error
Definition globals.h:17
@ pointing
Definition globals.h:17
#define M1IN2
Motor 1 PWM pin.
#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 M1IN1
Motor 1 PWM pin.
#define M2IN1
Motor 2 PWM pin.
#define SW1
Digital input, to read the status of end-stop for motor 1.
endstop switch_el(SW2, DEFAULT_HOME_STATE)
endstop switch_az(SW1, DEFAULT_HOME_STATE)
easycomm comm
#define MAX_ACCELERATION
In steps/s^2, consider the microstep.
#define RATIO
Gear ratio of rotator gear box.
float step2deg(int32_t step)
Convert steps to degrees according to step/revolution, rotator gear box ratio and microstep.
int32_t deg2step(float deg)
Convert degrees to steps according to step/revolution, rotator gear box ratio and microstep.
wdt_timer wdt
#define DEFAULT_HOME_STATE
Change to LOW according to Home sensor.
uint32_t t_run
#define SPR
Step Per Revolution, consider the microstep.
#define MAX_SPEED
In steps/s, consider the microstep.
#define MAX_M1_ANGLE
Maximum angle of azimuth.
AccelStepper stepper_az(1, M1IN1, M1IN2)
#define MAX_M2_ANGLE
Maximum angle of elevation.
#define HOME_DELAY
Time for homing Deceleration in millisecond.
#define MIN_PULSE_WIDTH
In microsecond for AccelStepper.
AccelStepper stepper_el(1, M2IN1, M2IN2)
double setpoint
Position set point in deg.
Definition globals.h:33
double input
Motor Position feedback in deg.
Definition globals.h:30
enum _rotator_error rotator_error
Rotator error.
Definition globals.h:42
bool switch_az
Definition globals.h:48
enum _control_mode control_mode
Control mode.
Definition globals.h:43
bool switch_el
End-stop vales.
Definition globals.h:48
enum _rotator_status rotator_status
Rotator status.
Definition globals.h:41
bool homing_flag
Homing flag.
Definition globals.h:44