受け取りです.質問・アドバイスがあればどうぞ.
Dependencies: MultiSerial mbed
main.h@0:f38700ce12ea, 2014-06-28 (annotated)
- Committer:
- lilac0112_1
- Date:
- Sat Jun 28 04:31:44 2014 +0000
- Revision:
- 0:f38700ce12ea
- Child:
- 1:b43b0cbff722
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
lilac0112_1 | 0:f38700ce12ea | 1 | #include "mbed.h" |
lilac0112_1 | 0:f38700ce12ea | 2 | //NC = 調整中なので無機能にしておく |
lilac0112_1 | 0:f38700ce12ea | 3 | bool GetBit(uint8_t n, uint8_t bit){//n=8[bit]data, bit = 1-8(8bitのデータから指定bitの数値を読み取る) |
lilac0112_1 | 0:f38700ce12ea | 4 | return (n>>(bit-1))%2; |
lilac0112_1 | 0:f38700ce12ea | 5 | } |
lilac0112_1 | 0:f38700ce12ea | 6 | DigitalOut LED[4]={LED1, LED2, LED3, LED4};//確認用 |
lilac0112_1 | 0:f38700ce12ea | 7 | enum Logic{OFF=0, ON};//殆どはモーターのON,OFFに使う |
lilac0112_1 | 0:f38700ce12ea | 8 | enum Revolution{CW=0, CCW};//正回転,逆回転(オムニ,上体回転) |
lilac0112_1 | 0:f38700ce12ea | 9 | //オムニホイール関連 |
lilac0112_1 | 0:f38700ce12ea | 10 | enum Omni{Front=0, Back, Right, Left};//前後左右のオムニホイール |
lilac0112_1 | 0:f38700ce12ea | 11 | DigitalOut Omni[4][2]={{NC, NC}, {NC, NC}, {NC, NC}, {NC, NC}};//{p5, p6}, {p7, p8}, {p9, p10}, {p11, p12} |
lilac0112_1 | 0:f38700ce12ea | 12 | PwmOut OmniSpeed[4]={NC, NC, NC, NC};//p21, p22, p23, p24 |
lilac0112_1 | 0:f38700ce12ea | 13 | #define OmniRev 0.5 //PWM |
lilac0112_1 | 0:f38700ce12ea | 14 | //ex)Omni[Front][CCW]=OFF; |
lilac0112_1 | 0:f38700ce12ea | 15 | void pause(void){//停止 |
lilac0112_1 | 0:f38700ce12ea | 16 | Omni[Front][CW] = Omni[Front][CCW] = OFF; |
lilac0112_1 | 0:f38700ce12ea | 17 | Omni[Back][CW] = Omni[Back][CCW] = OFF; |
lilac0112_1 | 0:f38700ce12ea | 18 | Omni[Right][CW] = Omni[Right][CCW] = OFF; |
lilac0112_1 | 0:f38700ce12ea | 19 | Omni[Left][CW] = Omni[Left][CCW] = OFF; |
lilac0112_1 | 0:f38700ce12ea | 20 | } |
lilac0112_1 | 0:f38700ce12ea | 21 | void go(void){//前進 |
lilac0112_1 | 0:f38700ce12ea | 22 | Omni[Front][CW] = Omni[Front][CCW] = OFF; |
lilac0112_1 | 0:f38700ce12ea | 23 | Omni[Back][CW] = Omni[Back][CCW] = OFF; |
lilac0112_1 | 0:f38700ce12ea | 24 | Omni[Right][CW] = OFF; Omni[Right][CCW] = ON; |
lilac0112_1 | 0:f38700ce12ea | 25 | Omni[Left][CW] = ON; Omni[Left][CCW] = OFF; |
lilac0112_1 | 0:f38700ce12ea | 26 | } |
lilac0112_1 | 0:f38700ce12ea | 27 | void back(void){//後進 |
lilac0112_1 | 0:f38700ce12ea | 28 | Omni[Front][CW] = Omni[Front][CCW] = OFF; |
lilac0112_1 | 0:f38700ce12ea | 29 | Omni[Back][CW] = Omni[Back][CCW] = OFF; |
lilac0112_1 | 0:f38700ce12ea | 30 | Omni[Right][CW] = ON; Omni[Right][CCW] = OFF; |
lilac0112_1 | 0:f38700ce12ea | 31 | Omni[Left][CW] = OFF; Omni[Left][CCW] = ON; |
lilac0112_1 | 0:f38700ce12ea | 32 | } |
lilac0112_1 | 0:f38700ce12ea | 33 | void sidleleft(void){//左移動 |
lilac0112_1 | 0:f38700ce12ea | 34 | Omni[Front][CW] = OFF; Omni[Front][CCW] = ON; |
lilac0112_1 | 0:f38700ce12ea | 35 | Omni[Back][CW] = ON; Omni[Back][CCW] = OFF; |
lilac0112_1 | 0:f38700ce12ea | 36 | Omni[Right][CW] = Omni[Right][CCW] = OFF; |
lilac0112_1 | 0:f38700ce12ea | 37 | Omni[Left][CW] = Omni[Left][CCW] = OFF; |
lilac0112_1 | 0:f38700ce12ea | 38 | } |
lilac0112_1 | 0:f38700ce12ea | 39 | void sidleright(void){//右移動 |
lilac0112_1 | 0:f38700ce12ea | 40 | Omni[Front][CW] = ON; Omni[Front][CCW] = OFF; |
lilac0112_1 | 0:f38700ce12ea | 41 | Omni[Back][CW] = OFF; Omni[Back][CCW] = ON; |
lilac0112_1 | 0:f38700ce12ea | 42 | Omni[Right][CW] = Omni[Right][CCW] = OFF; |
lilac0112_1 | 0:f38700ce12ea | 43 | Omni[Left][CW] = Omni[Left][CCW] = OFF; |
lilac0112_1 | 0:f38700ce12ea | 44 | } |
lilac0112_1 | 0:f38700ce12ea | 45 | void turnleft(void){//左旋回 |
lilac0112_1 | 0:f38700ce12ea | 46 | Omni[Front][CW] = OFF; Omni[Front][CCW] = ON; |
lilac0112_1 | 0:f38700ce12ea | 47 | Omni[Back][CW] = OFF; Omni[Back][CCW] = ON; |
lilac0112_1 | 0:f38700ce12ea | 48 | Omni[Right][CW] = OFF; Omni[Right][CCW] = ON; |
lilac0112_1 | 0:f38700ce12ea | 49 | Omni[Left][CW] = OFF; Omni[Left][CCW] = ON; |
lilac0112_1 | 0:f38700ce12ea | 50 | } |
lilac0112_1 | 0:f38700ce12ea | 51 | void turnright(void){//右旋回 |
lilac0112_1 | 0:f38700ce12ea | 52 | Omni[Front][CW] = ON; Omni[Front][CCW] = OFF; |
lilac0112_1 | 0:f38700ce12ea | 53 | Omni[Back][CW] = ON; Omni[Back][CCW] = OFF; |
lilac0112_1 | 0:f38700ce12ea | 54 | Omni[Right][CW] = ON; Omni[Right][CCW] = OFF; |
lilac0112_1 | 0:f38700ce12ea | 55 | Omni[Left][CW] = ON; Omni[Left][CCW] = OFF; |
lilac0112_1 | 0:f38700ce12ea | 56 | } |
lilac0112_1 | 0:f38700ce12ea | 57 | //上体回転関連 |
lilac0112_1 | 0:f38700ce12ea | 58 | DigitalOut Turn[2]={NC, NC};//p29, p30 |
lilac0112_1 | 0:f38700ce12ea | 59 | DigitalIn TurnSW(NC);//p26 |
lilac0112_1 | 0:f38700ce12ea | 60 | PwmOut TurnSpeed(NC);//p25 |
lilac0112_1 | 0:f38700ce12ea | 61 | #define TurnRev 0.5//PWM |
lilac0112_1 | 0:f38700ce12ea | 62 | //ex)Turn[CW]=ON; |
lilac0112_1 | 0:f38700ce12ea | 63 | //パワーウィンドウ(腕)関連 |
lilac0112_1 | 0:f38700ce12ea | 64 | #define TimeLimit 1.0//腕が閉まっても動けるのはTimeLimit[s]まで |
lilac0112_1 | 0:f38700ce12ea | 65 | enum Scissors{R=0, L, C};//右腕と左腕 |
lilac0112_1 | 0:f38700ce12ea | 66 | enum Clippers{Open=0, Close};//開ける動作と閉める動作 |
lilac0112_1 | 0:f38700ce12ea | 67 | bool Pushed[2]={false, false}; |
lilac0112_1 | 0:f38700ce12ea | 68 | InterruptIn HandSW[3]={NC, NC, NC};//p14, p15, p16 |
lilac0112_1 | 0:f38700ce12ea | 69 | DigitalOut Hand[2][2]={{NC, NC}, {NC, NC}};//{p17, p18}, {p19, p20} |
lilac0112_1 | 0:f38700ce12ea | 70 | //ex)Hand[R][Close]=ON; |
lilac0112_1 | 0:f38700ce12ea | 71 | Timeout Touch;//腕で掴むときの安全対策 |
lilac0112_1 | 0:f38700ce12ea | 72 | void StopCatching(void){//停止 |
lilac0112_1 | 0:f38700ce12ea | 73 | Hand[R][Close]=Hand[L][Close]=OFF; |
lilac0112_1 | 0:f38700ce12ea | 74 | } |
lilac0112_1 | 0:f38700ce12ea | 75 | void StopLeaving(void){//停止 |
lilac0112_1 | 0:f38700ce12ea | 76 | Hand[R][Open]=Hand[L][Open]=OFF; |
lilac0112_1 | 0:f38700ce12ea | 77 | } |
lilac0112_1 | 0:f38700ce12ea | 78 | void StartCatching(void){//掴む |
lilac0112_1 | 0:f38700ce12ea | 79 | Hand[R][Close]=Hand[L][Close]=ON; |
lilac0112_1 | 0:f38700ce12ea | 80 | } |
lilac0112_1 | 0:f38700ce12ea | 81 | void StartLeaving(void){//離す |
lilac0112_1 | 0:f38700ce12ea | 82 | Hand[R][Open]=Hand[L][Open]=ON; |
lilac0112_1 | 0:f38700ce12ea | 83 | } |
lilac0112_1 | 0:f38700ce12ea | 84 | void RightCatch(void){//右腕のリミットスイッチ |
lilac0112_1 | 0:f38700ce12ea | 85 | Pushed[R] = true;//接触の記録 |
lilac0112_1 | 0:f38700ce12ea | 86 | if(Pushed[R]&&Pushed[L]){//どちらも接触した時点で停止 |
lilac0112_1 | 0:f38700ce12ea | 87 | Pushed[R] = Pushed[L] = false; |
lilac0112_1 | 0:f38700ce12ea | 88 | Touch.detach();//TimeLimit[s]経たなくても停止 |
lilac0112_1 | 0:f38700ce12ea | 89 | StopCatching(); |
lilac0112_1 | 0:f38700ce12ea | 90 | } |
lilac0112_1 | 0:f38700ce12ea | 91 | } |
lilac0112_1 | 0:f38700ce12ea | 92 | void LeftCatch(void){//左腕のリミットスイッチ |
lilac0112_1 | 0:f38700ce12ea | 93 | Pushed[L] = true;//接触の記録 |
lilac0112_1 | 0:f38700ce12ea | 94 | if(Pushed[R]&&Pushed[L]){//どちらも接触した時点で停止 |
lilac0112_1 | 0:f38700ce12ea | 95 | Pushed[R] = Pushed[L] = false; |
lilac0112_1 | 0:f38700ce12ea | 96 | Touch.detach(); |
lilac0112_1 | 0:f38700ce12ea | 97 | StopCatching(); |
lilac0112_1 | 0:f38700ce12ea | 98 | } |
lilac0112_1 | 0:f38700ce12ea | 99 | } |
lilac0112_1 | 0:f38700ce12ea | 100 | void CenterCatch(void){//中央のリミットスイッチ |
lilac0112_1 | 0:f38700ce12ea | 101 | Touch.attach(StopCatching, TimeLimit);//TimeLimit[s]で停止 |
lilac0112_1 | 0:f38700ce12ea | 102 | StartCatching(); |
lilac0112_1 | 0:f38700ce12ea | 103 | } |
lilac0112_1 | 0:f38700ce12ea | 104 | //通信関連 |
lilac0112_1 | 0:f38700ce12ea | 105 | #define DATA_NUM 10 //DATA_NUM[byte]通信 |
lilac0112_1 | 0:f38700ce12ea | 106 | #define MY_KEYCODE 0xAA//keycode |
lilac0112_1 | 0:f38700ce12ea | 107 | #define EN_KEYCODE 0xAA//keycode |
lilac0112_1 | 0:f38700ce12ea | 108 | #define KEY 0//number of first data |
lilac0112_1 | 0:f38700ce12ea | 109 | #define CHECK DATA_NUM-1//number of last data |
lilac0112_1 | 0:f38700ce12ea | 110 | Serial pc(USBTX, USBRX); |
lilac0112_1 | 0:f38700ce12ea | 111 | Serial Xbee(NC, NC);//p28, p27 |
lilac0112_1 | 0:f38700ce12ea | 112 | volatile uint8_t INdata[DATA_NUM]={0}, EXdata[DATA_NUM]={0}; |
lilac0112_1 | 0:f38700ce12ea | 113 | void TX(void){//送信バッファ空き割り込み用関数 |
lilac0112_1 | 0:f38700ce12ea | 114 | static uint8_t TX=DATA_NUM, i; |
lilac0112_1 | 0:f38700ce12ea | 115 | static uint8_t TXdata[DATA_NUM]={MY_KEYCODE}; |
lilac0112_1 | 0:f38700ce12ea | 116 | static uint8_t TX_CHECKCODE=0; |
lilac0112_1 | 0:f38700ce12ea | 117 | if(TX >= DATA_NUM){ |
lilac0112_1 | 0:f38700ce12ea | 118 | TXdata[KEY] = MY_KEYCODE; |
lilac0112_1 | 0:f38700ce12ea | 119 | TXdata[1] = EXdata[1]; |
lilac0112_1 | 0:f38700ce12ea | 120 | TXdata[2] = EXdata[2]; |
lilac0112_1 | 0:f38700ce12ea | 121 | TXdata[3] = EXdata[3]; |
lilac0112_1 | 0:f38700ce12ea | 122 | TXdata[4] = EXdata[4]; |
lilac0112_1 | 0:f38700ce12ea | 123 | TXdata[5] = EXdata[5]; |
lilac0112_1 | 0:f38700ce12ea | 124 | TXdata[6] = EXdata[6]; |
lilac0112_1 | 0:f38700ce12ea | 125 | TXdata[7] = EXdata[7]; |
lilac0112_1 | 0:f38700ce12ea | 126 | TXdata[8] = EXdata[8]; |
lilac0112_1 | 0:f38700ce12ea | 127 | //送るデータ = センサ等のデータ |
lilac0112_1 | 0:f38700ce12ea | 128 | for(i=KEY+1, TX_CHECKCODE=0; i<CHECK; i++){ |
lilac0112_1 | 0:f38700ce12ea | 129 | TX_CHECKCODE ^= TXdata[i]; |
lilac0112_1 | 0:f38700ce12ea | 130 | }//CHECKCODE作成 |
lilac0112_1 | 0:f38700ce12ea | 131 | TXdata[CHECK] = TX_CHECKCODE; |
lilac0112_1 | 0:f38700ce12ea | 132 | TX=0; |
lilac0112_1 | 0:f38700ce12ea | 133 | } |
lilac0112_1 | 0:f38700ce12ea | 134 | Xbee.putc(TXdata[TX]); |
lilac0112_1 | 0:f38700ce12ea | 135 | TX++; |
lilac0112_1 | 0:f38700ce12ea | 136 | }//受信割り込み用関数 |
lilac0112_1 | 0:f38700ce12ea | 137 | void RX(void){ |
lilac0112_1 | 0:f38700ce12ea | 138 | static uint8_t RX=0, i; |
lilac0112_1 | 0:f38700ce12ea | 139 | static uint8_t RXdata[DATA_NUM]={EN_KEYCODE}; |
lilac0112_1 | 0:f38700ce12ea | 140 | static uint8_t RX_CHECKCODE=0; |
lilac0112_1 | 0:f38700ce12ea | 141 | RXdata[RX] = Xbee.getc(); |
lilac0112_1 | 0:f38700ce12ea | 142 | if(RXdata[KEY]==EN_KEYCODE){ |
lilac0112_1 | 0:f38700ce12ea | 143 | RX++; |
lilac0112_1 | 0:f38700ce12ea | 144 | } |
lilac0112_1 | 0:f38700ce12ea | 145 | if(RX==CHECK){ |
lilac0112_1 | 0:f38700ce12ea | 146 | for(i=KEY+1, RX_CHECKCODE=0; i<CHECK; i++){ |
lilac0112_1 | 0:f38700ce12ea | 147 | RX_CHECKCODE ^= RXdata[i]; |
lilac0112_1 | 0:f38700ce12ea | 148 | }//CHECKCODE作成 |
lilac0112_1 | 0:f38700ce12ea | 149 | } |
lilac0112_1 | 0:f38700ce12ea | 150 | if(RX >= DATA_NUM){ |
lilac0112_1 | 0:f38700ce12ea | 151 | if(RXdata[CHECK]==RX_CHECKCODE){ |
lilac0112_1 | 0:f38700ce12ea | 152 | INdata[1] = RXdata[1]; |
lilac0112_1 | 0:f38700ce12ea | 153 | INdata[2] = RXdata[2]; |
lilac0112_1 | 0:f38700ce12ea | 154 | INdata[3] = RXdata[3]; |
lilac0112_1 | 0:f38700ce12ea | 155 | INdata[4] = RXdata[4]; |
lilac0112_1 | 0:f38700ce12ea | 156 | INdata[5] = RXdata[5]; |
lilac0112_1 | 0:f38700ce12ea | 157 | INdata[6] = RXdata[6]; |
lilac0112_1 | 0:f38700ce12ea | 158 | INdata[7] = RXdata[7]; |
lilac0112_1 | 0:f38700ce12ea | 159 | INdata[8] = RXdata[8]; |
lilac0112_1 | 0:f38700ce12ea | 160 | //使うデータ = 受け取ったデータ |
lilac0112_1 | 0:f38700ce12ea | 161 | } |
lilac0112_1 | 0:f38700ce12ea | 162 | RX=0; |
lilac0112_1 | 0:f38700ce12ea | 163 | } |
lilac0112_1 | 0:f38700ce12ea | 164 | } |