|
| 1 | +/** |
| 2 | + * @file HybridStepperMotor.h |
| 3 | + * |
| 4 | + */ |
| 5 | + |
| 6 | +#ifndef HybridStepperMotor_h |
| 7 | +#define HybridStepperMotor_h |
| 8 | + |
| 9 | +#include "Arduino.h" |
| 10 | +#include "common/base_classes/FOCMotor.h" |
| 11 | +#include "common/base_classes/StepperDriver.h" |
| 12 | +#include "common/base_classes/Sensor.h" |
| 13 | +#include "common/foc_utils.h" |
| 14 | +#include "common/time_utils.h" |
| 15 | +#include "common/defaults.h" |
| 16 | + |
| 17 | +/** |
| 18 | + Stepper Motor class |
| 19 | +*/ |
| 20 | +class HybridStepperMotor : public FOCMotor |
| 21 | +{ |
| 22 | +public: |
| 23 | + /** |
| 24 | + HybridStepperMotor class constructor |
| 25 | + @param pp pole pair number |
| 26 | + @param R motor phase resistance - [Ohm] |
| 27 | + @param KV motor KV rating (1/K_bemf) - rpm/V |
| 28 | + @param L motor phase inductance - [H] |
| 29 | + */ |
| 30 | + HybridStepperMotor(int pp, float R = NOT_SET, float KV = NOT_SET, float L = NOT_SET); |
| 31 | + |
| 32 | + /** |
| 33 | + * Function linking a motor and a foc driver |
| 34 | + * |
| 35 | + * @param driver BLDCDriver handle for hardware peripheral control |
| 36 | + */ |
| 37 | + void linkDriver(BLDCDriver *driver); |
| 38 | + |
| 39 | + /** |
| 40 | + * BLDCDriver link: |
| 41 | + */ |
| 42 | + BLDCDriver *driver; |
| 43 | + |
| 44 | + /** Motor hardware init function */ |
| 45 | + int init() override; |
| 46 | + /** Motor disable function */ |
| 47 | + void disable() override; |
| 48 | + /** Motor enable function */ |
| 49 | + void enable() override; |
| 50 | + |
| 51 | + /** |
| 52 | + * Function initializing FOC algorithm |
| 53 | + * and aligning sensor's and motors' zero position |
| 54 | + */ |
| 55 | + int initFOC() override; |
| 56 | + |
| 57 | + /** |
| 58 | + * Function running FOC algorithm in real-time |
| 59 | + * it calculates the gets motor angle and sets the appropriate voltages |
| 60 | + * to the phase pwm signals |
| 61 | + * - the faster you can run it the better Arduino UNO ~1ms, Bluepill ~ 100us |
| 62 | + */ |
| 63 | + void loopFOC() override; |
| 64 | + |
| 65 | + /** |
| 66 | + * Function executing the control loops set by the controller parameter of the HybridStepperMotor. |
| 67 | + * |
| 68 | + * @param target Either voltage, angle or velocity based on the motor.controller |
| 69 | + * If it is not set the motor will use the target set in its variable motor.target |
| 70 | + * |
| 71 | + * This function doesn't need to be run upon each loop execution - depends of the use case |
| 72 | + */ |
| 73 | + void move(float target = NOT_SET) override; |
| 74 | + |
| 75 | + float Ua, Ub, Uc; //!< Phase voltages used for inverse Park and Clarke transform |
| 76 | + |
| 77 | + /** |
| 78 | + * Method using FOC to set Uq to the motor at the optimal angle |
| 79 | + * Heart of the FOC algorithm |
| 80 | + * |
| 81 | + * @param Uq Current voltage in q axis to set to the motor |
| 82 | + * @param Ud Current voltage in d axis to set to the motor |
| 83 | + * @param angle_el current electrical angle of the motor |
| 84 | + */ |
| 85 | + void setPhaseVoltage(float Uq, float Ud, float angle_el) override; |
| 86 | + |
| 87 | +private: |
| 88 | + int alignCurrentSense(); |
| 89 | + /** Sensor alignment to electrical 0 angle of the motor */ |
| 90 | + int alignSensor(); |
| 91 | + /** Motor and sensor alignment to the sensors absolute 0 angle */ |
| 92 | + int absoluteZeroSearch(); |
| 93 | + |
| 94 | + // Open loop motion control |
| 95 | + /** |
| 96 | + * Function (iterative) generating open loop movement for target velocity |
| 97 | + * it uses voltage_limit variable |
| 98 | + * |
| 99 | + * @param target_velocity - rad/s |
| 100 | + */ |
| 101 | + float velocityOpenloop(float target_velocity); |
| 102 | + /** |
| 103 | + * Function (iterative) generating open loop movement towards the target angle |
| 104 | + * it uses voltage_limit and velocity_limit variables |
| 105 | + * |
| 106 | + * @param target_angle - rad |
| 107 | + */ |
| 108 | + float angleOpenloop(float target_angle); |
| 109 | + // open loop variables |
| 110 | + long open_loop_timestamp; |
| 111 | +}; |
| 112 | + |
| 113 | +#endif |
0 commit comments