teamALI / Mbed 2 deprecated HB2018

Dependencies:   mbed FreeRTOS

userTask.cpp

Committer:
takeru0x1103
Date:
2018-12-05
Revision:
20:0394e15412c3
Parent:
18:5aa48aec9cae
Child:
21:78302ecdb661

File content as of revision 20:0394e15412c3:

//RTOS関連
#include "FreeRTOS.h"
#include "task.h"
#include "queue.h"
#include "globalFlags.h"
#include "HbManager.h"
#include "hbCommand.h"
#include "uart.h"
#include "fpga.h"

//タスクハンドル(停止とか再開に必要)
static xTaskHandle  tskHandle[3]={NULL,};

//タスク停止
//------------------
static void taskStop(int iId){
    sp.printf("Task[%d] Stop\r\n" , iId);
    vTaskSuspend(tskHandle[iId]);//タスクを止める
}

//タスク再開
//------------------
static void taskStart(int iId){
    sp.printf("Task[%d] Start!!\r\n" , iId);
    vTaskResume(tskHandle[iId]);//タスク再開
}

//========================================================
//ホバーバイク制御タスク
//========================================================
void taskHbControl(void *pvParameters){
    int8_t          *pcTaskName;
    portTickType    xLastWakeTime;
    HbManager       hb;
    
    pcTaskName = (int8_t *) pvParameters;
    xLastWakeTime = xTaskGetTickCount();
    
    //
    while(1){
        led1=!led1;
        
        hb.getAttitude();//現在角度読み出し
        hb.controlAttitude();//姿勢制御
        hb.controlMotor();//モーター指令出し
        hb.controlEngine();//エンジン回転数読み出し
        
        
        //表示フラグを落とす(けどモニタフラグが立ってる箇所は残る)
        if(gf_Print.flg!=0){
            gf_Print.flg=gf_Mon.flg;
            sp.printf("\r\n");
        }
        
        //次の周期まで待つ
        vTaskDelayUntil(&xLastWakeTime, 20 / portTICK_RATE_MS );
    }
}
//========================================================
//コマンド解析タスク
//========================================================
void taskCmdParser(void *pvParameters){
    int8_t *pcTaskName;
    portTickType xLastWakeTime;
    
    pcTaskName = (int8_t *) pvParameters;
    xLastWakeTime = xTaskGetTickCount();
        
    vTaskDelay(300);//制御タスクを先に走らせたいので待たす
    
    while(1){
        led2=!led2;
        //コマンド解釈
        commandParse();
        
        //
        switch(gf.state){
            CHK_ENT:    //チェックエンター
                taskStop(0);//制御タスクを止める
                taskStart(2);//デバッグタスク再開
                break;
            CHK_EXIT:   //チェックステート脱出
                taskStop(2);//デバッグタスクを止める
                taskStart(0);//制御タスク再開
                break;
            default:
                break;
        }//switch

        //次の周期まで待つ
        vTaskDelayUntil(&xLastWakeTime, 100 / portTICK_RATE_MS );
    }
}
//========================================================
//デバッグ用タスク
//========================================================
void taskDebug(void *pvParameters){
    int8_t *pcTaskName;
    portTickType xLastWakeTime;
    
    pcTaskName = (int8_t *) pvParameters;
    xLastWakeTime = xTaskGetTickCount();
        
    taskStop(2);
    
    while(1){
        switch(gf.state){
            CHK_MOT://モーターチェック
                //hb.
                //CHK_MOT:fpgaMotorChk();
                break;
        }
 
        //自らタスク停止させて次の周期まで待つ
        taskStop(2);
    }
}

//-------------------------------------------------------------
//初期化:タスク登録
//-------------------------------------------------------------
void taskInit(){
    portBASE_TYPE   TaskRtn;
    
    //制御タスク
    TaskRtn= xTaskCreate(taskHbControl, (signed portCHAR *)"Task50Hz", 192, NULL, 2, &tskHandle[0]);
    if(TaskRtn==pdTRUE){printf("Hoverbike Control task Set\r\n");}
    
    //コマンド解析タスク
    TaskRtn= xTaskCreate(taskCmdParser, (signed portCHAR *)"TaskParser", 192, NULL, 1, &tskHandle[1]);
    if(TaskRtn==pdTRUE){printf("Command Parser task Set\r\n");}
    
    //デバッグタスク
    TaskRtn= xTaskCreate(taskDebug, (signed portCHAR *)"TaskDebug", 192, NULL, 0, &tskHandle[2]);
    if(TaskRtn==pdTRUE){printf("Debug Parser task Set\r\n");}
    
    //RTOSカーネルの起動
    vTaskStartScheduler();
}