Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Fork of 2015robot_main by
communicate.h@14:a99f81878336, 2015-09-16 (annotated)
- Committer:
- DeguNaoto
- Date:
- Wed Sep 16 08:11:06 2015 +0000
- Revision:
- 14:a99f81878336
- Parent:
- 13:87794ce49b50
??????????????????????? "MachineState"??????
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
DeguNaoto | 0:bd4719e15f7e | 1 | /***RS485 Communication.***/ |
DeguNaoto | 4:51d87d2b698c | 2 | |
DeguNaoto | 0:bd4719e15f7e | 3 | Serial RS485(RS485_TX, RS485_RX); |
DeguNaoto | 4:51d87d2b698c | 4 | inline void sendData(int address, int data5) { |
DeguNaoto | 4:51d87d2b698c | 5 | unsigned int data; |
DeguNaoto | 4:51d87d2b698c | 6 | if (data5 <= 31) { |
DeguNaoto | 4:51d87d2b698c | 7 | data = ((address << 5) | data5); |
DeguNaoto | 4:51d87d2b698c | 8 | RS485.putc(data); |
DeguNaoto | 4:51d87d2b698c | 9 | } |
DeguNaoto | 4:51d87d2b698c | 10 | } |
DeguNaoto | 0:bd4719e15f7e | 11 | |
DeguNaoto | 0:bd4719e15f7e | 12 | /***Get state ps3con.***/ |
DeguNaoto | 13:87794ce49b50 | 13 | inline void SBDBT_interrupt(); |
DeguNaoto | 13:87794ce49b50 | 14 | inline void initializeSBDBT(); |
DeguNaoto | 14:a99f81878336 | 15 | inline void data_clear(); |
DeguNaoto | 14:a99f81878336 | 16 | inline void data_check(); |
DeguNaoto | 0:bd4719e15f7e | 17 | Serial SBDBT(SBDBT_TX, SBDBT_RX); |
DeguNaoto | 0:bd4719e15f7e | 18 | #define ps3_start 0x80 |
DeguNaoto | 14:a99f81878336 | 19 | short toggle = 0; |
DeguNaoto | 14:a99f81878336 | 20 | short edge_circle = 0; |
DeguNaoto | 14:a99f81878336 | 21 | short edge_triangle = 0; |
DeguNaoto | 14:a99f81878336 | 22 | short edge_r1 = 0; |
DeguNaoto | 14:a99f81878336 | 23 | short edge_l1 = 0; |
DeguNaoto | 14:a99f81878336 | 24 | short edge_l_up = 0; |
DeguNaoto | 14:a99f81878336 | 25 | short edge_l_down = 0; |
DeguNaoto | 14:a99f81878336 | 26 | short edge_r_up = 0; |
DeguNaoto | 14:a99f81878336 | 27 | short edge_r_down = 0; |
DeguNaoto | 14:a99f81878336 | 28 | short edge_right = 0; |
DeguNaoto | 14:a99f81878336 | 29 | short edge_left = 0; |
DeguNaoto | 14:a99f81878336 | 30 | short edge_up = 0; |
DeguNaoto | 14:a99f81878336 | 31 | short square = 0; |
DeguNaoto | 14:a99f81878336 | 32 | short triangle = 0; |
DeguNaoto | 14:a99f81878336 | 33 | short circle = 0; |
DeguNaoto | 14:a99f81878336 | 34 | short cross = 0; |
DeguNaoto | 14:a99f81878336 | 35 | short up = 0; |
DeguNaoto | 14:a99f81878336 | 36 | short down = 0; |
DeguNaoto | 14:a99f81878336 | 37 | short right = 0; |
DeguNaoto | 14:a99f81878336 | 38 | short left = 0; |
DeguNaoto | 14:a99f81878336 | 39 | short l1 = 0; |
DeguNaoto | 14:a99f81878336 | 40 | short l2 = 0; |
DeguNaoto | 14:a99f81878336 | 41 | short r1 = 0; |
DeguNaoto | 14:a99f81878336 | 42 | short r2 = 0; |
DeguNaoto | 8:1638e8c8ca93 | 43 | signed int analog_lx=0,analog_ly=0,analog_rx=0,analog_ry=0; |
DeguNaoto | 0:bd4719e15f7e | 44 | int ps3_data[7]; |
DeguNaoto | 0:bd4719e15f7e | 45 | int count=0; |
DeguNaoto | 0:bd4719e15f7e | 46 | int sample=0; |
DeguNaoto | 2:cf8ca6742db9 | 47 | inline void data_clear() |
DeguNaoto | 2:cf8ca6742db9 | 48 | { |
DeguNaoto | 2:cf8ca6742db9 | 49 | for(int i=0; i<7; i++) ps3_data[i]=0; |
DeguNaoto | 0:bd4719e15f7e | 50 | } |
DeguNaoto | 2:cf8ca6742db9 | 51 | inline void data_check() |
DeguNaoto | 2:cf8ca6742db9 | 52 | { |
DeguNaoto | 0:bd4719e15f7e | 53 | square=0,triangle=0,circle=0,cross=0,up=0,down=0,right=0,left=0,l1=0,l2=0,r1=0,r2=0; |
DeguNaoto | 0:bd4719e15f7e | 54 | analog_lx=0,analog_ly=0,analog_rx=0,analog_ry=0; |
DeguNaoto | 0:bd4719e15f7e | 55 | if((ps3_data[0]==0x00)&&(ps3_data[1]==0x01)) up=1; |
DeguNaoto | 0:bd4719e15f7e | 56 | else if((ps3_data[0]==0x00)&&(ps3_data[1]==0x02)) down=1; |
DeguNaoto | 0:bd4719e15f7e | 57 | else if((ps3_data[0]==0x00)&&(ps3_data[1]==0x04)) right=1; |
DeguNaoto | 0:bd4719e15f7e | 58 | else if((ps3_data[0]==0x00)&&(ps3_data[1]==0x08)) left=1; |
DeguNaoto | 0:bd4719e15f7e | 59 | else if((ps3_data[0]==0x00)&&(ps3_data[1]==0x10)) triangle=1; |
DeguNaoto | 0:bd4719e15f7e | 60 | else if((ps3_data[0]==0x00)&&(ps3_data[1]==0x20)) cross=1; |
DeguNaoto | 0:bd4719e15f7e | 61 | else if((ps3_data[0]==0x00)&&(ps3_data[1]==0x40)) circle=1; |
DeguNaoto | 0:bd4719e15f7e | 62 | else if((ps3_data[0]==0x01)&&(ps3_data[1]==0x00)) square=1; |
DeguNaoto | 0:bd4719e15f7e | 63 | else if((ps3_data[0]==0x02)&&(ps3_data[1]==0x00)) l1=1; |
DeguNaoto | 0:bd4719e15f7e | 64 | else if((ps3_data[0]==0x04)&&(ps3_data[1]==0x00)) l2=1; |
DeguNaoto | 0:bd4719e15f7e | 65 | else if((ps3_data[0]==0x08)&&(ps3_data[1]==0x00)) r1=1; |
DeguNaoto | 0:bd4719e15f7e | 66 | else if((ps3_data[0]==0x10)&&(ps3_data[1]==0x00)) r2=1; |
DeguNaoto | 8:1638e8c8ca93 | 67 | analog_lx=64-(signed int)ps3_data[2]; |
DeguNaoto | 8:1638e8c8ca93 | 68 | analog_ly=64-(signed int)ps3_data[3]; |
DeguNaoto | 8:1638e8c8ca93 | 69 | analog_rx=64-(signed int)ps3_data[4]; |
DeguNaoto | 8:1638e8c8ca93 | 70 | analog_ry=64-(signed int)ps3_data[5]; |
DeguNaoto | 4:51d87d2b698c | 71 | if(!circle) edge_circle=1; |
DeguNaoto | 4:51d87d2b698c | 72 | if(!triangle) edge_triangle=1; |
DeguNaoto | 4:51d87d2b698c | 73 | if(!r1) edge_r1=1; |
DeguNaoto | 4:51d87d2b698c | 74 | if(!l1) edge_l1=1; |
DeguNaoto | 4:51d87d2b698c | 75 | /*if(!(analog_ry<40)) edge_r_up=1; |
DeguNaoto | 4:51d87d2b698c | 76 | if(!(analog_ry>90)) edge_r_down=1; |
DeguNaoto | 4:51d87d2b698c | 77 | if(!(analog_ly<40)) edge_l_up=1; |
DeguNaoto | 4:51d87d2b698c | 78 | if(!(analog_ly>90)) edge_l_down=1;*/ |
DeguNaoto | 4:51d87d2b698c | 79 | if(!edge_right) edge_right=1; |
DeguNaoto | 4:51d87d2b698c | 80 | if(!edge_left) edge_left=1; |
DeguNaoto | 4:51d87d2b698c | 81 | if(!edge_up) edge_up=1; |
DeguNaoto | 0:bd4719e15f7e | 82 | } |
DeguNaoto | 0:bd4719e15f7e | 83 | ///interrupt SBDBT RX. |
DeguNaoto | 2:cf8ca6742db9 | 84 | inline void SBDBT_interrupt() |
DeguNaoto | 2:cf8ca6742db9 | 85 | { |
DeguNaoto | 0:bd4719e15f7e | 86 | sample=SBDBT.getc(); |
DeguNaoto | 0:bd4719e15f7e | 87 | if(count==7) data_clear(); |
DeguNaoto | 0:bd4719e15f7e | 88 | if(sample==ps3_start) count=0; |
DeguNaoto | 2:cf8ca6742db9 | 89 | else { |
DeguNaoto | 0:bd4719e15f7e | 90 | ps3_data[count]=sample; |
DeguNaoto | 0:bd4719e15f7e | 91 | count++; |
DeguNaoto | 0:bd4719e15f7e | 92 | } |
DeguNaoto | 0:bd4719e15f7e | 93 | if(count==6) data_check(); |
DeguNaoto | 0:bd4719e15f7e | 94 | } |
DeguNaoto | 2:cf8ca6742db9 | 95 | inline void initializeSBDBT() |
DeguNaoto | 2:cf8ca6742db9 | 96 | { |
DeguNaoto | 2:cf8ca6742db9 | 97 | for(int i=0; i<7; i++) ps3_data[i]=0; |
DeguNaoto | 0:bd4719e15f7e | 98 | SBDBT.baud(2400); |
DeguNaoto | 0:bd4719e15f7e | 99 | SBDBT.format(8, Serial::None, 1); |
DeguNaoto | 0:bd4719e15f7e | 100 | SBDBT.attach(SBDBT_interrupt, Serial::RxIrq); |
DeguNaoto | 0:bd4719e15f7e | 101 | } |