继上篇之后,大概看了下源码,今天移植的主要任务是commander,方便以后串口发指令调试电机参数。
由于odrive主板有UART4可用,目前使用它打印信息,并接收指令,后续再将usb虚拟串口专门用来接收指令。
uart4接收中断
void UART4_IRQHandler(void)
{
/* USER CODE BEGIN UART4_IRQn 0 */
uint8_t recDat;
/* USER CODE END UART4_IRQn 0 */
// HAL_UART_IRQHandler(&huart4);
/* USER CODE BEGIN UART4_IRQn 1 */
uint32_t isrflags = READ_REG(huart4.Instance->SR);
if ((isrflags & USART_SR_RXNE) != RESET)
{
recDat = (uint8_t)(huart4.Instance->DR & (uint8_t)0x00FF);
recvTask(recDat);
}
/* USER CODE END UART4_IRQn 1 */
}
保持commander 接收数据和解析指令。
// Commander command = Commander(Serial);
Commander *pCommander;
void recvTask(uint8_t ch)
{
// int ch ;//= serial.read();
if(pCommander->com_port!=nullptr)
{
pCommander->received_chars[pCommander->rec_cnt++] = (char)ch;
// end of user input
if(pCommander->echo)
pCommander->print((char)ch);
if (pCommander->isSentinel(ch)) {
// execute the user command
pCommander->run(pCommander->received_chars);
// reset the command buffer
pCommander->received_chars[0] = 0;
pCommander->rec_cnt=0;
}
if (pCommander->rec_cnt>=MAX_COMMAND_LENGTH) { // prevent buffer overrun if message is too long
pCommander->received_chars[0] = 0;
pCommander->rec_cnt=0;
}
}
}
并且开了一个基本定时器6,84MHz 分频为1MHz 周期为50000个,即最大周期50ms
time_utils.cpp delay函数里变量即在sysTick(1KHz)中断里如果大于0 减 1
#include "time_utils.h"
#include "tim.h"
volatile unsigned int tim_cnt = 0;
void delay(unsigned long dly)
{
tim_cnt=dly;
while(tim_cnt);
}
void delayMicroseconds(unsigned long dly)
{
unsigned long t = _micros() + dly;
if(t>=50000)
{
t -= 50000;
while(_micros() < 50000){};
while(_micros() < t){};
}
else
{
while( _micros() < t ){};
}
}
// function buffering delay()
// arduino uno function doesn't work well with interrupts
void _delay(unsigned long ms){
unsigned long t = _micros() + ms*1000;
if(t>=50000)
{
t -= 50000;
while(_micros() < 50000){};
while(_micros() < t){};
}
else
{
while( _micros() < t ){};
}
}
// function buffering _micros()
// arduino function doesn't work well with interrupts
unsigned long _micros(){
uint16_t t= __HAL_TIM_GET_COUNTER(&htim6);
return t;
}
今天 基本将drv8301及大部分代码添加到了工程里,接下来就是将timer,pwm,电流采样,有感采集接入 simpleFoc,即可以调试起来。源码工程更新了:https://github.com/wenyezhong/simpleFoc