test

Dependencies:   mbed

Committer:
Shimada_Takumi
Date:
Wed Jul 10 05:27:05 2019 +0000
Revision:
0:6d1c8d3e84cd
test

Who changed what in which revision?

UserRevisionLine numberNew contents of line
Shimada_Takumi 0:6d1c8d3e84cd 1 #include "mbed.h"
Shimada_Takumi 0:6d1c8d3e84cd 2
Shimada_Takumi 0:6d1c8d3e84cd 3 #define LF 0
Shimada_Takumi 0:6d1c8d3e84cd 4 #define LB 1
Shimada_Takumi 0:6d1c8d3e84cd 5 #define RF 2
Shimada_Takumi 0:6d1c8d3e84cd 6 #define RB 3
Shimada_Takumi 0:6d1c8d3e84cd 7
Shimada_Takumi 0:6d1c8d3e84cd 8 #define S 0
Shimada_Takumi 0:6d1c8d3e84cd 9 #define P 1
Shimada_Takumi 0:6d1c8d3e84cd 10 #define M 2
Shimada_Takumi 0:6d1c8d3e84cd 11
Shimada_Takumi 0:6d1c8d3e84cd 12 #define F 0
Shimada_Takumi 0:6d1c8d3e84cd 13 #define B 1
Shimada_Takumi 0:6d1c8d3e84cd 14 #define L 2
Shimada_Takumi 0:6d1c8d3e84cd 15 #define R 3
Shimada_Takumi 0:6d1c8d3e84cd 16 #define W 4
Shimada_Takumi 0:6d1c8d3e84cd 17
Shimada_Takumi 0:6d1c8d3e84cd 18 #define PRINT
Shimada_Takumi 0:6d1c8d3e84cd 19
Shimada_Takumi 0:6d1c8d3e84cd 20 Serial pc(SERIAL_TX, SERIAL_RX);
Shimada_Takumi 0:6d1c8d3e84cd 21
Shimada_Takumi 0:6d1c8d3e84cd 22 PwmOut dir[4][2] = {{PC_9,PB_8},
Shimada_Takumi 0:6d1c8d3e84cd 23 {PC_8,PC_6},
Shimada_Takumi 0:6d1c8d3e84cd 24 {PA_5,PA_6},
Shimada_Takumi 0:6d1c8d3e84cd 25 {PB_6,PB_7}
Shimada_Takumi 0:6d1c8d3e84cd 26 };
Shimada_Takumi 0:6d1c8d3e84cd 27
Shimada_Takumi 0:6d1c8d3e84cd 28 DigitalIn step_sensor[2]={D5,D4};
Shimada_Takumi 0:6d1c8d3e84cd 29
Shimada_Takumi 0:6d1c8d3e84cd 30 Ticker flip;
Shimada_Takumi 0:6d1c8d3e84cd 31
Shimada_Takumi 0:6d1c8d3e84cd 32 int flag=0;
Shimada_Takumi 0:6d1c8d3e84cd 33
Shimada_Takumi 0:6d1c8d3e84cd 34 float duty=0.5;
Shimada_Takumi 0:6d1c8d3e84cd 35
Shimada_Takumi 0:6d1c8d3e84cd 36 char a;
Shimada_Takumi 0:6d1c8d3e84cd 37
Shimada_Takumi 0:6d1c8d3e84cd 38 /*モータ制御*/
Shimada_Takumi 0:6d1c8d3e84cd 39 void motor(int number,int move,float duty){
Shimada_Takumi 0:6d1c8d3e84cd 40 switch(move){
Shimada_Takumi 0:6d1c8d3e84cd 41 case S:
Shimada_Takumi 0:6d1c8d3e84cd 42 dir[number][0]=0;
Shimada_Takumi 0:6d1c8d3e84cd 43 dir[number][1]=0;
Shimada_Takumi 0:6d1c8d3e84cd 44 break;
Shimada_Takumi 0:6d1c8d3e84cd 45
Shimada_Takumi 0:6d1c8d3e84cd 46 case P:
Shimada_Takumi 0:6d1c8d3e84cd 47 dir[number][0]=0;
Shimada_Takumi 0:6d1c8d3e84cd 48 dir[number][1]=duty;
Shimada_Takumi 0:6d1c8d3e84cd 49 break;
Shimada_Takumi 0:6d1c8d3e84cd 50
Shimada_Takumi 0:6d1c8d3e84cd 51 case M:
Shimada_Takumi 0:6d1c8d3e84cd 52 dir[number][0]=duty;
Shimada_Takumi 0:6d1c8d3e84cd 53 dir[number][1]=0;
Shimada_Takumi 0:6d1c8d3e84cd 54 break;
Shimada_Takumi 0:6d1c8d3e84cd 55 }
Shimada_Takumi 0:6d1c8d3e84cd 56 }
Shimada_Takumi 0:6d1c8d3e84cd 57
Shimada_Takumi 0:6d1c8d3e84cd 58 void move(int v){
Shimada_Takumi 0:6d1c8d3e84cd 59 switch(v){
Shimada_Takumi 0:6d1c8d3e84cd 60 case F:
Shimada_Takumi 0:6d1c8d3e84cd 61 motor(LF,F,duty);
Shimada_Takumi 0:6d1c8d3e84cd 62 motor(LB,F,duty);
Shimada_Takumi 0:6d1c8d3e84cd 63 motor(RF,B,duty);
Shimada_Takumi 0:6d1c8d3e84cd 64 motor(RB,B,duty);
Shimada_Takumi 0:6d1c8d3e84cd 65 break;
Shimada_Takumi 0:6d1c8d3e84cd 66
Shimada_Takumi 0:6d1c8d3e84cd 67 case B:
Shimada_Takumi 0:6d1c8d3e84cd 68 motor(LF,B,duty);
Shimada_Takumi 0:6d1c8d3e84cd 69 motor(LB,B,duty);
Shimada_Takumi 0:6d1c8d3e84cd 70 motor(RF,F,duty);
Shimada_Takumi 0:6d1c8d3e84cd 71 motor(RB,F,duty);
Shimada_Takumi 0:6d1c8d3e84cd 72 break;
Shimada_Takumi 0:6d1c8d3e84cd 73
Shimada_Takumi 0:6d1c8d3e84cd 74 case L:
Shimada_Takumi 0:6d1c8d3e84cd 75 motor(LF,B,duty);
Shimada_Takumi 0:6d1c8d3e84cd 76 motor(LB,F,duty);
Shimada_Takumi 0:6d1c8d3e84cd 77 motor(RF,F,duty);
Shimada_Takumi 0:6d1c8d3e84cd 78 motor(RB,B,duty);
Shimada_Takumi 0:6d1c8d3e84cd 79 break;
Shimada_Takumi 0:6d1c8d3e84cd 80
Shimada_Takumi 0:6d1c8d3e84cd 81 case R:
Shimada_Takumi 0:6d1c8d3e84cd 82 motor(LF,F,duty);
Shimada_Takumi 0:6d1c8d3e84cd 83 motor(LB,B,duty);
Shimada_Takumi 0:6d1c8d3e84cd 84 motor(RF,B,duty);
Shimada_Takumi 0:6d1c8d3e84cd 85 motor(RB,F,duty);
Shimada_Takumi 0:6d1c8d3e84cd 86 break;
Shimada_Takumi 0:6d1c8d3e84cd 87
Shimada_Takumi 0:6d1c8d3e84cd 88 case W:
Shimada_Takumi 0:6d1c8d3e84cd 89 motor(LF,S,duty);
Shimada_Takumi 0:6d1c8d3e84cd 90 motor(LB,S,duty);
Shimada_Takumi 0:6d1c8d3e84cd 91 motor(RF,S,duty);
Shimada_Takumi 0:6d1c8d3e84cd 92 motor(RB,S,duty);
Shimada_Takumi 0:6d1c8d3e84cd 93 break;
Shimada_Takumi 0:6d1c8d3e84cd 94 }
Shimada_Takumi 0:6d1c8d3e84cd 95 }
Shimada_Takumi 0:6d1c8d3e84cd 96
Shimada_Takumi 0:6d1c8d3e84cd 97 //階段センサ
Shimada_Takumi 0:6d1c8d3e84cd 98 int sensor_move(){
Shimada_Takumi 0:6d1c8d3e84cd 99 if(step_sensor[0]==1 || step_sensor[1]==1){
Shimada_Takumi 0:6d1c8d3e84cd 100 return 1;
Shimada_Takumi 0:6d1c8d3e84cd 101 }else{
Shimada_Takumi 0:6d1c8d3e84cd 102 return 0;
Shimada_Takumi 0:6d1c8d3e84cd 103 }
Shimada_Takumi 0:6d1c8d3e84cd 104 }
Shimada_Takumi 0:6d1c8d3e84cd 105
Shimada_Takumi 0:6d1c8d3e84cd 106 /*緊急停止割り込み*/
Shimada_Takumi 0:6d1c8d3e84cd 107 void flipper(){
Shimada_Takumi 0:6d1c8d3e84cd 108 }
Shimada_Takumi 0:6d1c8d3e84cd 109
Shimada_Takumi 0:6d1c8d3e84cd 110 /*コントロール*/
Shimada_Takumi 0:6d1c8d3e84cd 111 void control(){
Shimada_Takumi 0:6d1c8d3e84cd 112 pc.printf("what?");
Shimada_Takumi 0:6d1c8d3e84cd 113
Shimada_Takumi 0:6d1c8d3e84cd 114 a = pc.getc();
Shimada_Takumi 0:6d1c8d3e84cd 115
Shimada_Takumi 0:6d1c8d3e84cd 116 switch(a){
Shimada_Takumi 0:6d1c8d3e84cd 117 case 'w':
Shimada_Takumi 0:6d1c8d3e84cd 118 move(F);
Shimada_Takumi 0:6d1c8d3e84cd 119 #ifdef PRINT
Shimada_Takumi 0:6d1c8d3e84cd 120 pc.printf("move_F\n\r");
Shimada_Takumi 0:6d1c8d3e84cd 121 #endif
Shimada_Takumi 0:6d1c8d3e84cd 122 wait(0.1);
Shimada_Takumi 0:6d1c8d3e84cd 123 break;
Shimada_Takumi 0:6d1c8d3e84cd 124
Shimada_Takumi 0:6d1c8d3e84cd 125 case 'a':
Shimada_Takumi 0:6d1c8d3e84cd 126 move(L);
Shimada_Takumi 0:6d1c8d3e84cd 127 #ifdef PRINT
Shimada_Takumi 0:6d1c8d3e84cd 128 pc.printf("move_L\n\r");
Shimada_Takumi 0:6d1c8d3e84cd 129 #endif
Shimada_Takumi 0:6d1c8d3e84cd 130 wait(0.1);
Shimada_Takumi 0:6d1c8d3e84cd 131 break;
Shimada_Takumi 0:6d1c8d3e84cd 132
Shimada_Takumi 0:6d1c8d3e84cd 133 case 's':
Shimada_Takumi 0:6d1c8d3e84cd 134 move(R);
Shimada_Takumi 0:6d1c8d3e84cd 135 #ifdef PRINT
Shimada_Takumi 0:6d1c8d3e84cd 136 pc.printf("move_B\n\r");
Shimada_Takumi 0:6d1c8d3e84cd 137 #endif
Shimada_Takumi 0:6d1c8d3e84cd 138 wait(0.1);
Shimada_Takumi 0:6d1c8d3e84cd 139 break;
Shimada_Takumi 0:6d1c8d3e84cd 140
Shimada_Takumi 0:6d1c8d3e84cd 141 case 'd':
Shimada_Takumi 0:6d1c8d3e84cd 142 move(B);
Shimada_Takumi 0:6d1c8d3e84cd 143 #ifdef PRINT
Shimada_Takumi 0:6d1c8d3e84cd 144 pc.printf("move_R\n\r");
Shimada_Takumi 0:6d1c8d3e84cd 145 #endif
Shimada_Takumi 0:6d1c8d3e84cd 146 wait(0.1);
Shimada_Takumi 0:6d1c8d3e84cd 147 break;
Shimada_Takumi 0:6d1c8d3e84cd 148
Shimada_Takumi 0:6d1c8d3e84cd 149 case 'q':
Shimada_Takumi 0:6d1c8d3e84cd 150 #ifdef PRINT
Shimada_Takumi 0:6d1c8d3e84cd 151 pc.printf("END\n\r");
Shimada_Takumi 0:6d1c8d3e84cd 152 #endif
Shimada_Takumi 0:6d1c8d3e84cd 153 flag=0;
Shimada_Takumi 0:6d1c8d3e84cd 154 break;
Shimada_Takumi 0:6d1c8d3e84cd 155
Shimada_Takumi 0:6d1c8d3e84cd 156 default:
Shimada_Takumi 0:6d1c8d3e84cd 157 move(W);
Shimada_Takumi 0:6d1c8d3e84cd 158 #ifdef printf
Shimada_Takumi 0:6d1c8d3e84cd 159 pc.printf("move_W\n\r");
Shimada_Takumi 0:6d1c8d3e84cd 160 #endif
Shimada_Takumi 0:6d1c8d3e84cd 161 break;
Shimada_Takumi 0:6d1c8d3e84cd 162 }
Shimada_Takumi 0:6d1c8d3e84cd 163 move(W);
Shimada_Takumi 0:6d1c8d3e84cd 164 }
Shimada_Takumi 0:6d1c8d3e84cd 165
Shimada_Takumi 0:6d1c8d3e84cd 166 /*初期化*/
Shimada_Takumi 0:6d1c8d3e84cd 167 void init(){
Shimada_Takumi 0:6d1c8d3e84cd 168 pc.printf("hello\n\r");
Shimada_Takumi 0:6d1c8d3e84cd 169
Shimada_Takumi 0:6d1c8d3e84cd 170 move(W);
Shimada_Takumi 0:6d1c8d3e84cd 171 wait(3);
Shimada_Takumi 0:6d1c8d3e84cd 172
Shimada_Takumi 0:6d1c8d3e84cd 173 flag = 1;
Shimada_Takumi 0:6d1c8d3e84cd 174 }
Shimada_Takumi 0:6d1c8d3e84cd 175
Shimada_Takumi 0:6d1c8d3e84cd 176 /*メイン*/
Shimada_Takumi 0:6d1c8d3e84cd 177 int main(void){
Shimada_Takumi 0:6d1c8d3e84cd 178 init();
Shimada_Takumi 0:6d1c8d3e84cd 179
Shimada_Takumi 0:6d1c8d3e84cd 180 while(flag == 1){
Shimada_Takumi 0:6d1c8d3e84cd 181 control();
Shimada_Takumi 0:6d1c8d3e84cd 182 }
Shimada_Takumi 0:6d1c8d3e84cd 183 }