Commit a17ffe57 authored by Wenxi XU's avatar Wenxi XU
Browse files

添加HYBRID模式的初步支持,优化电机模式设置逻辑,修复未找到模式时的错误处理。

parent be18b714
Loading
Loading
Loading
Loading
+37 −18
Original line number Diff line number Diff line
@@ -123,6 +123,7 @@ static void dm_motor_pack(const struct device *dev, struct can_frame *frame)
{
	uint16_t pos_tmp, vel_tmp, kp_tmp, kd_tmp, tor_tmp;
	uint8_t *pbuf, *vbuf;
	// float hybrid_p_tmp;
	struct dm_motor_data *data = dev->data;
	const struct dm_motor_config *cfg = dev->config;

@@ -158,6 +159,13 @@ static void dm_motor_pack(const struct device *dev, struct can_frame *frame)

		memcpy(frame->data, vbuf, 4);
		break;
	case HYBRID:
		// hybrid_p_tmp = data->target_angle * DEG2RAD;
		// memcpy(frame->data, &hybrid_p_tmp, 4);
		// vel_tmp = data->target_radps * 100;
		// frame->data[4] = (vel_tmp >> 8);
		// frame->data[5] = vel_tmp;
		break;
	default:
		break;
	}
@@ -255,7 +263,6 @@ void dm_motor_set_mode(const struct device *dev, enum motor_mode mode)
		dm_control(dev, DISABLE_MOTOR);
	}

	if (mode != VO) {
	bool found = false;
	for (int i = 0; i < SIZE_OF_ARRAY(cfg->common.capabilities); i++) {
		if (cfg->common.pid_datas[i]->pid_dev == NULL) {
@@ -274,9 +281,21 @@ void dm_motor_set_mode(const struct device *dev, enum motor_mode mode)
	}
	if (!found) {
		LOG_ERR("Mode %s not found", mode_str);
		if (mode != VO && mode != HYBRID) {
			dm_control(dev, DISABLE_MOTOR);
			data->enable = false;
		}
	} else if (mode == VO) {
		union {
			float f;
			uint32_t u;
		} conv;
		conv.f = data->params.k_p;
		dm_edit_reg_value(cfg->common.phy, cfg->common.tx_id, 0x19, conv.u);
		conv.f = data->params.k_i;
		dm_edit_reg_value(cfg->common.phy, cfg->common.tx_id, 0x1A, conv.u);
	} else if (mode == PV) {
		LOG_ERR("PV mode params setting not supported");
	}
}

+1 −0
Original line number Diff line number Diff line
@@ -26,6 +26,7 @@

#define RAD2ROUND 1.0f / (2 * PI)
#define RAD2DEG   180.0f / PI
#define DEG2RAD   PI / 180.0f

#define CANID_L 0u
#define CANID_H 1u