移植 simpleFoc笔记(四)

本文记录了将encoder和InlineCurrentSense移植到STM32平台的过程,使用了odrive测试电机和TLB5012编码器。通过中断处理和编码器函数注册实现了电机角度和速度的实时监测。同时,利用DRV8301进行电流采集,通过ADC中断获取转换结果。测试结果显示编码器功能正常,但PID参数未调优,建议使用云台电机进一步测试。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

今天主要记录一下昨天闭环移植的成果,移植了encoder和InlineCurrentSense

也就是电流采集和编码器,本主用的是odrive测试电机,自带了一块TLB5012,ABZ接口

程序中开了三个外中断,嫁接encoder.cpp

一方面是中断处理

typedef void (*pf_callbakck)(void);

pf_callbakck functionA=nullptr;
pf_callbakck functionB=nullptr;
pf_callbakck functionZ=nullptr;

void HAL_GPIO_EXTI_Callback(uint16_t GPIO_Pin)
{
  switch(GPIO_Pin)
  {
    case M0_ENC_A_Pin:{
      functionA();
    }break;
    case M0_ENC_B_Pin:{
      functionB();
    }break;
    case M0_ENC_Z_Pin:{
      functionZ();
    }break;
    default:break;
  }
  
}

另一方面是中断注册编码器三个函数

void Encoder::enableInterrupts(void (*doA)(), void(*doB)(), void(*doIndex)())
{

  GPIO_InitTypeDef GPIO_InitStruct = {0};
  if(doA != nullptr) functionA=doA;
  if(doB != nullptr) functionB=doB;

  GPIO_InitStruct.Pin = M0_ENC_A_Pin|M0_ENC_B_Pin;
  GPIO_InitStruct.Pull = GPIO_NOPULL;
  switch(quadrature){
    case Quadrature::ON:      
      /*Configure GPIO pins : PBPin PBPin */
      
      GPIO_InitStruct.Mode = GPIO_MODE_IT_RISING_FALLING;
      
      break;
    case Quadrature::OFF:
      // A callback and B callback

      GPIO_InitStruct.Mode = GPIO_MODE_IT_RISING;

      break;
  }
  HAL_GPIO_Init(GPIOB, &GPIO_InitStruct);

  GPIO_InitStruct.Pin = M0_ENC_Z_Pin;
  GPIO_InitStruct.Mode = GPIO_MODE_IT_RISING_FALLING;
  GPIO_InitStruct.Pull = GPIO_NOPULL;
  HAL_GPIO_Init(M0_ENC_Z_GPIO_Port, &GPIO_InitStruct);

  // if index used initialize the index interrupt
  if(hasIndex() && doIndex != nullptr) 
    functionZ=doIndex;
}

编码器测试如下

Encoder encoder = Encoder(1,2,4096,3);
// interrupt routine intialisation
void doA(){encoder.handleA();}
void doB(){encoder.handleB();}
void doIndex(){encoder.handleIndex();}

void setup() {
  // monitoring port
//   Serial.begin(115200);

  // enable/disable quadrature mode
  encoder.quadrature = Quadrature::ON;

  // check if you need internal pullups
  encoder.pullup = Pullup::USE_EXTERN;
  
  // initialise encoder hardware
  encoder.init();
  // hardware interrupt enable
  encoder.enableInterrupts(doA, doB,doIndex);

  Serial.println("Encoder ready");
  _delay(1000);
}

void loop() {
  // iterative function updating the sensor internal variables
  // it is usually called in motor.loopFOC()
  // not doing much for the encoder though
  encoder.update();
  // display the angle and the angular velocity to the terminal
  Serial.print(encoder.getAngle());
  Serial.print("\t");
  Serial.println(encoder.getVelocity());
}

测试时,手转到电机一圈,即打印信息为6.28,即该部分代码移植正确。

电流采集,即用DRV8301的DC_CAL, gain为40V/V  定时器TRGO触发ADC注入式转换,中断接收转换结果。

即整个移植工作告一段落,在测试时,由于手上只有A2212航模电机,闭环也工作起来了,但发烫pid参数还没有时间去调试,也有网上推荐使用云台电机测试。

源码:GitHub - wenyezhong/simpleFoc: 移植simpleFoc到odrive主板上笔记

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值