手動機 ヌクレオ用のプログラムです

Dependencies:   mbed

Fork of F3RC_syudou_slave_3 by F3RC1班

Committer:
yuto17320508
Date:
Thu Aug 24 04:49:21 2017 +0000
Revision:
3:1a3ed86511d3
Parent:
2:96509fc6e151
a

Who changed what in which revision?

UserRevisionLine numberNew contents of line
yuto17320508 0:d0441e7d5ea3 1 #include "mbed.h"
yuto17320508 0:d0441e7d5ea3 2
yuto17320508 0:d0441e7d5ea3 3 Serial pc(USBTX,USBRX);
yuto17320508 3:1a3ed86511d3 4 SPISlave spi(PA_7,PA_6,PA_5,PA_4);
yuto17320508 2:96509fc6e151 5 PwmOut moter_1(PB_14);
yuto17320508 2:96509fc6e151 6 PwmOut moter_2(PB_15);
yuto17320508 0:d0441e7d5ea3 7 DigitalOut cyli_1(PB_5);
yuto17320508 0:d0441e7d5ea3 8 DigitalOut cyli_2(PB_3);
yuto17320508 0:d0441e7d5ea3 9 DigitalOut cyli_3(PA_10);
yuto17320508 2:96509fc6e151 10 //フォトインタラプタ上
yuto17320508 2:96509fc6e151 11 InterruptIn photo_1(PC_2);
yuto17320508 2:96509fc6e151 12 //フォトインタラプタ下
yuto17320508 2:96509fc6e151 13 InterruptIn photo_2(PC_3);
yuto17320508 0:d0441e7d5ea3 14
yuto17320508 2:96509fc6e151 15 int L_up,L_down,L_open,L_close,R_up,R_down,R_open,R_close;
yuto17320508 0:d0441e7d5ea3 16
yuto17320508 0:d0441e7d5ea3 17 //モーターの出力の設定
yuto17320508 0:d0441e7d5ea3 18 double moter_power = 0.7;
yuto17320508 0:d0441e7d5ea3 19
yuto17320508 2:96509fc6e151 20
yuto17320508 2:96509fc6e151 21 //腕上昇下降の関数
yuto17320508 2:96509fc6e151 22 void up()
yuto17320508 2:96509fc6e151 23 {
yuto17320508 2:96509fc6e151 24 moter_1=moter_power;
yuto17320508 2:96509fc6e151 25 moter_2=0;
yuto17320508 2:96509fc6e151 26 }
yuto17320508 2:96509fc6e151 27 void down()
yuto17320508 2:96509fc6e151 28 {
yuto17320508 2:96509fc6e151 29 moter_1=0;
yuto17320508 2:96509fc6e151 30 moter_2=moter_power;
yuto17320508 2:96509fc6e151 31 }
yuto17320508 2:96509fc6e151 32 void stop()
yuto17320508 2:96509fc6e151 33 {
yuto17320508 2:96509fc6e151 34 moter_1=0;
yuto17320508 2:96509fc6e151 35 moter_2=0;
yuto17320508 2:96509fc6e151 36 }
yuto17320508 2:96509fc6e151 37 //フラグ
yuto17320508 2:96509fc6e151 38 int up_flag=1;
yuto17320508 2:96509fc6e151 39 int down_flag=1;
yuto17320508 2:96509fc6e151 40 //フォトトランジスタのフラグ呼び出し
yuto17320508 2:96509fc6e151 41 void photo_1_rise()
yuto17320508 2:96509fc6e151 42 {
yuto17320508 2:96509fc6e151 43 stop();
yuto17320508 2:96509fc6e151 44 up_flag=0;
yuto17320508 2:96509fc6e151 45 }
yuto17320508 2:96509fc6e151 46 void photo_1_fall()
yuto17320508 2:96509fc6e151 47 {
yuto17320508 2:96509fc6e151 48 up_flag=1;
yuto17320508 2:96509fc6e151 49 }
yuto17320508 2:96509fc6e151 50 void photo_2_rise()
yuto17320508 2:96509fc6e151 51 {
yuto17320508 2:96509fc6e151 52 stop();
yuto17320508 2:96509fc6e151 53 down_flag=0;
yuto17320508 2:96509fc6e151 54 }
yuto17320508 2:96509fc6e151 55 void photo_2_fall()
yuto17320508 2:96509fc6e151 56 {
yuto17320508 2:96509fc6e151 57 down_flag=1;
yuto17320508 2:96509fc6e151 58 }
yuto17320508 0:d0441e7d5ea3 59 int main()
yuto17320508 0:d0441e7d5ea3 60 {
yuto17320508 1:dfabac3b39e9 61 spi.format(8,3);
yuto17320508 0:d0441e7d5ea3 62 spi.frequency(1000000);
yuto17320508 0:d0441e7d5ea3 63
yuto17320508 2:96509fc6e151 64 //フォトインタラプタ1
yuto17320508 2:96509fc6e151 65 photo_1.rise(&photo_1_rise);
yuto17320508 2:96509fc6e151 66 photo_1.fall(&photo_1_fall);
yuto17320508 2:96509fc6e151 67
yuto17320508 2:96509fc6e151 68 //フォトインタラプタ2
yuto17320508 2:96509fc6e151 69 photo_2.rise(&photo_2_rise);
yuto17320508 2:96509fc6e151 70 photo_2.fall(&photo_2_fall);
yuto17320508 0:d0441e7d5ea3 71 while(1) {
yuto17320508 0:d0441e7d5ea3 72
yuto17320508 0:d0441e7d5ea3 73 if(spi.receive()) {
yuto17320508 0:d0441e7d5ea3 74
yuto17320508 2:96509fc6e151 75 int L_up = spi.read() & 0b1;
yuto17320508 2:96509fc6e151 76 int L_down = (spi.read() & 0b10)>>1;
yuto17320508 2:96509fc6e151 77 int L_open = (spi.read() & 0b100)>>2;
yuto17320508 2:96509fc6e151 78 int L_close = (spi.read() & 0b1000)>>3;
yuto17320508 2:96509fc6e151 79 int R_up = (spi.read() & 0b10000)>>4;
yuto17320508 2:96509fc6e151 80 int R_down = (spi.read() & 0b100000)>>5;
yuto17320508 2:96509fc6e151 81 int R_open = (spi.read() & 0b1000000)>>6;
yuto17320508 2:96509fc6e151 82 int R_close = (spi.read() & 0b10000000)>>7;
yuto17320508 2:96509fc6e151 83
yuto17320508 2:96509fc6e151 84 //spiの値プリント
yuto17320508 2:96509fc6e151 85 pc.printf("L_up:%d\tL_down:%d\tL_open:%d\tL_close:%d\t\nR_up:%d\tR_down:%d\tR_open:%d\tR_close:%d\tsend:%d\r\n",L_up,L_down,L_open,L_close,R_up,R_down,R_open,R_close,spi.read());
yuto17320508 0:d0441e7d5ea3 86
yuto17320508 0:d0441e7d5ea3 87 }
yuto17320508 0:d0441e7d5ea3 88
yuto17320508 0:d0441e7d5ea3 89
yuto17320508 0:d0441e7d5ea3 90 //腕1 上昇下降
yuto17320508 2:96509fc6e151 91 if(L_up == 1 && up_flag==1) {//上昇
yuto17320508 2:96509fc6e151 92 up();
yuto17320508 2:96509fc6e151 93 } else if(L_down == 1 && down_flag==1) {//下降
yuto17320508 2:96509fc6e151 94 down();
yuto17320508 0:d0441e7d5ea3 95 } else {
yuto17320508 2:96509fc6e151 96 stop();
yuto17320508 0:d0441e7d5ea3 97 }
yuto17320508 0:d0441e7d5ea3 98
yuto17320508 0:d0441e7d5ea3 99 //腕1 開閉
yuto17320508 2:96509fc6e151 100 if(L_open == 1) {
yuto17320508 0:d0441e7d5ea3 101 cyli_1=1;
yuto17320508 2:96509fc6e151 102 } else if(L_close == 1) {
yuto17320508 0:d0441e7d5ea3 103 cyli_1=0;
yuto17320508 0:d0441e7d5ea3 104 }
yuto17320508 0:d0441e7d5ea3 105
yuto17320508 0:d0441e7d5ea3 106 //腕2 上昇下降
yuto17320508 2:96509fc6e151 107 if(R_up == 1) {
yuto17320508 0:d0441e7d5ea3 108 cyli_2=1;
yuto17320508 2:96509fc6e151 109 } else if(R_down == 1) {
yuto17320508 0:d0441e7d5ea3 110 cyli_2=0;
yuto17320508 0:d0441e7d5ea3 111 }
yuto17320508 0:d0441e7d5ea3 112
yuto17320508 0:d0441e7d5ea3 113 //腕2 開閉
yuto17320508 2:96509fc6e151 114 if(R_open == 1) {
yuto17320508 0:d0441e7d5ea3 115 cyli_3=1;
yuto17320508 2:96509fc6e151 116 } else if(R_close == 1) {
yuto17320508 0:d0441e7d5ea3 117 cyli_3=0;
yuto17320508 1:dfabac3b39e9 118 }
yuto17320508 0:d0441e7d5ea3 119 }
yuto17320508 0:d0441e7d5ea3 120
yuto17320508 0:d0441e7d5ea3 121 }