2号機

Dependencies:   HCSR04 mbed

Fork of Nucleo_SS2 by SS2

Committer:
Morimoto448
Date:
Mon Dec 04 04:30:44 2017 +0000
Revision:
1:aabe6887447c
Parent:
0:7e3b7c017977
Child:
2:f568ec2e6955
test;

Who changed what in which revision?

UserRevisionLine numberNew contents of line
Morimoto448 0:7e3b7c017977 1 #include "mbed.h"
Morimoto448 0:7e3b7c017977 2
Morimoto448 0:7e3b7c017977 3 PwmOut mypwm4(D4);
Morimoto448 1:aabe6887447c 4 PwmOut mypwm6(D6);
Morimoto448 0:7e3b7c017977 5 PwmOut mypwm2(D2);
Morimoto448 0:7e3b7c017977 6 PwmOut mypwm3(D3);
Morimoto448 0:7e3b7c017977 7
Morimoto448 0:7e3b7c017977 8 Ticker flag;
Morimoto448 1:aabe6887447c 9 Timer t;
Morimoto448 0:7e3b7c017977 10
Morimoto448 0:7e3b7c017977 11 DigitalOut myled(LED1);
Morimoto448 0:7e3b7c017977 12
Morimoto448 0:7e3b7c017977 13
Morimoto448 0:7e3b7c017977 14 #define STATE_A (0)
Morimoto448 0:7e3b7c017977 15 #define STATE_B (1)
Morimoto448 0:7e3b7c017977 16 #define STATE_C (2)
Morimoto448 0:7e3b7c017977 17 #define STATE_D (3)
Morimoto448 0:7e3b7c017977 18 #define STATE_E (4)
Morimoto448 0:7e3b7c017977 19
Morimoto448 0:7e3b7c017977 20 char mode;
Morimoto448 0:7e3b7c017977 21 int wait_flag;
Morimoto448 1:aabe6887447c 22 int timer;
Morimoto448 0:7e3b7c017977 23
Morimoto448 0:7e3b7c017977 24 void flg(){
Morimoto448 1:aabe6887447c 25 timer = t.read();
Morimoto448 0:7e3b7c017977 26 }
Morimoto448 0:7e3b7c017977 27
Morimoto448 0:7e3b7c017977 28 int main() {
Morimoto448 0:7e3b7c017977 29
Morimoto448 1:aabe6887447c 30 t.start();
Morimoto448 0:7e3b7c017977 31 mode = STATE_E;
Morimoto448 0:7e3b7c017977 32 wait_flag = 0;
Morimoto448 0:7e3b7c017977 33
Morimoto448 0:7e3b7c017977 34 mypwm4.period_ms(100);
Morimoto448 1:aabe6887447c 35 mypwm6.period_ms(100);
Morimoto448 0:7e3b7c017977 36 mypwm2.period_ms(100);
Morimoto448 0:7e3b7c017977 37 mypwm3.period_ms(100);
Morimoto448 0:7e3b7c017977 38
Morimoto448 1:aabe6887447c 39 flag.attach(&flg, 1.0);
Morimoto448 0:7e3b7c017977 40
Morimoto448 0:7e3b7c017977 41
Morimoto448 1:aabe6887447c 42
Morimoto448 1:aabe6887447c 43 while(1){
Morimoto448 1:aabe6887447c 44
Morimoto448 1:aabe6887447c 45 switch(timer)
Morimoto448 1:aabe6887447c 46 {
Morimoto448 1:aabe6887447c 47 case 8.0:
Morimoto448 1:aabe6887447c 48 wait_flag = 1;
Morimoto448 1:aabe6887447c 49 mode = STATE_A;
Morimoto448 1:aabe6887447c 50 break;
Morimoto448 1:aabe6887447c 51 case 13.0:
Morimoto448 1:aabe6887447c 52 wait_flag = 1;
Morimoto448 1:aabe6887447c 53 mode = STATE_B;
Morimoto448 1:aabe6887447c 54 break;
Morimoto448 1:aabe6887447c 55 case 17.0:
Morimoto448 1:aabe6887447c 56 wait_flag = 1;
Morimoto448 1:aabe6887447c 57 mode = STATE_A;
Morimoto448 1:aabe6887447c 58 break;
Morimoto448 1:aabe6887447c 59 case 27.0:
Morimoto448 1:aabe6887447c 60 wait_flag = 1;
Morimoto448 1:aabe6887447c 61 mode = STATE_D;
Morimoto448 1:aabe6887447c 62 break;
Morimoto448 1:aabe6887447c 63 case 42.0:
Morimoto448 1:aabe6887447c 64 wait_flag = 1;
Morimoto448 1:aabe6887447c 65 mode = STATE_A;
Morimoto448 1:aabe6887447c 66 break;
Morimoto448 1:aabe6887447c 67 case 50.0:
Morimoto448 1:aabe6887447c 68 wait_flag = 1;
Morimoto448 1:aabe6887447c 69 mode = STATE_C;
Morimoto448 1:aabe6887447c 70 break;
Morimoto448 1:aabe6887447c 71 case 54.0:
Morimoto448 1:aabe6887447c 72 wait_flag = 1;
Morimoto448 1:aabe6887447c 73 mode = STATE_A;
Morimoto448 1:aabe6887447c 74 break;
Morimoto448 1:aabe6887447c 75
Morimoto448 1:aabe6887447c 76 }
Morimoto448 1:aabe6887447c 77
Morimoto448 1:aabe6887447c 78 // 状態が切り替わるときは一時停止
Morimoto448 0:7e3b7c017977 79 if(wait_flag==1)
Morimoto448 0:7e3b7c017977 80 {
Morimoto448 0:7e3b7c017977 81 // フラグの初期化
Morimoto448 0:7e3b7c017977 82 wait_flag = 0;
Morimoto448 0:7e3b7c017977 83 myled = 0; // LED消灯
Morimoto448 0:7e3b7c017977 84 // 左モータの制御
Morimoto448 0:7e3b7c017977 85 mypwm4.pulsewidth_ms(80);
Morimoto448 1:aabe6887447c 86 mypwm6.pulsewidth_ms(80);
Morimoto448 0:7e3b7c017977 87 // 右モータの制御
Morimoto448 0:7e3b7c017977 88 mypwm2.pulsewidth_ms(80);
Morimoto448 0:7e3b7c017977 89 mypwm3.pulsewidth_ms(80);
Morimoto448 0:7e3b7c017977 90 // 500ms待機する
Morimoto448 1:aabe6887447c 91 wait(0.5);
Morimoto448 0:7e3b7c017977 92 }
Morimoto448 0:7e3b7c017977 93
Morimoto448 0:7e3b7c017977 94 switch (mode)
Morimoto448 0:7e3b7c017977 95 {
Morimoto448 0:7e3b7c017977 96 // STATE_A : 前進(左:正転 右:正転)
Morimoto448 0:7e3b7c017977 97 case STATE_A:
Morimoto448 0:7e3b7c017977 98 myled = 1; // LED点灯
Morimoto448 0:7e3b7c017977 99 // 左モータの制御
Morimoto448 1:aabe6887447c 100 mypwm4.pulsewidth_ms(50);
Morimoto448 1:aabe6887447c 101 mypwm6.pulsewidth_ms(0);
Morimoto448 0:7e3b7c017977 102 // 右モータの制御
Morimoto448 1:aabe6887447c 103 mypwm2.pulsewidth_ms(53);
Morimoto448 0:7e3b7c017977 104 mypwm3.pulsewidth_ms(0);
Morimoto448 0:7e3b7c017977 105 break;
Morimoto448 0:7e3b7c017977 106
Morimoto448 0:7e3b7c017977 107 // STATE_B : 右旋回(左:正転 右:逆転)
Morimoto448 0:7e3b7c017977 108 case STATE_B:
Morimoto448 0:7e3b7c017977 109 myled = 1; // LED点灯
Morimoto448 0:7e3b7c017977 110 // 左モータの制御
Morimoto448 1:aabe6887447c 111 mypwm4.pulsewidth_ms(50);
Morimoto448 1:aabe6887447c 112 mypwm6.pulsewidth_ms(0);
Morimoto448 0:7e3b7c017977 113 // 右モータの制御
Morimoto448 0:7e3b7c017977 114 mypwm2.pulsewidth_ms(0);
Morimoto448 1:aabe6887447c 115 mypwm3.pulsewidth_ms(50);
Morimoto448 0:7e3b7c017977 116 break;
Morimoto448 0:7e3b7c017977 117
Morimoto448 0:7e3b7c017977 118 // STATE_C : 左旋回(左:逆転 右:正転)
Morimoto448 0:7e3b7c017977 119 case STATE_C:
Morimoto448 0:7e3b7c017977 120 myled = 1; // LED点灯
Morimoto448 0:7e3b7c017977 121 // 左モータの制御
Morimoto448 0:7e3b7c017977 122 mypwm4.pulsewidth_ms(0);
Morimoto448 1:aabe6887447c 123 mypwm6.pulsewidth_ms(50);
Morimoto448 0:7e3b7c017977 124 // 右モータの制御
Morimoto448 1:aabe6887447c 125 mypwm2.pulsewidth_ms(50);
Morimoto448 0:7e3b7c017977 126 mypwm3.pulsewidth_ms(0);
Morimoto448 0:7e3b7c017977 127 break;
Morimoto448 0:7e3b7c017977 128
Morimoto448 0:7e3b7c017977 129 // STATE_D : 後退(左:逆転 右:逆転)
Morimoto448 0:7e3b7c017977 130 case STATE_D:
Morimoto448 0:7e3b7c017977 131 myled = 1; // LED点灯
Morimoto448 0:7e3b7c017977 132 // 左モータの制御
Morimoto448 0:7e3b7c017977 133 mypwm4.pulsewidth_ms(0);
Morimoto448 1:aabe6887447c 134 mypwm6.pulsewidth_ms(50);
Morimoto448 0:7e3b7c017977 135 // 右モータの制御
Morimoto448 0:7e3b7c017977 136 mypwm2.pulsewidth_ms(0);
Morimoto448 1:aabe6887447c 137 mypwm3.pulsewidth_ms(50);
Morimoto448 0:7e3b7c017977 138 break;
Morimoto448 0:7e3b7c017977 139
Morimoto448 0:7e3b7c017977 140 // STATE_E : ブレーキ(左:ブレーキ 右:ブレーキ)
Morimoto448 0:7e3b7c017977 141 case STATE_E:
Morimoto448 0:7e3b7c017977 142 myled = 0; // LED消灯
Morimoto448 0:7e3b7c017977 143 // 左モータの制御
Morimoto448 0:7e3b7c017977 144 mypwm4.pulsewidth_ms(60);
Morimoto448 1:aabe6887447c 145 mypwm6.pulsewidth_ms(60);
Morimoto448 0:7e3b7c017977 146 // 右モータの制御
Morimoto448 0:7e3b7c017977 147 mypwm2.pulsewidth_ms(60);
Morimoto448 0:7e3b7c017977 148 mypwm3.pulsewidth_ms(60);
Morimoto448 0:7e3b7c017977 149 break;
Morimoto448 0:7e3b7c017977 150 }
Morimoto448 0:7e3b7c017977 151 }
Morimoto448 0:7e3b7c017977 152
Morimoto448 0:7e3b7c017977 153 }