Unverified Commit ca9e1193 authored by Wenxi Xu's avatar Wenxi Xu Committed by GitHub
Browse files

Merge pull request from new-atomic-motor-api

增加原子化的电机API等诸多修改
parents c490efc1 47fa43e2
Loading
Loading
Loading
Loading
+38 −17
Original line number Diff line number Diff line
# 电机驱动API
```c
motor_driver_api {
    float motor_get_speed(const struct device *dev);
    float motor_get_torque(const struct device *dev);
    float motor_get_angle(const struct device *dev);
    int motor_set_speed(const struct device *dev, float speed_rpm);
    int motor_set_torque(const struct device *dev, float torque);
    int motor_set_angle(const struct device *dev, float angle);
    int motor_set_mode(const struct device *dev, enum motor_mode mode);
    int motor_get(const struct device *dev, motor_status_t *status);
    int motor_set(const struct device *dev, motor_status_t *status);
    void motor_control(const struct device *dev, enum motor_cmd cmd);
};
```
@@ -18,22 +13,48 @@ motor_driver_api {
 * MIT: MIT模式
 * PV: 位置-速度控制
 * VO: 速度控制  
 * MULTILOOP: 多环串联控制
 * ML_TORQUE: 多环扭矩控制
 * ML_ANGLE: 多环角度控制
 * ML_SPEED: 多环速度控制
 */
enum motor_mode { MIT, PV, VO, MULTILOOP };
enum motor_mode { MIT, PV, VO, ML_TORQUE, ML_ANGLE, ML_SPEED };
```
```c
/**
 * @brief 电机控制命令
 */
enum motor_cmd { ENABLE_MOTOR, DISABLE_MOTOR, SET_ZERO_OFFSET, CLEAR_PID, CLEAR_ERROR };
enum motor_cmd { ENABLE_MOTOR, DISABLE_MOTOR, SET_ZERO, CLEAR_PID, CLEAR_ERROR };
```
```c
motor_status_t {
	float angle;
	float rpm;
	float torque;
	float temperature; /* Cannot be set in target */
	int round_cnt;

	float speed_limit[2];
	float torque_limit[2];

	enum motor_mode mode;
};
```
## 电机状态获取
`motor_get_speed(const struct device *dev)`: 获取电机当前速度,返回值为电机当前速度,单位为rpm。
`motor_get_torque(const struct device *dev)`: 获取电机当前扭矩,返回值为电机当前扭矩,单位为N*m。
`motor_get_angle(const struct device *dev)`: 获取电机当前角度,返回值为电机当前角度,单位为°。
`motor_set_speed(const struct device *dev, float speed_rpm)`: 设置电机目标速度,参数为目标速度,单位为rpm。
`motor_set_torque(const struct device *dev, float torque)`: 设置电机目标扭矩,参数为目标扭矩,单位为N*m。
`motor_set_angle(const struct device *dev, float angle)`: 设置电机目标角度,参数为目标角度,单位为°。
`motor_set_mode(const struct device *dev, enum motor_mode mode)`: 设置电机工作模式,参数为工作模式。
`motor_get(const struct device *dev, motor_status_t *status)`: 获取电机当前状态,参数为电机状态结构体。
`motor_set(const struct device *dev, motor_status_t *status)`: 设置电机目标状态,参数为电机状态结构体。
`motor_control(const struct device *dev, enum motor_cmd cmd)`: 控制电机,参数为控制命令。

## 示例
设置电机为多环扭矩控制模式,并设置目标扭矩为10N*m。
```c
motor_status_t status;
status.mode = ML_TORQUE;
status.torque = 10;
motor_set(motor, &status);
```
获取电机当前的扭矩
```c
motor_status_t status;
motor_get(motor, &status);
LOG_INF("Motor torque: %.2f", status.torque);
```
 No newline at end of file
+0 −18
Original line number Diff line number Diff line
@@ -22,7 +22,6 @@
		zephyr,sram = &sram0;
		zephyr,button = &user_button;
		zephyr,flash = &flash0;
		zephyr,canbus = &fdcan1;
		zephyr,dtcm = &dtcm;
		zephyr,itcm = &itcm;
		ares,pwm = &heater;
@@ -45,23 +44,6 @@
			zephyr,code = <INPUT_KEY_0>;
		};
	};
	canbus {
		canbus1: canbus1 {
				compatible = "vnd,canbus";
				status = "okay";
				can_device = <&fdcan1>;
		};
		canbus2: canbus2 {
				compatible = "vnd,canbus";
				status = "okay";
				can_device = <&fdcan2>;
		};
		canbus3: canbus3 {
				compatible = "vnd,canbus";
				status = "okay";
				can_device = <&fdcan3>;
		};
	};
    
	xt30 {
		compatible = "gpio-leds";
+2 −0
Original line number Diff line number Diff line
@@ -95,3 +95,5 @@ CONFIG_CMSIS_DSP_FASTMATH=y

CONFIG_ISR_STACK_SIZE=3200
CONFIG_MAIN_STACK_SIZE=3200

CONFIG_CAN_COUNT=3
 No newline at end of file
+0 −13
Original line number Diff line number Diff line
@@ -16,7 +16,6 @@
		zephyr,sram = &sram0;
		zephyr,button = &user_button;
		zephyr,flash = &flash0;
		zephyr,canbus = &can1;
		zephyr,ccm = &ccm0;
		sbus,uart = &usart1;
	};
@@ -89,18 +88,6 @@
			zephyr,code = <INPUT_KEY_0>;
		};
	};
	canbus {
		canbus1: canbus1 {
				compatible = "vnd,canbus";
				status = "okay";
				can_device = <&can1>;
		};
		canbus2: canbus2 {
				compatible = "vnd,canbus";
				status = "okay";
				can_device = <&can2>;
		};
	};
    powerctrl {
		compatible = "gpio-leds";
		status = "okay";
+0 −13
Original line number Diff line number Diff line
@@ -20,7 +20,6 @@
		zephyr,sram = &sram0;
		zephyr,button = &user_button;
		zephyr,flash = &flash0;
		zephyr,canbus = &can1;
		zephyr,ccm = &ccm0;
		ares,pwm = &heater;
		sbus,uart = &usart3;
@@ -69,18 +68,6 @@
			zephyr,code = <INPUT_KEY_0>;
		};
	};
	canbus {
		canbus1: canbus1 {
				compatible = "vnd,canbus";
				status = "okay";
				can_device = <&can1>;
		};
		canbus2: canbus2 {
				compatible = "vnd,canbus";
				status = "okay";
				can_device = <&can2>;
		};
	};
};

&clk_lsi {
Loading