Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
userTask.cpp@22:24c9c2dedca9, 2018-12-06 (annotated)
- Committer:
- MasashiNomura
- Date:
- Thu Dec 06 11:03:13 2018 +0000
- Revision:
- 22:24c9c2dedca9
- Parent:
- 21:78302ecdb661
- Child:
- 23:79e20be4bc5b
Temp State Transfer
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
takeru0x1103 | 21:78302ecdb661 | 1 | //RTOS関連------------------ |
takeru0x1103 | 1:15ab74f0d0f1 | 2 | #include "FreeRTOS.h" |
takeru0x1103 | 1:15ab74f0d0f1 | 3 | #include "task.h" |
takeru0x1103 | 21:78302ecdb661 | 4 | //#include "queue.h" |
takeru0x1103 | 21:78302ecdb661 | 5 | //------------------RTOS関連 |
takeru0x1103 | 21:78302ecdb661 | 6 | |
takeru0x1103 | 17:f9610f3cfa1b | 7 | #include "globalFlags.h" |
takeru0x1103 | 17:f9610f3cfa1b | 8 | #include "HbManager.h" |
takeru0x1103 | 18:5aa48aec9cae | 9 | #include "hbCommand.h" |
takeru0x1103 | 18:5aa48aec9cae | 10 | #include "uart.h" |
takeru0x1103 | 1:15ab74f0d0f1 | 11 | |
takeru0x1103 | 18:5aa48aec9cae | 12 | //タスクハンドル(停止とか再開に必要) |
takeru0x1103 | 18:5aa48aec9cae | 13 | static xTaskHandle tskHandle[3]={NULL,}; |
takeru0x1103 | 1:15ab74f0d0f1 | 14 | |
takeru0x1103 | 21:78302ecdb661 | 15 | //------------------ |
takeru0x1103 | 18:5aa48aec9cae | 16 | //タスク停止 |
takeru0x1103 | 18:5aa48aec9cae | 17 | //------------------ |
takeru0x1103 | 18:5aa48aec9cae | 18 | static void taskStop(int iId){ |
takeru0x1103 | 18:5aa48aec9cae | 19 | sp.printf("Task[%d] Stop\r\n" , iId); |
takeru0x1103 | 18:5aa48aec9cae | 20 | vTaskSuspend(tskHandle[iId]);//タスクを止める |
takeru0x1103 | 18:5aa48aec9cae | 21 | } |
takeru0x1103 | 18:5aa48aec9cae | 22 | |
takeru0x1103 | 21:78302ecdb661 | 23 | //------------------ |
takeru0x1103 | 18:5aa48aec9cae | 24 | //タスク再開 |
takeru0x1103 | 18:5aa48aec9cae | 25 | //------------------ |
takeru0x1103 | 18:5aa48aec9cae | 26 | static void taskStart(int iId){ |
takeru0x1103 | 18:5aa48aec9cae | 27 | sp.printf("Task[%d] Start!!\r\n" , iId); |
takeru0x1103 | 18:5aa48aec9cae | 28 | vTaskResume(tskHandle[iId]);//タスク再開 |
takeru0x1103 | 18:5aa48aec9cae | 29 | } |
takeru0x1103 | 18:5aa48aec9cae | 30 | |
takeru0x1103 | 18:5aa48aec9cae | 31 | //======================================================== |
takeru0x1103 | 21:78302ecdb661 | 32 | //コマンド解析タスク |
takeru0x1103 | 21:78302ecdb661 | 33 | //======================================================== |
takeru0x1103 | 21:78302ecdb661 | 34 | void taskCmdParser(void *pvParameters){ |
takeru0x1103 | 21:78302ecdb661 | 35 | // int8_t *pcTaskName = (int8_t *) pvParameters;; |
takeru0x1103 | 21:78302ecdb661 | 36 | portTickType xLastWakeTime = xTaskGetTickCount(); |
takeru0x1103 | 21:78302ecdb661 | 37 | |
takeru0x1103 | 21:78302ecdb661 | 38 | // vTaskDelay(300);//制御タスクを先に走らせたいので待たす |
takeru0x1103 | 21:78302ecdb661 | 39 | |
takeru0x1103 | 21:78302ecdb661 | 40 | while(1){ |
takeru0x1103 | 21:78302ecdb661 | 41 | led2=!led2; |
takeru0x1103 | 21:78302ecdb661 | 42 | //コマンド解釈 |
takeru0x1103 | 21:78302ecdb661 | 43 | commandParse(); |
takeru0x1103 | 21:78302ecdb661 | 44 | |
takeru0x1103 | 21:78302ecdb661 | 45 | // |
takeru0x1103 | 21:78302ecdb661 | 46 | if(gf_Armed){ |
MasashiNomura | 22:24c9c2dedca9 | 47 | setState(WAKEUP); |
MasashiNomura | 22:24c9c2dedca9 | 48 | gf_Armed = false; |
MasashiNomura | 22:24c9c2dedca9 | 49 | // taskStop(0);//制御タスクを止める |
MasashiNomura | 22:24c9c2dedca9 | 50 | // taskStart(2);//デバッグタスク再開 |
MasashiNomura | 22:24c9c2dedca9 | 51 | } |
MasashiNomura | 22:24c9c2dedca9 | 52 | // else{ |
MasashiNomura | 22:24c9c2dedca9 | 53 | // taskStop(2);//デバッグタスクを止める |
MasashiNomura | 22:24c9c2dedca9 | 54 | // taskStart(0);//制御タスク再開 |
MasashiNomura | 22:24c9c2dedca9 | 55 | // }//switch |
takeru0x1103 | 21:78302ecdb661 | 56 | |
takeru0x1103 | 21:78302ecdb661 | 57 | //次の周期まで待つ |
takeru0x1103 | 21:78302ecdb661 | 58 | vTaskDelayUntil(&xLastWakeTime, 50 / portTICK_RATE_MS ); |
takeru0x1103 | 21:78302ecdb661 | 59 | } |
takeru0x1103 | 21:78302ecdb661 | 60 | } |
takeru0x1103 | 21:78302ecdb661 | 61 | //======================================================== |
takeru0x1103 | 17:f9610f3cfa1b | 62 | //ホバーバイク制御タスク |
takeru0x1103 | 18:5aa48aec9cae | 63 | //======================================================== |
takeru0x1103 | 17:f9610f3cfa1b | 64 | void taskHbControl(void *pvParameters){ |
takeru0x1103 | 21:78302ecdb661 | 65 | // int8_t *pcTaskName = (int8_t *) pvParameters;; |
takeru0x1103 | 21:78302ecdb661 | 66 | portTickType xLastWakeTime = xTaskGetTickCount(); |
takeru0x1103 | 21:78302ecdb661 | 67 | |
takeru0x1103 | 17:f9610f3cfa1b | 68 | HbManager hb; |
takeru0x1103 | 8:1ca49cb18290 | 69 | |
takeru0x1103 | 18:5aa48aec9cae | 70 | // |
takeru0x1103 | 1:15ab74f0d0f1 | 71 | while(1){ |
takeru0x1103 | 16:05b9e44889f1 | 72 | led1=!led1; |
takeru0x1103 | 21:78302ecdb661 | 73 | //▼①状態読み出し系 |
MasashiNomura | 22:24c9c2dedca9 | 74 | hb.getAttitude(); //現在角度読み出し |
MasashiNomura | 22:24c9c2dedca9 | 75 | hb.controlEngine(); //エンジン回転数読み出し |
MasashiNomura | 22:24c9c2dedca9 | 76 | hb.getUserCommand(); //操作ボタン状態読み出し |
takeru0x1103 | 18:5aa48aec9cae | 77 | |
takeru0x1103 | 21:78302ecdb661 | 78 | //▼②ステート遷移 |
MasashiNomura | 22:24c9c2dedca9 | 79 | switch(gf_State){ |
MasashiNomura | 22:24c9c2dedca9 | 80 | case SLEEP: |
MasashiNomura | 22:24c9c2dedca9 | 81 | if(gf_Dbg){ |
MasashiNomura | 22:24c9c2dedca9 | 82 | taskStart(2); |
MasashiNomura | 22:24c9c2dedca9 | 83 | } |
MasashiNomura | 22:24c9c2dedca9 | 84 | break; |
MasashiNomura | 22:24c9c2dedca9 | 85 | case WAKEUP: |
MasashiNomura | 22:24c9c2dedca9 | 86 | break; |
MasashiNomura | 22:24c9c2dedca9 | 87 | case STANDBY: |
MasashiNomura | 22:24c9c2dedca9 | 88 | break; |
MasashiNomura | 22:24c9c2dedca9 | 89 | case IDLE: |
MasashiNomura | 22:24c9c2dedca9 | 90 | break; |
MasashiNomura | 22:24c9c2dedca9 | 91 | case TAKE_OFF: |
MasashiNomura | 22:24c9c2dedca9 | 92 | break; |
MasashiNomura | 22:24c9c2dedca9 | 93 | case GROUND: |
MasashiNomura | 22:24c9c2dedca9 | 94 | break; |
MasashiNomura | 22:24c9c2dedca9 | 95 | case HOVER: |
MasashiNomura | 22:24c9c2dedca9 | 96 | break; |
MasashiNomura | 22:24c9c2dedca9 | 97 | case DRIVE: |
MasashiNomura | 22:24c9c2dedca9 | 98 | break; |
MasashiNomura | 22:24c9c2dedca9 | 99 | case EMGGND: |
MasashiNomura | 22:24c9c2dedca9 | 100 | break; |
MasashiNomura | 22:24c9c2dedca9 | 101 | } |
takeru0x1103 | 21:78302ecdb661 | 102 | |
takeru0x1103 | 21:78302ecdb661 | 103 | //▼③各種設定 |
takeru0x1103 | 21:78302ecdb661 | 104 | //hb.controlAttitude(); //姿勢制御 |
takeru0x1103 | 21:78302ecdb661 | 105 | //hb.controlMotor();//モーター指令出し |
takeru0x1103 | 18:5aa48aec9cae | 106 | |
takeru0x1103 | 18:5aa48aec9cae | 107 | |
takeru0x1103 | 18:5aa48aec9cae | 108 | //表示フラグを落とす(けどモニタフラグが立ってる箇所は残る) |
takeru0x1103 | 18:5aa48aec9cae | 109 | if(gf_Print.flg!=0){ |
takeru0x1103 | 18:5aa48aec9cae | 110 | gf_Print.flg=gf_Mon.flg; |
takeru0x1103 | 18:5aa48aec9cae | 111 | sp.printf("\r\n"); |
takeru0x1103 | 18:5aa48aec9cae | 112 | } |
takeru0x1103 | 1:15ab74f0d0f1 | 113 | |
takeru0x1103 | 1:15ab74f0d0f1 | 114 | //次の周期まで待つ |
takeru0x1103 | 1:15ab74f0d0f1 | 115 | vTaskDelayUntil(&xLastWakeTime, 20 / portTICK_RATE_MS ); |
takeru0x1103 | 1:15ab74f0d0f1 | 116 | } |
takeru0x1103 | 1:15ab74f0d0f1 | 117 | } |
takeru0x1103 | 18:5aa48aec9cae | 118 | //======================================================== |
takeru0x1103 | 21:78302ecdb661 | 119 | //タスク2:デバッグ用タスク |
takeru0x1103 | 18:5aa48aec9cae | 120 | //======================================================== |
takeru0x1103 | 18:5aa48aec9cae | 121 | void taskDebug(void *pvParameters){ |
takeru0x1103 | 21:78302ecdb661 | 122 | |
takeru0x1103 | 21:78302ecdb661 | 123 | taskStop(2);//自ら止めてレジュームされるまで待つ |
takeru0x1103 | 18:5aa48aec9cae | 124 | |
takeru0x1103 | 21:78302ecdb661 | 125 | //この関数をリターンしてしまうと他のタスクも止まるのでwhileで回すこと! |
takeru0x1103 | 18:5aa48aec9cae | 126 | while(1){ |
takeru0x1103 | 21:78302ecdb661 | 127 | |
MasashiNomura | 22:24c9c2dedca9 | 128 | |
MasashiNomura | 22:24c9c2dedca9 | 129 | |
MasashiNomura | 22:24c9c2dedca9 | 130 | gf_Dbg = false; |
takeru0x1103 | 21:78302ecdb661 | 131 | //自タスク停止させて次の周期まで待つ |
takeru0x1103 | 18:5aa48aec9cae | 132 | taskStop(2); |
takeru0x1103 | 18:5aa48aec9cae | 133 | } |
takeru0x1103 | 18:5aa48aec9cae | 134 | } |
takeru0x1103 | 17:f9610f3cfa1b | 135 | |
takeru0x1103 | 1:15ab74f0d0f1 | 136 | //------------------------------------------------------------- |
takeru0x1103 | 17:f9610f3cfa1b | 137 | //初期化:タスク登録 |
takeru0x1103 | 1:15ab74f0d0f1 | 138 | //------------------------------------------------------------- |
takeru0x1103 | 1:15ab74f0d0f1 | 139 | void taskInit(){ |
takeru0x1103 | 21:78302ecdb661 | 140 | portBASE_TYPE TaskRtn;//タスククリエイトの結果を受け取る型 |
takeru0x1103 | 18:5aa48aec9cae | 141 | |
takeru0x1103 | 21:78302ecdb661 | 142 | //タスク0:コマンド解析タスク |
takeru0x1103 | 21:78302ecdb661 | 143 | TaskRtn= xTaskCreate(taskCmdParser, (signed portCHAR *)"TaskCmd", 32, NULL, 1, &tskHandle[0]); |
takeru0x1103 | 21:78302ecdb661 | 144 | if(TaskRtn==pdTRUE){sp.printf("Task0 Set : Command parser\r\n");} |
takeru0x1103 | 21:78302ecdb661 | 145 | |
takeru0x1103 | 21:78302ecdb661 | 146 | //タスク1:制御タスク |
takeru0x1103 | 21:78302ecdb661 | 147 | TaskRtn= xTaskCreate(taskHbControl, (signed portCHAR *)"TaskHbCont", 192, NULL, 2, &tskHandle[1]); |
takeru0x1103 | 21:78302ecdb661 | 148 | if(TaskRtn==pdTRUE){sp.printf("Task1 Set : Hoverbike Control\r\n");} |
takeru0x1103 | 21:78302ecdb661 | 149 | |
takeru0x1103 | 21:78302ecdb661 | 150 | //タスク2:デバッグタスク |
takeru0x1103 | 21:78302ecdb661 | 151 | TaskRtn= xTaskCreate(taskDebug, (signed portCHAR *)"TaskDebug", 64, NULL, 0, &tskHandle[2]); |
takeru0x1103 | 21:78302ecdb661 | 152 | if(TaskRtn==pdTRUE){sp.printf("Task2 Set : Debug\r\n");} |
takeru0x1103 | 8:1ca49cb18290 | 153 | |
takeru0x1103 | 21:78302ecdb661 | 154 | //RTOSカーネル起動(タスクが走り出す) |
takeru0x1103 | 1:15ab74f0d0f1 | 155 | vTaskStartScheduler(); |
takeru0x1103 | 1:15ab74f0d0f1 | 156 | } |
takeru0x1103 | 1:15ab74f0d0f1 | 157 | |
takeru0x1103 | 1:15ab74f0d0f1 | 158 |