Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 4 additions & 2 deletions GSRL/Device/inc/dvc_motor.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -118,11 +118,12 @@ class MotorGM6020 : public Motor
uint8_t m_djiMotorID; // 大疆电机ID,6020电机对应1~7
uint16_t m_encoderHistory[2]; // 0:当前值 1:上一次值
int16_t m_currentRPMSpeed; // RPM
uint8_t m_mergedData[8]; // getMergedControlData 输出缓冲区

public:
MotorGM6020(uint8_t dji6020MotorID, Controller *controller, uint16_t encoderOffset = 0);
uint8_t getDjiMotorID() const;
MotorGM6020 operator+(const MotorGM6020 &otherMotor) const;
const uint8_t *getMergedControlData(MotorGM6020 &otherMotor);

protected:
bool decodeCanRxMessage(const can_rx_message_t &rxMessage) override;
Expand Down Expand Up @@ -215,12 +216,13 @@ class MotorDMmulti : public Motor
uint16_t m_encoderHistory[2]; // 0:当前值 1:上一次值
int16_t m_currentRPMSpeed; // 电机速度, 单位RPM(已除以100后的真实值)
uint8_t m_errorState; // 反馈帧D[7]错误状态字节, 具体含义详见达妙错误状态说明书
uint8_t m_mergedData[8]; // getMergedControlData 输出缓冲区

public:
MotorDMmulti(uint8_t dmMotorID, Controller *controller, uint16_t encoderOffset = 0);
uint8_t getDmMotorID() const;
uint8_t getErrorState() const;
MotorDMmulti operator+(const MotorDMmulti &otherMotor) const;
const uint8_t *getMergedControlData(MotorDMmulti &otherMotor);

protected:
bool decodeCanRxMessage(const can_rx_message_t &rxMessage) override;
Expand Down
48 changes: 26 additions & 22 deletions GSRL/Device/src/dvc_motor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -533,26 +533,25 @@ uint8_t MotorGM6020::getDjiMotorID() const
}

/**
* @brief 大疆电机加法运算符重载,用于合并CAN控制数据
* @brief 合并两个同控制ID的大疆电机CAN控制数据, 同时触发双方掉线检测
* @param otherMotor 另一个同控制ID的大疆电机
* @return const uint8_t* 合并后的8字节CAN控制数据
* @note 返回的指针指向内部静态缓冲区, 下次调用会覆盖
Comment on lines +538 to +539
Copy link

Copilot AI Apr 17, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

注释里写“返回的指针指向内部静态缓冲区”,但这里返回的是对象成员 m_mergedData(每个对象一份),并非 static 缓冲区。建议将说明改为“内部成员缓冲区/对象内缓冲区”,并明确仅同一对象的下次调用会覆盖。

Suggested change
* @return const uint8_t* 合并后的8字节CAN控制数据
* @note 返回的指针指向内部静态缓冲区, 下次调用会覆盖
* @return const uint8_t* 指向合并后的8字节CAN控制数据的指针
* @note 返回的指针指向当前对象的内部成员缓冲区m_mergedData, 仅当前对象的后续调用会覆盖该缓冲区内容

Copilot uses AI. Check for mistakes.
*/
MotorGM6020 MotorGM6020::operator+(const MotorGM6020 &otherMotor) const
const uint8_t *MotorGM6020::getMergedControlData(MotorGM6020 &otherMotor)
{
const uint8_t *selfData = this->getMotorControlData();
const uint8_t *otherData = otherMotor.getMotorControlData();

if (m_motorControlMessageID != otherMotor.m_motorControlMessageID) {
return *this;
return selfData;
}

MotorGM6020 combineMotor = *this;
if (otherMotor.m_djiMotorID < 5) {
uint8_t offset = otherMotor.m_djiMotorID * 2 - 2;
combineMotor.m_motorControlData[offset] = otherMotor.m_motorControlData[offset];
combineMotor.m_motorControlData[offset + 1] = otherMotor.m_motorControlData[offset + 1];
} else {
uint8_t offset = otherMotor.m_djiMotorID * 2 - 10;
combineMotor.m_motorControlData[offset] = otherMotor.m_motorControlData[offset];
combineMotor.m_motorControlData[offset + 1] = otherMotor.m_motorControlData[offset + 1];
}
return combineMotor;
memcpy(m_mergedData, selfData, 8);
uint8_t offset = (uint8_t)(((otherMotor.m_djiMotorID - 1) & 0x3) * 2);
m_mergedData[offset] = otherData[offset];
Comment on lines +550 to +552
Copy link

Copilot AI Apr 17, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

这里新增使用 memcpy,但本文件并未直接包含 <cstring>/<string.h>,当前是通过 alg_general.hpp 的传递包含拿到声明,耦合较强。建议在本文件或对应头文件中显式包含 <cstring>(或 <string.h>)以避免后续移除间接 include 导致编译问题。

Copilot uses AI. Check for mistakes.
m_mergedData[offset + 1] = otherData[offset + 1];
return m_mergedData;
Comment on lines +541 to +554
Copy link

Copilot AI Apr 17, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

当前实现每次都用 this->getMotorControlData() 作为合并的基底并 memcpy 覆盖整帧,因此连续多次调用该方法并不能像之前的 operator+ 那样累积合并 3+ 个电机的数据(会反复从“this 的原始控制帧”重新开始)。如果一条控制帧需要承载多电机(如一控四),建议提供可累积的合并接口(例如传入/返回可继续合并的缓冲区,或提供合并 N 个电机的函数),否则上层很难正确拼出完整 8 字节控制帧。

Copilot uses AI. Check for mistakes.
}

/******************************************************************************
Expand Down Expand Up @@ -783,20 +782,25 @@ uint8_t MotorDMmulti::getErrorState() const
}

/**
* @brief 达妙一控四电机加法运算符重载, 用于合并同控制ID下多个电机的CAN控制数据
* @brief 合并两个同控制ID的一控四固件达妙电机CAN控制数据, 同时触发双方掉线检测
* @param otherMotor 另一个同控制ID的达妙一控四电机
* @return const uint8_t* 合并后的8字节CAN控制数据
* @note 返回的指针指向内部静态缓冲区, 下次调用会覆盖
Copy link

Copilot AI Apr 17, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

注释里写“返回的指针指向内部静态缓冲区”,但这里返回的是对象成员 m_mergedData(每个对象一份),并非 static 缓冲区。建议将说明改为“内部成员缓冲区/对象内缓冲区”,并明确仅同一对象的下次调用会覆盖。

Suggested change
* @note 返回的指针指向内部静态缓冲区, 下次调用会覆盖
* @note 返回的指针指向当前对象的内部成员缓冲区, 仅当前对象的下次调用会覆盖

Copilot uses AI. Check for mistakes.
*/
MotorDMmulti MotorDMmulti::operator+(const MotorDMmulti &otherMotor) const
const uint8_t *MotorDMmulti::getMergedControlData(MotorDMmulti &otherMotor)
{
const uint8_t *selfData = this->getMotorControlData();
const uint8_t *otherData = otherMotor.getMotorControlData();

if (m_motorControlMessageID != otherMotor.m_motorControlMessageID) {
return *this;
return selfData;
}

MotorDMmulti combineMotor = *this;
uint8_t offset = (uint8_t)(((otherMotor.m_dmMotorID - 1) & 0x3) * 2);
combineMotor.m_motorControlData[offset] = otherMotor.m_motorControlData[offset];
combineMotor.m_motorControlData[offset + 1] = otherMotor.m_motorControlData[offset + 1];
return combineMotor;
memcpy(m_mergedData, selfData, 8);
uint8_t offset = (uint8_t)(((otherMotor.m_dmMotorID - 1) & 0x3) * 2);
m_mergedData[offset] = otherData[offset];
Comment on lines +799 to +801
Copy link

Copilot AI Apr 17, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

同样地,这里新增使用 memcpy,建议显式包含 <cstring>/<string.h>,避免依赖其他头文件的传递包含导致后续编译脆弱。

Copilot uses AI. Check for mistakes.
m_mergedData[offset + 1] = otherData[offset + 1];
return m_mergedData;
Comment on lines +790 to +803
Copy link

Copilot AI Apr 17, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

与上面的 GM6020 同理:该实现每次都从 this->getMotorControlData() 重新拷贝整帧,再覆盖另一个电机的 2 字节,因此无法通过多次调用累积合并 3~4 个电机(会丢失此前已合并的电机数据)。考虑到该类语义是一控四,建议提供可累积/可合并多个电机的接口,或至少在注释中明确该方法仅合并两台电机且不支持链式累积。

Copilot uses AI. Check for mistakes.
}

/******************************************************************************
Expand Down
Loading