Skip to content

Latest commit

 

History

History
35 lines (27 loc) · 839 Bytes

GM6020位置控制.md

File metadata and controls

35 lines (27 loc) · 839 Bytes

GM6020 电机位置闭环控制

简介

本例程是一个 FreeRTOS 任务,演示如何使用本库中的 GM6020 电机驱动和 PID 控制器实现电机编码器位置闭环控制。

代码

#include "can.h"
#include "cmsis_os.h"
#include "librm.hpp"

using rm::hal::Can;
using namespace rm::device;
using namespace rm::modules::algorithm;

extern "C" {

void ExampleGM6020Task(const void *pv_arg) {
  Can can1(hcan1);
  GM6020 gm6020(can1, 1);
  RingPID<PIDType::kPosition> pid(5, 0, 0, 30000, 0, 8191);
  can1.SetFilter(0, 0);
  can1.Begin();

  for (;;) {
    pid.Update(3000, gm6020.encoder());  // 更新PID控制器输出
    gm6020.SetCurrent(static_cast<int16_t>(pid.value()));  // 设置电机电流
    DjiMotor<>::SendCommand();                             // 发送控制报文
    osDelay(1);
  }
}
}