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@23:5c56747bcc43, 2015-09-18 (annotated)
- Committer:
- DeguNaoto
- Date:
- Fri Sep 18 02:36:45 2015 +0000
- Revision:
- 23:5c56747bcc43
- Parent:
- 22:3996c3f41922
- Child:
- 25:e591b9fa5796
manualModePs3()?????;
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
DeguNaoto | 0:bd4719e15f7e | 1 | /***RS485 Communication.***/ |
DeguNaoto | 18:526124eef0d1 | 2 | #define RS485_TX p13 |
DeguNaoto | 18:526124eef0d1 | 3 | #define RS485_RX p14 |
DeguNaoto | 0:bd4719e15f7e | 4 | Serial RS485(RS485_TX, RS485_RX); |
DeguNaoto | 4:51d87d2b698c | 5 | inline void sendData(int address, int data5) { |
DeguNaoto | 4:51d87d2b698c | 6 | unsigned int data; |
DeguNaoto | 4:51d87d2b698c | 7 | if (data5 <= 31) { |
DeguNaoto | 4:51d87d2b698c | 8 | data = ((address << 5) | data5); |
DeguNaoto | 4:51d87d2b698c | 9 | RS485.putc(data); |
DeguNaoto | 4:51d87d2b698c | 10 | } |
DeguNaoto | 4:51d87d2b698c | 11 | } |
DeguNaoto | 0:bd4719e15f7e | 12 | |
DeguNaoto | 0:bd4719e15f7e | 13 | /***Get state ps3con.***/ |
DeguNaoto | 18:526124eef0d1 | 14 | #define SBDBT_TX p28 |
DeguNaoto | 18:526124eef0d1 | 15 | #define SBDBT_RX p27 |
DeguNaoto | 0:bd4719e15f7e | 16 | Serial SBDBT(SBDBT_TX, SBDBT_RX); |
DeguNaoto | 0:bd4719e15f7e | 17 | #define ps3_start 0x80 |
DeguNaoto | 18:526124eef0d1 | 18 | short toggle = 0; |
DeguNaoto | 18:526124eef0d1 | 19 | short edge_circle = 0; |
DeguNaoto | 18:526124eef0d1 | 20 | short edge_triangle = 0; |
DeguNaoto | 21:79b94cb922f0 | 21 | short edge_square = 0; |
DeguNaoto | 18:526124eef0d1 | 22 | short edge_r1 = 0; |
DeguNaoto | 18:526124eef0d1 | 23 | short edge_l1 = 0; |
DeguNaoto | 18:526124eef0d1 | 24 | short edge_l_up = 0; |
DeguNaoto | 18:526124eef0d1 | 25 | short edge_l_down = 0; |
DeguNaoto | 18:526124eef0d1 | 26 | short edge_r_up = 0; |
DeguNaoto | 18:526124eef0d1 | 27 | short edge_r_down = 0; |
DeguNaoto | 18:526124eef0d1 | 28 | short edge_right = 0; |
DeguNaoto | 18:526124eef0d1 | 29 | short edge_left = 0; |
DeguNaoto | 18:526124eef0d1 | 30 | short edge_up = 0; |
DeguNaoto | 18:526124eef0d1 | 31 | short square = 0; |
DeguNaoto | 18:526124eef0d1 | 32 | short triangle = 0; |
DeguNaoto | 18:526124eef0d1 | 33 | short circle = 0; |
DeguNaoto | 18:526124eef0d1 | 34 | short cross = 0; |
DeguNaoto | 18:526124eef0d1 | 35 | short up = 0; |
DeguNaoto | 18:526124eef0d1 | 36 | short down = 0; |
DeguNaoto | 18:526124eef0d1 | 37 | short right = 0; |
DeguNaoto | 18:526124eef0d1 | 38 | short left = 0; |
DeguNaoto | 18:526124eef0d1 | 39 | short l1 = 0; |
DeguNaoto | 18:526124eef0d1 | 40 | short l2 = 0; |
DeguNaoto | 18:526124eef0d1 | 41 | short r1 = 0; |
DeguNaoto | 18:526124eef0d1 | 42 | short r2 = 0; |
DeguNaoto | 18:526124eef0d1 | 43 | signed int analog_lx = 0; |
DeguNaoto | 18:526124eef0d1 | 44 | signed int analog_ly = 0; |
DeguNaoto | 18:526124eef0d1 | 45 | signed int analog_rx = 0; |
DeguNaoto | 18:526124eef0d1 | 46 | signed int analog_ry = 0; |
DeguNaoto | 18:526124eef0d1 | 47 | int count = 0; |
DeguNaoto | 18:526124eef0d1 | 48 | int sample = 0; |
DeguNaoto | 0:bd4719e15f7e | 49 | int ps3_data[7]; |
DeguNaoto | 18:526124eef0d1 | 50 | inline void data_clear(); |
DeguNaoto | 18:526124eef0d1 | 51 | inline void data_check(); |
DeguNaoto | 18:526124eef0d1 | 52 | inline void SBDBT_interrupt(); |
DeguNaoto | 18:526124eef0d1 | 53 | inline void initializeSBDBT(); |
DeguNaoto | 18:526124eef0d1 | 54 | |
DeguNaoto | 2:cf8ca6742db9 | 55 | inline void data_clear() |
DeguNaoto | 2:cf8ca6742db9 | 56 | { |
DeguNaoto | 2:cf8ca6742db9 | 57 | for(int i=0; i<7; i++) ps3_data[i]=0; |
DeguNaoto | 0:bd4719e15f7e | 58 | } |
DeguNaoto | 2:cf8ca6742db9 | 59 | inline void data_check() |
DeguNaoto | 2:cf8ca6742db9 | 60 | { |
DeguNaoto | 0:bd4719e15f7e | 61 | 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 | 62 | analog_lx=0,analog_ly=0,analog_rx=0,analog_ry=0; |
DeguNaoto | 0:bd4719e15f7e | 63 | if((ps3_data[0]==0x00)&&(ps3_data[1]==0x01)) up=1; |
DeguNaoto | 0:bd4719e15f7e | 64 | else if((ps3_data[0]==0x00)&&(ps3_data[1]==0x02)) down=1; |
DeguNaoto | 0:bd4719e15f7e | 65 | else if((ps3_data[0]==0x00)&&(ps3_data[1]==0x04)) right=1; |
DeguNaoto | 0:bd4719e15f7e | 66 | else if((ps3_data[0]==0x00)&&(ps3_data[1]==0x08)) left=1; |
DeguNaoto | 0:bd4719e15f7e | 67 | else if((ps3_data[0]==0x00)&&(ps3_data[1]==0x10)) triangle=1; |
DeguNaoto | 0:bd4719e15f7e | 68 | else if((ps3_data[0]==0x00)&&(ps3_data[1]==0x20)) cross=1; |
DeguNaoto | 0:bd4719e15f7e | 69 | else if((ps3_data[0]==0x00)&&(ps3_data[1]==0x40)) circle=1; |
DeguNaoto | 0:bd4719e15f7e | 70 | else if((ps3_data[0]==0x01)&&(ps3_data[1]==0x00)) square=1; |
DeguNaoto | 0:bd4719e15f7e | 71 | else if((ps3_data[0]==0x02)&&(ps3_data[1]==0x00)) l1=1; |
DeguNaoto | 0:bd4719e15f7e | 72 | else if((ps3_data[0]==0x04)&&(ps3_data[1]==0x00)) l2=1; |
DeguNaoto | 0:bd4719e15f7e | 73 | else if((ps3_data[0]==0x08)&&(ps3_data[1]==0x00)) r1=1; |
DeguNaoto | 0:bd4719e15f7e | 74 | else if((ps3_data[0]==0x10)&&(ps3_data[1]==0x00)) r2=1; |
DeguNaoto | 8:1638e8c8ca93 | 75 | analog_lx=64-(signed int)ps3_data[2]; |
DeguNaoto | 8:1638e8c8ca93 | 76 | analog_ly=64-(signed int)ps3_data[3]; |
DeguNaoto | 8:1638e8c8ca93 | 77 | analog_rx=64-(signed int)ps3_data[4]; |
DeguNaoto | 8:1638e8c8ca93 | 78 | analog_ry=64-(signed int)ps3_data[5]; |
DeguNaoto | 4:51d87d2b698c | 79 | if(!circle) edge_circle=1; |
DeguNaoto | 4:51d87d2b698c | 80 | if(!triangle) edge_triangle=1; |
DeguNaoto | 4:51d87d2b698c | 81 | if(!r1) edge_r1=1; |
DeguNaoto | 4:51d87d2b698c | 82 | if(!l1) edge_l1=1; |
DeguNaoto | 4:51d87d2b698c | 83 | if(!edge_right) edge_right=1; |
DeguNaoto | 4:51d87d2b698c | 84 | if(!edge_left) edge_left=1; |
DeguNaoto | 4:51d87d2b698c | 85 | if(!edge_up) edge_up=1; |
DeguNaoto | 21:79b94cb922f0 | 86 | if(!edge_square) edge_square=1; |
DeguNaoto | 0:bd4719e15f7e | 87 | } |
DeguNaoto | 0:bd4719e15f7e | 88 | ///interrupt SBDBT RX. |
DeguNaoto | 2:cf8ca6742db9 | 89 | inline void SBDBT_interrupt() |
DeguNaoto | 2:cf8ca6742db9 | 90 | { |
DeguNaoto | 0:bd4719e15f7e | 91 | sample=SBDBT.getc(); |
DeguNaoto | 0:bd4719e15f7e | 92 | if(count==7) data_clear(); |
DeguNaoto | 0:bd4719e15f7e | 93 | if(sample==ps3_start) count=0; |
DeguNaoto | 2:cf8ca6742db9 | 94 | else { |
DeguNaoto | 0:bd4719e15f7e | 95 | ps3_data[count]=sample; |
DeguNaoto | 0:bd4719e15f7e | 96 | count++; |
DeguNaoto | 0:bd4719e15f7e | 97 | } |
DeguNaoto | 0:bd4719e15f7e | 98 | if(count==6) data_check(); |
DeguNaoto | 0:bd4719e15f7e | 99 | } |
DeguNaoto | 2:cf8ca6742db9 | 100 | inline void initializeSBDBT() |
DeguNaoto | 2:cf8ca6742db9 | 101 | { |
DeguNaoto | 22:3996c3f41922 | 102 | wait(0.1); |
DeguNaoto | 22:3996c3f41922 | 103 | while(analog_ly==-64) |
DeguNaoto | 2:cf8ca6742db9 | 104 | for(int i=0; i<7; i++) ps3_data[i]=0; |
DeguNaoto | 0:bd4719e15f7e | 105 | SBDBT.baud(2400); |
DeguNaoto | 0:bd4719e15f7e | 106 | SBDBT.format(8, Serial::None, 1); |
DeguNaoto | 0:bd4719e15f7e | 107 | SBDBT.attach(SBDBT_interrupt, Serial::RxIrq); |
DeguNaoto | 0:bd4719e15f7e | 108 | } |