DS402是建立在DS301(DS301就是一个通讯协议栈)基础之上的伺服类控制协议。
ds402.c
#include <stm32g0xx_hal.h> //Sets up the correct chip specifc defines required by arm_math
//#define ARM_MATH_CM4
//#include <arm_math.h>#include <mcpwm.h>#include <stdlib.h>
#include <math.h>
//#include <cmsis_os.h>#include <main.h>
#include <adc.h>
#include <tim.h>
#include <spi.h>
#include <utils.h>
#include "delay.h"
#include "usart.h"short operation_mode=1,operation_mode_buff=0;
union Status_uint16_t status_word;
union Control_uint16_t control_word,control_word_b;
u16 motor_on=0;
uint8_t tmp[20];//测试用
void DS402_process(void)
{ if(control_word.all!=control_word_b.all){//control_word_b.all=0x1234;//测试用
// SendByte(control_word.all>>8);//测试用
// SendByte(control_word.all);//测试用// SendByte(Error_State.all>>24);//测试用
// SendByte(Error_State.all>>16);//测试用
// SendByte(Error_State.all>>8);//测试用
// SendByte(Error_State.all);//测试用switch(control_word.all){case 0x0f:if(Error_State.all==0){SendMessage("case0x0f\r\n");//测试用//Scop_Start=1;V_current_control_integral_d=0;V_current_control_integral_q=0;Iq_demand=0;speed_demand=0;position_demand=0;snprintf(tmp,sizeof(tmp),"mode is %d",operation_mode);//测试用SendMessage(tmp);//测试用switch(operation_mode){case 14:case 12:case 11:case 17:phase_dir=1;commutation_founded=1;//kci=0; case -8:if(Error_State.all==0){start_pwm(&htim1);motor_on=1;}break;case 1:SendMessage("case0x0f3333\r\n");//测试用case 3:target_speed_now=real_speed_filter*1000;target_pos_now=pos_actual; speed_demand=real_speed_filter;position_demand=pos_actual;case 4:case 2:case 5:case 7: //snprintf(tmp,sizeof(tmp),"mode is %d",commutation_founded);sprintf(tmp,"mode is %d",commutation_founded);//按照 十进制 打印 communtation_founded SendMessage(tmp);//测试用if(commutation_founded==0){find_commutation();delay_ms(1);}if(commutation_founded==1){if(Error_State.all==0){start_pwm(&htim1);motor_on=1;}}break;default:break;}}SendMessage("case0x0f4444\r\n");//测试用break;case 0x06:SendMessage("case06\r\n");//测试用stop_pwm(&htim1);motor_on=0;break;case 0x86:SendMessage("case86\r\n");//测试用stop_pwm(&htim1);motor_on=0;drv8301_error=0;Error_State.all=0;ENC_Counting_Error=0;break;default:break;}control_word_b=control_word;}if(Error_State.all){SendMessage("ifError_State\n");//测试用stop_pwm(&htim1);motor_on=0; }
}
函数执行过程:
1、在
void Auto_reserve_process(void)中
让control_word.all=0x06(初始值),control_word_b.all=0x00(初始值)所以它会走if(control_word.all!=control_word_b.all)分支
2、在if分支中,switch(control_word.all)分支走的是case 0x06,执行了如下代码:
3、control_word_b=control_word;使得control_word_b=0x06,
4、Error_State.all=0x00000008,所以执行了如下代码:
然后每次进来control_word.all与control_word_b.all都不相等
会一直执行如下函数:
所以测试串口的程序会一直发送






















