mech / Mbed OS Shiken_3
Committer:
isshikiyuya
Date:
Mon Dec 17 12:02:19 2018 +0000
Revision:
5:237c49800e14
Parent:
4:45b45e8ca173
Child:
6:9bd1da494e0b
a

Who changed what in which revision?

UserRevisionLine numberNew contents of line
isshikiyuya 4:45b45e8ca173 1 #include "function.h"
isshikiyuya 4:45b45e8ca173 2
isshikiyuya 4:45b45e8ca173 3 PwmOut pwm11(D5); //左正転
isshikiyuya 4:45b45e8ca173 4 PwmOut pwm12(D1); //左逆転
isshikiyuya 4:45b45e8ca173 5 PwmOut pwm21(D6); //右正転
isshikiyuya 4:45b45e8ca173 6 PwmOut pwm22(D9); //右逆転
isshikiyuya 4:45b45e8ca173 7 DigitalOut LED(D2);
isshikiyuya 4:45b45e8ca173 8 InterruptIn button1(A2); //フォトインタラプタ左
isshikiyuya 4:45b45e8ca173 9 InterruptIn button2(A1); //フォトインタラプタ右
isshikiyuya 4:45b45e8ca173 10 AnalogIn in1(A6); //フォトリフレクタ左
isshikiyuya 4:45b45e8ca173 11 AnalogIn in2(A4); //フォトリフレクタ真ん中
isshikiyuya 4:45b45e8ca173 12 AnalogIn in3(A3); //フォトリフレクタ右
isshikiyuya 4:45b45e8ca173 13
isshikiyuya 4:45b45e8ca173 14 float g = 0.0f;//目標値(パルス数)
isshikiyuya 4:45b45e8ca173 15 float gain1 = 0.0f;//左ゲイン
isshikiyuya 4:45b45e8ca173 16 float gain2 = 0.0f;//右ゲイン
isshikiyuya 4:45b45e8ca173 17 float dif1 = 0.0f;//目標との差左
isshikiyuya 4:45b45e8ca173 18 float dif2 = 0.0f;//目標との差右
isshikiyuya 5:237c49800e14 19 float thr1 = 0.40f;//フォトリフレクタのしきい値左
isshikiyuya 5:237c49800e14 20 float thr2 = 0.40f;//フォトリフレクタのしきい値真ん中
isshikiyuya 5:237c49800e14 21 float thr3 = 0.40f;//フォトリフレクタのしきい値右
isshikiyuya 4:45b45e8ca173 22 int judge1 = 0;
isshikiyuya 4:45b45e8ca173 23 int judge2 = 0;
isshikiyuya 4:45b45e8ca173 24 int judge3 = 0;
isshikiyuya 4:45b45e8ca173 25 int mode1 = 10;
isshikiyuya 4:45b45e8ca173 26 int mode2 = 2;
isshikiyuya 4:45b45e8ca173 27 int mode3 = 2;
isshikiyuya 4:45b45e8ca173 28
isshikiyuya 4:45b45e8ca173 29 int main() {
isshikiyuya 4:45b45e8ca173 30 pwm11.period(0.01f); //100Hz
isshikiyuya 4:45b45e8ca173 31 pwm12.period(0.01f); //100Hz
isshikiyuya 4:45b45e8ca173 32 pwm21.period(0.01f); //100Hz
isshikiyuya 4:45b45e8ca173 33 pwm22.period(0.01f); //100Hz
isshikiyuya 4:45b45e8ca173 34 button1.rise(&flip1);
isshikiyuya 4:45b45e8ca173 35 button2.rise(&flip2);
isshikiyuya 4:45b45e8ca173 36 int n;//3biteで状態を表現
isshikiyuya 4:45b45e8ca173 37
isshikiyuya 4:45b45e8ca173 38 flipper.attach(&flip3,0.05f);//0.05秒ごとに回転数cを更新
isshikiyuya 4:45b45e8ca173 39
isshikiyuya 4:45b45e8ca173 40
isshikiyuya 4:45b45e8ca173 41 while(1) {
isshikiyuya 4:45b45e8ca173 42 distance = (distance1+distance2)/2;
isshikiyuya 4:45b45e8ca173 43
isshikiyuya 4:45b45e8ca173 44 if(distance < 43*2*12*mode1){
isshikiyuya 5:237c49800e14 45 /*
isshikiyuya 4:45b45e8ca173 46 if( led_count1 + led_count2 > 43*2*12){ //200mm毎にLEDを点滅
isshikiyuya 4:45b45e8ca173 47 LED = 1.0f;
isshikiyuya 4:45b45e8ca173 48 wait(0.5f);
isshikiyuya 4:45b45e8ca173 49 LED = 0;
isshikiyuya 4:45b45e8ca173 50 led_count1 = 0;
isshikiyuya 4:45b45e8ca173 51 led_count2 = 0;
isshikiyuya 4:45b45e8ca173 52 }
isshikiyuya 5:237c49800e14 53 */
isshikiyuya 4:45b45e8ca173 54 //////////////////floatのinをintのjudgeに変換
isshikiyuya 4:45b45e8ca173 55 if(in1 > thr1){
isshikiyuya 4:45b45e8ca173 56 judge1 = 1;
isshikiyuya 4:45b45e8ca173 57 }
isshikiyuya 4:45b45e8ca173 58 else{
isshikiyuya 4:45b45e8ca173 59 judge1 = 0;
isshikiyuya 4:45b45e8ca173 60 }
isshikiyuya 4:45b45e8ca173 61 if(in2 > thr2){
isshikiyuya 4:45b45e8ca173 62 judge2 = 1;
isshikiyuya 4:45b45e8ca173 63 }
isshikiyuya 4:45b45e8ca173 64 else{
isshikiyuya 4:45b45e8ca173 65 judge2 = 0;
isshikiyuya 4:45b45e8ca173 66 }
isshikiyuya 4:45b45e8ca173 67 if(in3 > thr3){
isshikiyuya 4:45b45e8ca173 68 judge3 = 1;
isshikiyuya 4:45b45e8ca173 69 }
isshikiyuya 4:45b45e8ca173 70 else{
isshikiyuya 4:45b45e8ca173 71 judge3 = 0;
isshikiyuya 4:45b45e8ca173 72 }
isshikiyuya 4:45b45e8ca173 73 /////////////////
isshikiyuya 4:45b45e8ca173 74 n = judge1 + judge2*2 + judge3*4;
isshikiyuya 4:45b45e8ca173 75
isshikiyuya 4:45b45e8ca173 76 switch(n){
isshikiyuya 4:45b45e8ca173 77 case(2)://白黒白進む
isshikiyuya 4:45b45e8ca173 78 g = 500.0f;
isshikiyuya 5:237c49800e14 79 gain1 = g - c1;
isshikiyuya 5:237c49800e14 80 gain2 = g - c2;
isshikiyuya 4:45b45e8ca173 81
isshikiyuya 5:237c49800e14 82 pwm11.write(0.40f);
isshikiyuya 4:45b45e8ca173 83 pwm12.write(0.30f);
isshikiyuya 5:237c49800e14 84 pwm21.write(0.40f);
isshikiyuya 4:45b45e8ca173 85 pwm22.write(0.30f);
isshikiyuya 4:45b45e8ca173 86 break;
isshikiyuya 4:45b45e8ca173 87
isshikiyuya 4:45b45e8ca173 88 case(0)://白白白
isshikiyuya 4:45b45e8ca173 89 g = 600.0f;
isshikiyuya 5:237c49800e14 90 gain1 = g - c1;
isshikiyuya 5:237c49800e14 91 gain2 = g - c2;
isshikiyuya 4:45b45e8ca173 92
isshikiyuya 5:237c49800e14 93 pwm11.write(0.30f);
isshikiyuya 5:237c49800e14 94 pwm12.write(0.40f);
isshikiyuya 5:237c49800e14 95 pwm21.write(0.30f);
isshikiyuya 5:237c49800e14 96 pwm22.write(0.40f);
isshikiyuya 4:45b45e8ca173 97 break;
isshikiyuya 4:45b45e8ca173 98
isshikiyuya 4:45b45e8ca173 99
isshikiyuya 4:45b45e8ca173 100
isshikiyuya 4:45b45e8ca173 101 case(1)://黒白白左回転強
isshikiyuya 5:237c49800e14 102 pwm11.write(0.30f);
isshikiyuya 4:45b45e8ca173 103 pwm12.write(0.30f);
isshikiyuya 5:237c49800e14 104 pwm21.write(0.30f);
isshikiyuya 5:237c49800e14 105 pwm22.write(0.20f);
isshikiyuya 4:45b45e8ca173 106 break;
isshikiyuya 4:45b45e8ca173 107
isshikiyuya 4:45b45e8ca173 108 case(4)://白白黒右回転
isshikiyuya 5:237c49800e14 109 pwm11.write(0.30f);
isshikiyuya 5:237c49800e14 110 pwm12.write(0.20f);
isshikiyuya 5:237c49800e14 111 pwm21.write(0.30f);
isshikiyuya 4:45b45e8ca173 112 pwm22.write(0.30f);
isshikiyuya 4:45b45e8ca173 113 break;
isshikiyuya 4:45b45e8ca173 114
isshikiyuya 4:45b45e8ca173 115 case(3)://黒黒白左回転弱
isshikiyuya 5:237c49800e14 116 pwm11.write(0.30f);
isshikiyuya 4:45b45e8ca173 117 pwm12.write(0.30f);
isshikiyuya 5:237c49800e14 118 pwm21.write(0.30f);
isshikiyuya 5:237c49800e14 119 pwm22.write(0.20f);
isshikiyuya 4:45b45e8ca173 120 break;
isshikiyuya 4:45b45e8ca173 121
isshikiyuya 4:45b45e8ca173 122 case(6)://白黒黒右回転弱
isshikiyuya 5:237c49800e14 123 pwm11.write(0.30f);
isshikiyuya 5:237c49800e14 124 pwm12.write(0.20f);
isshikiyuya 5:237c49800e14 125 pwm21.write(0.30f);
isshikiyuya 4:45b45e8ca173 126 pwm22.write(0.30f);
isshikiyuya 4:45b45e8ca173 127 break;
isshikiyuya 4:45b45e8ca173 128
isshikiyuya 4:45b45e8ca173 129 case(7)://黒黒黒爆走
isshikiyuya 5:237c49800e14 130 pwm11.write(0.40f);
isshikiyuya 4:45b45e8ca173 131 pwm12.write(0.30f);
isshikiyuya 5:237c49800e14 132 pwm21.write(0.40f);
isshikiyuya 4:45b45e8ca173 133 pwm22.write(0.30f);
isshikiyuya 4:45b45e8ca173 134 break;
isshikiyuya 4:45b45e8ca173 135
isshikiyuya 4:45b45e8ca173 136 default://それ以外(ないけど)
isshikiyuya 4:45b45e8ca173 137 pwm11.write(0.00f);
isshikiyuya 4:45b45e8ca173 138 pwm12.write(0.00f);
isshikiyuya 4:45b45e8ca173 139 pwm21.write(0.00f);
isshikiyuya 4:45b45e8ca173 140 pwm22.write(0.00f);
isshikiyuya 4:45b45e8ca173 141 break;
isshikiyuya 4:45b45e8ca173 142 }
isshikiyuya 4:45b45e8ca173 143 }
isshikiyuya 4:45b45e8ca173 144 else{
isshikiyuya 4:45b45e8ca173 145
isshikiyuya 4:45b45e8ca173 146 pwm11.write(0.00f);
isshikiyuya 4:45b45e8ca173 147 pwm12.write(0.00f);
isshikiyuya 4:45b45e8ca173 148 pwm21.write(0.00f);
isshikiyuya 4:45b45e8ca173 149 pwm22.write(0.00f);
isshikiyuya 4:45b45e8ca173 150 }
isshikiyuya 4:45b45e8ca173 151 }
mbed_official 0:50d2b9c62765 152 }