今天主要记录一下昨天闭环移植的成果,移植了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参数还没有时间去调试,也有网上推荐使用云台电机测试。