2020/* USER CODE END Header */
2121/* Includes ------------------------------------------------------------------*/
2222
23-
2423#ifndef COMMON_H
2524#define COMMON_H
2625
@@ -57,6 +56,8 @@ typedef struct
5756 volatile int Old_Position_Ticks ; // position with multiple rotations from previous cycle
5857 volatile float Position_RAD ; // Raw position but in radians
5958 volatile float Electric_Angle ; // Electrical angle - depends on number of pole pairs
59+ volatile float theta_offset = 0.0f ; // will be determined at calibration
60+
6061 volatile int ROTATIONS ; // Number of full rotations that motor did
6162 volatile int Velocity_Filter ; // Filtered velocity of the motor
6263 volatile int Velocity ; // velocity of the motor
@@ -88,7 +89,7 @@ typedef struct
8889 volatile float flux_linkage = 0 ; // Flux linkage of your BLDC motor
8990 volatile float KV = 0 ;
9091
91- volatile int Max_temperature = 75 ; // Max temperature in degrees
92+ volatile int Max_temperature = 75 ; // Max temperature in degrees
9293 volatile int Min_temperature = -20 ; // Min temperature in degrees
9394
9495 volatile int Max_Vbus = 29500 ; // Max vbus voltage in mV
@@ -98,9 +99,9 @@ typedef struct
9899
99100 volatile int watchdog_time_ms = 0 ;
100101 volatile bool watchdog_reset = 1 ;
101- volatile int watchdog_action = 0 ; // Hold position, Idle, brake
102+ volatile int watchdog_action = 0 ; // Hold position, Idle, brake
102103 volatile int Heartbeat_rate_ms = 0 ; // Motor controller will send data every Heartbeat_rate_ms
103- volatile bool Send_heartbeat = 0 ;
104+ volatile bool Send_heartbeat = 0 ;
104105
105106 volatile bool Activated = 0 ; // If controller is activated or not
106107 volatile bool Error = 0 ; // General error
@@ -123,7 +124,7 @@ typedef struct
123124 volatile int Open_loop_voltage = 5000 ; // Voltage to use when spinning the motor open loop. In mV
124125 volatile float Open_loop_speed = 20.0 ; // Speed to be used when spinning the motor open loop. In RAD/s
125126 volatile int Number_of_rotations = 8 ; // Number of open loop electrical rotations during calibration
126- volatile int Phase_voltage = 5000 ; // Max voltage to be used during Kv calculation
127+ volatile int Phase_voltage = 5000 ; // Max voltage to be used during Kv calculation
127128
128129 // Calibration steps
129130 // 0 is neutral, 1 is error, 2 is success
@@ -156,6 +157,27 @@ typedef struct
156157
157158 volatile bool handle_can = 0 ;
158159
160+ volatile bool hall_trigger = 1 ; // used to record if hall sensor was triggered or not, normal state of hall is high (1)
161+ volatile bool trigger_value = 0 ;
162+
163+ // temp / testing ?
164+ volatile int calibration_offset_current = 300 ;
165+ volatile int temp_var_offset_calib = 0 ;
166+ volatile int align_state = 0 ;
167+ volatile int align_counter = 0 ;
168+ volatile float aligned_angle = 0 ;
169+ volatile bool align_done = false;
170+ volatile bool calib_done = false;
171+ volatile bool calib_reset = false;
172+ volatile int Velocity_fwd = 0 ;
173+ volatile int Velocity_bwd = 0 ;
174+
175+ // Open loop movements
176+ volatile int microstep = 16 ;
177+ volatile int gosteps = 0 ;
178+ volatile int step_interval = 1000 ; // step interval in microseconds
179+ volatile int open_loop_move_done = 0 ; // 1 = finished, cleared by user
180+
159181} Measure ;
160182
161183extern Measure controller ;
@@ -211,11 +233,10 @@ extern _FOC FOC;
211233typedef struct
212234{
213235
214-
215236 volatile int Voltage_limit = 0 ; // Voltage limit for Ud and Uq voltages. If 0 value of controller.VBUS_mV will be used
216237 // if != 0 it will use that number (if bigger than controller.VBUS_mV it will use controller.VBUS_mV)
217238 // This is extremely important for proper operation of BLDC motor loops since their resistances can be less
218- // than 1 ohm and using full range of 24V makes the loop unstable.
239+ // than 1 ohm and using full range of 24V makes the loop unstable.
219240 // Usually it setting this to 0 is good (so using full controller.VBUS_mV range)
220241 // Use smaller numbers if you have really small phase resistances like 1 ohm or less
221242
@@ -228,25 +249,25 @@ typedef struct
228249 volatile float P_errSum = 0 ;
229250
230251 // Speed loop PID
231- volatile float Kp_v = 0.03 ; // 0.006 basic values that work for most motors but not optimal
252+ volatile float Kp_v = 0.03 ; // 0.006 basic values that work for most motors but not optimal
232253 volatile float Ki_v = 0.0003 ; // 0.0001 basic values that work for most motors but not optimal
233254 volatile float V_errSum = 0 ;
234255 volatile float Feedforward_speed = 0 ;
235256 volatile float Velocity_setpoint = 0 ; // -1.5
236257
237- volatile float Velocity_limit = 80000 ; // Clamp integrals to this [TICKS/s]
258+ volatile float Velocity_limit = 80000 ; // Clamp integrals to this [TICKS/s]
238259 volatile float Velocity_limit_error = 20000000 ; // [TICKS/s] ; Velocity when we will report error
239260
240261 // current loop PID
241262 volatile float Ki = 0 ; // zero location
242263
243- volatile float Kp_id = 2.67 ; // 2.67
264+ volatile float Kp_id = 2.67 ; // 2.67
244265 volatile float Ki_id = 1.93 ; // 1.93
245266 volatile float Id_errSum ;
246267 volatile float Id_setpoint = 0 ;
247268
248269 volatile float Kp_iq = 2.67 ; // 2.67 // 12.8
249- volatile float Ki_iq = 1.93 ; // 1.93 // 9
270+ volatile float Ki_iq = 1.93 ; // 1.93 // 9
250271 volatile float Iq_errSum ;
251272 volatile float Iq_setpoint ;
252273
@@ -263,16 +284,15 @@ typedef struct
263284 volatile int Uq_setpoint = 0 ;
264285 volatile int Ud_setpoint = 0 ;
265286
266-
267287} PID_par ;
268288
269289extern PID_par PID ;
270290
271291typedef struct
272292{
273293
274- volatile uint8_t previousBytes [5 ] = {0 ,0 , 0 , 0 , 0 };
275- volatile bool Same_command = 0 ;
294+ volatile uint8_t previousBytes [5 ] = {0 , 0 , 0 , 0 , 0 };
295+ volatile bool Same_command = 0 ;
276296 volatile bool At_position = 0 ;
277297
278298 volatile uint8_t position_setpoint = 0 ;
@@ -288,11 +308,11 @@ typedef struct
288308 volatile bool activated = 0 ;
289309
290310 // 0 in motion, 1 object detected closing, 2 object detected opening, 3 at positon
291- volatile int object_detection_status = 3 ;
311+ volatile int object_detection_status = 3 ;
292312 volatile bool temperature_error = 0 ;
293313 volatile bool timeout_error = 0 ;
294314 volatile bool estop_error = 0 ;
295-
315+
296316 volatile bool release_direction = 0 ;
297317 volatile bool estop_status = 0 ;
298318
@@ -302,7 +322,7 @@ typedef struct
302322
303323 volatile int position_ticks = 0 ;
304324
305- // Map this to 255 and 0
325+ // Map this to 255 and 0
306326 volatile int max_speed = 80000 ;
307327 volatile int min_speed = 40 ;
308328
0 commit comments