2023-04-19 23:54:02 +08:00
|
|
|
|
#pragma once
|
|
|
|
|
#include <Arduino.h>
|
|
|
|
|
#include <CAN.h>
|
|
|
|
|
#include <ESP32SJA1000.h>
|
|
|
|
|
#define MOTO_REVERSED 1 //正反转1为正转,-1为反转
|
|
|
|
|
#define MOTO_REDUCTION 36
|
|
|
|
|
#define MOTO_MAXANG 8192 //0-8191
|
|
|
|
|
#define MOTO_MAXANG_HALF MOTO_MAXANG / 2
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
typedef struct _PID_TypeDef
|
|
|
|
|
{
|
|
|
|
|
float target; // 目标值====
|
|
|
|
|
float lastNoneZeroTarget;
|
|
|
|
|
float kp;
|
|
|
|
|
float ki;
|
|
|
|
|
float kd;
|
|
|
|
|
float measure; // 测量值
|
|
|
|
|
float err; // 误差
|
|
|
|
|
float last_err; // 上次误差
|
|
|
|
|
float pout;
|
|
|
|
|
float iout;
|
|
|
|
|
float dout;
|
|
|
|
|
float output; // 本次输出====
|
|
|
|
|
// float last_output; //上次输出
|
|
|
|
|
float MaxOutput; // 输出限幅=====
|
|
|
|
|
float IntegralLimit; // 积分限幅=====
|
|
|
|
|
float DeadBand; // 死区(绝对值)=====
|
|
|
|
|
// float ControlPeriod; //控制周期====
|
|
|
|
|
float Max_Err; // 最大误差====
|
|
|
|
|
|
|
|
|
|
uint32_t thistime;
|
|
|
|
|
uint32_t lasttime;
|
|
|
|
|
uint8_t dtime;
|
|
|
|
|
|
|
|
|
|
} PID_TypeDef;
|
|
|
|
|
|
|
|
|
|
typedef struct
|
|
|
|
|
{
|
|
|
|
|
int16_t speed_rpm;
|
|
|
|
|
int16_t real_current;
|
|
|
|
|
uint8_t temperature;
|
|
|
|
|
uint16_t angle; // abs angle range:[0,8191]
|
|
|
|
|
uint16_t last_angle; // abs angle range:[0,8191]
|
|
|
|
|
uint16_t offset_angle;
|
|
|
|
|
int32_t round_cnt;
|
|
|
|
|
int32_t total_angle;
|
|
|
|
|
uint32_t msg_cnt;
|
|
|
|
|
|
|
|
|
|
float output_speed_rpm; //输出轴速度 ---rpm
|
|
|
|
|
float output_round_cnt; //输出轴 总圈数
|
|
|
|
|
|
|
|
|
|
} moto_measure_t;
|
|
|
|
|
|
|
|
|
|
/// PID控制器
|
|
|
|
|
void pid_param_init(PID_TypeDef *pid,
|
|
|
|
|
uint16_t maxout,
|
|
|
|
|
uint16_t intergral_limit,
|
|
|
|
|
float deadband,
|
|
|
|
|
int16_t max_err,
|
|
|
|
|
int16_t target,
|
|
|
|
|
float kp,
|
|
|
|
|
float ki,
|
|
|
|
|
float kd);
|
|
|
|
|
inline void pid_reset(PID_TypeDef *pid, float kp, float ki, float kd);
|
|
|
|
|
inline void pid_target(PID_TypeDef *pid, float new_target);
|
2023-04-22 16:59:32 +08:00
|
|
|
|
float pid_cal(PID_TypeDef *pid, int16_t measure,float pidtarget);
|
|
|
|
|
void pid_init();
|
2023-04-19 23:54:02 +08:00
|
|
|
|
|
|
|
|
|
class moto
|
|
|
|
|
{
|
|
|
|
|
private:
|
2023-04-22 16:59:32 +08:00
|
|
|
|
bool _runing;
|
|
|
|
|
bool _closed;
|
2023-04-19 23:54:02 +08:00
|
|
|
|
void set_moto_current(int16_t iq1);
|
|
|
|
|
static void get_moto_measure(moto_measure_t *ptr, uint8_t *message_ptr);
|
|
|
|
|
static void onReceive(int packetSize);
|
|
|
|
|
|
|
|
|
|
public:
|
|
|
|
|
moto(); // 构造函数
|
|
|
|
|
~moto(); // 析构函数
|
2023-04-22 16:59:32 +08:00
|
|
|
|
bool brake; //刹车==默认true
|
2023-04-19 23:54:02 +08:00
|
|
|
|
bool init();
|
|
|
|
|
void update();
|
|
|
|
|
void settarget(float new_target);
|
|
|
|
|
moto_measure_t getmotoinfo();
|
2023-04-22 16:59:32 +08:00
|
|
|
|
void close();
|
2023-04-19 23:54:02 +08:00
|
|
|
|
};
|