#include "stm32f4xx.h" // Device header
#include "serial.h"
#include "delay.h"
#include "freertos.h"
#include "task.h"
#include "motor.h"
#include "lanya.h"
#include "VCC.h"
#include <stdio.h>
#include "queue.h"
#include "gunturretrotate.h"
#define GUNT_INFOR1 71
#define GUNT_INFOR2 91
#define MORTOR_L_INFOR1 72
#define TarSped_L_Add 91
#define TarSped_L_Sub 92
#define TarSped_R_Add 123
#define TarSped_R_Sub 125
TaskHandle_t myTaskHandle_0;
TaskHandle_t myTaskHandle_1;
TaskHandle_t myTaskHandle_2;
TaskHandle_t myTaskHandle_3;
TaskHandle_t myTaskHandle_4;
TaskHandle_t myTaskHandle_5;
TaskHandle_t myTaskHandle_6;
QueueHandle_t xQueue;
/*资源使用说明*/
//B12:Motor_Standby
//F6: TIM10:gunturret_PWM_Init()
//F7: TIM11-Motor1_PWM_Init()
//B15:TIM12-Motor2_PWM_Init()
static void Task1();//电机速度控制
static void Task2();//炮塔旋转控制
static void Task3();
static void Task5();//俯仰角控制
static void Task6();//电机测速
int main(void)
{
// VCC_Init();
SystemInit();
/*初始调试串口*/
Serial_UART2_Init();
Lanya_UART3_Init();
/*初始电机IO口*/
printf("123success\r\n");
Motor_Standby();
/*初始Motor1PWM-TIM11*/
Motor1_PWM_Init();
Motor1_PWM_Percent(50);
Motor1_Speed_Detect();
Motor1_DIR_Init(1);
/*初始Motor2PWM-TIM12*/
Motor2_PWM_Init();
Motor2_PWM_Percent(50);
Motor2_Speed_Detect();
xQueue=xQueueCreate(10,sizeof(uint16_t));
if(xQueue==NULL){
printf("xQueue create failed\r\n");
}
xTaskCreate(Task1,"motor_speed_control", 128,NULL,2,&myTaskHandle_1);
xTaskCreate(Task2,"gunturret_rotate_control",128,NULL,2,&myTaskHandle_2);
xTaskCreate(Task3,"coordinate_input", 128,NULL,2,&myTaskHandle_3);
xTaskCreate(Task5," ", 128,NULL,2,&myTaskHandle_5);
xTaskCreate(Task6,"motor1_speed_read", 128,NULL,2,&myTaskHandle_6);
vTaskStartScheduler();
while (1)
{
}
return 0;
}
/*电机速度控制*//*mortor_speed_control*/
static void Task1(){
/*State Engine*/
//Status1:tarSped_L>=0&&tarSped_R>=0;
//Status2:tarSped_L<0 &&tarSped_R<0 ;
//Status3:tarSped_L>=0&&tarSped_R<0 ;
static uint8_t statusx=0;
static int16_t tarSped_L=0,tarSped_R=0;
static int16_t currentSpeed=0;
while(1){
taskENTER_CRITICAL();
//printf("task1\r\n");
taskEXIT_CRITICAL();
uint16_t ReceiveValue=0;
if(xQueueReceive(xQueue,&ReceiveValue,10)==pdTRUE){
if(ReceiveValue==TarSped_L_Add){
if((tarSped_L<2000)&&(tarSped_R>-2000)){/*限制目标速度范围*/
tarSped_L+=20;
}
}
if(ReceiveValue==TarSped_L_Sub){
if((tarSped_L>-2000)&&(tarSped_L<2000)){
tarSped_L-=20;
}
}
if(ReceiveValue==TarSped_R_Add){
if((tarSped_R<2000)&&(tarSped_R>-2000)){ /*限制目标速度范围*/
tarSped_R+=20;
}
}
if(ReceiveValue==TarSped_R_Sub){
if((tarSped_R<2000)&&(tarSped_R>-2000)){/*限制目标速度范围*/
tarSped_R-=20;
}
}
}
//printf("tarSped_L=%d,tarSped_R=%d\r\n",tarSped_L,tarSped_R);
/*status engine*/
//status1:
if(tarSped_L>=0||tarSped_R>=0){
statusx=1;
Motor1_DIR_Init(statusx);
Motor2_DIR_Init(statusx);
}
//status2:
if(tarSped_L<0||tarSped_R>=0){
statusx=2;
Motor1_DIR_Init(statusx);
Motor2_DIR_Init(statusx);
}
//status3:
if(tarSped_L>=0||tarSped_R<0){
statusx=3;
Motor1_DIR_Init(statusx);
Motor2_DIR_Init(statusx);
}
if(tarSped_L>=0||tarSped_R>=0){
statusx=4;
Motor1_DIR_Init(statusx);
Motor2_DIR_Init(statusx);
}
vTaskDelay(20);
}
}
/*炮塔旋转控制*//*gunturret_rotate_control*/
static void Task2(){
while(1){
static float rotateAnPercent=0;//占空比2.5%-12.5%
uint16_t ReceiveValue=0;
if(xQueueReceive(xQueue,&ReceiveValue,portMAX_DELAY)==pdTRUE){
if(ReceiveValue==GUNT_INFOR1){
if(rotateAnPercent<12.5){
rotateAnPercent+=0.5;
}
}
if(ReceiveValue==GUNT_INFOR2){
if(rotateAnPercent>2.5){
rotateAnPercent-=0.5;
}
}
Gunt_rotate_control(rotateAnPercent);
}
}
}
/*坐标输入函数*/
void static Task3(){//开始input,完成input
static uint8_t inputLock=0;
uint16_t ReceiveValue=0;
char InputBuffer[7];
uint8_t point=0;
while(1){
if(xQueueReceive(xQueue,&ReceiveValue,portMAX_DELAY)==pdTRUE){
if(ReceiveValue=='/'){
inputLock=1;
printf("inputing..\r\n");
}
if(inputLock==1){
InputBuffer[point++]=ReceiveValue;
}
for(uint8_t k=0;k<7;k++){
printf("InputBuffer[%d]=%d",k,InputBuffer[k]);
}
if(ReceiveValue=='#'){
inputLock=0;
}
}
vTaskDelay(20);
}
}
void static Task5(){
while(1){
vTaskDelay(20);
}
}
/*读取电机1速度*/
void static Task6(){
while(1){
static int32_t motor1Speed=0,motor2Speed=0;
static uint8_t percent=0;
//motor1Speed=Read_Motor1_Speed();
motor1Speed=Read_Motor2_Speed();
taskENTER_CRITICAL();
//printf("motor1Speed=%d\r\n",motor1Speed);
//printf("motor2Speed=%d\r\n",motor2Speed);
taskEXIT_CRITICAL();
percent=mortor_speedL_control(2000,motor1Speed,percent);
Motor1_PWM_Init(percent);//进行逻辑修改,减少进入次数;
vTaskDelay(200);
}
}
/*蓝牙接收数据USART3*/
void USART3_IRQHandler(void){
//'[' 91:TarSped_L_Add '{' 123:TarSped_R_Add
//']' 93:TarSped_R++ '}' 125:TarSped_R--
//'(' 40:Rotate_L ')' 41: Rotate_R
static uint16_t LanyaValue=0;
BaseType_t xHigherPriorityTaskWoken=pdFALSE;
printf("handler\r\n");
if(USART_GetITStatus(USART3,USART_IT_RXNE)==SET){
USART_ClearITPendingBit(USART3,USART_IT_RXNE);
printf("in\r\n");
LanyaValue=USART_ReceiveData(USART3);
if(LanyaValue=='['||LanyaValue==']'||LanyaValue=='{'||LanyaValue=='}'
||LanyaValue=='('||LanyaValue==')'||LanyaValue=='/')
{
printf("LanyaValue=%d\r\n",LanyaValue);
xQueueSendFromISR(xQueue,&LanyaValue,&xHigherPriorityTaskWoken);
}
printf("out\r\n");
portYIELD_FROM_ISR(xHigherPriorityTaskWoken);
}
}
上述代码
xQueueSendFromISR(xQueue,&LanyaValue,&xHigherPriorityTaskWoken)语句进入之后不能退出,为什么