for CAN communication between mbed
Dependents: EM_Logger EM_Mission
myCAN.cpp@2:e0e7269bf1f0, 2013-08-03 (annotated)
- Committer:
- YSB
- Date:
- Sat Aug 03 09:13:48 2013 +0000
- Revision:
- 2:e0e7269bf1f0
- Parent:
- 1:0732a5dec208
aaa
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
YSB | 0:89073e0112db | 1 | #include "myCAN.h" |
YSB | 0:89073e0112db | 2 | #include "IDDATA.h" |
YSB | 0:89073e0112db | 3 | |
YSB | 0:89073e0112db | 4 | myCAN::myCAN(PinName rd,PinName td) : CAN(rd,td) |
YSB | 0:89073e0112db | 5 | { |
YSB | 0:89073e0112db | 6 | frequency(CAN_BAUD); |
YSB | 0:89073e0112db | 7 | attach(this, &myCAN::rxHandler); |
YSB | 0:89073e0112db | 8 | |
YSB | 0:89073e0112db | 9 | time[0]='0'; |
YSB | 0:89073e0112db | 10 | time[1]='0'; |
YSB | 0:89073e0112db | 11 | time[2]=':'; |
YSB | 0:89073e0112db | 12 | time[3]='0'; |
YSB | 0:89073e0112db | 13 | time[4]='0'; |
YSB | 0:89073e0112db | 14 | time[5]=':'; |
YSB | 0:89073e0112db | 15 | time[6]='0'; |
YSB | 0:89073e0112db | 16 | time[7]='0'; |
YSB | 0:89073e0112db | 17 | time[8]='\0'; |
YSB | 0:89073e0112db | 18 | |
YSB | 0:89073e0112db | 19 | latitude[0]='0'; |
YSB | 0:89073e0112db | 20 | latitude[1]='0'; |
YSB | 0:89073e0112db | 21 | latitude[2]='0'; |
YSB | 0:89073e0112db | 22 | latitude[3]='0'; |
YSB | 0:89073e0112db | 23 | latitude[4]=','; |
YSB | 0:89073e0112db | 24 | latitude[5]='0'; |
YSB | 0:89073e0112db | 25 | latitude[6]='0'; |
YSB | 0:89073e0112db | 26 | latitude[7]='0'; |
YSB | 0:89073e0112db | 27 | latitude[8]='0'; |
YSB | 0:89073e0112db | 28 | latitude[9]='\0'; |
YSB | 0:89073e0112db | 29 | |
YSB | 0:89073e0112db | 30 | longitude[0]='0'; |
YSB | 0:89073e0112db | 31 | longitude[1]='0'; |
YSB | 0:89073e0112db | 32 | longitude[2]='0'; |
YSB | 0:89073e0112db | 33 | longitude[3]='0'; |
YSB | 0:89073e0112db | 34 | longitude[4]='0'; |
YSB | 0:89073e0112db | 35 | longitude[5]=','; |
YSB | 0:89073e0112db | 36 | longitude[6]='0'; |
YSB | 0:89073e0112db | 37 | longitude[7]='0'; |
YSB | 0:89073e0112db | 38 | longitude[8]='0'; |
YSB | 0:89073e0112db | 39 | longitude[9]='0'; |
YSB | 0:89073e0112db | 40 | longitude[10]='\0'; |
YSB | 0:89073e0112db | 41 | } |
YSB | 0:89073e0112db | 42 | |
YSB | 0:89073e0112db | 43 | void myCAN::send(char user) |
YSB | 0:89073e0112db | 44 | { |
YSB | 0:89073e0112db | 45 | if(user == MISSION) { |
YSB | 0:89073e0112db | 46 | write(CANMessage(MISSION_STATUS,CANdata[MISSION_STATUS])); |
YSB | 2:e0e7269bf1f0 | 47 | wait_ms(5); |
YSB | 0:89073e0112db | 48 | } else if(user == LOGGER) { |
YSB | 0:89073e0112db | 49 | write(CANMessage(GPS_TIME,CANdata[GPS_TIME])); |
YSB | 1:0732a5dec208 | 50 | wait_ms(5); |
YSB | 0:89073e0112db | 51 | write(CANMessage(GPS_LAT_1,CANdata[GPS_LAT_1])); |
YSB | 1:0732a5dec208 | 52 | wait_ms(5); |
YSB | 0:89073e0112db | 53 | write(CANMessage(GPS_LAT_2,CANdata[GPS_LAT_2])); |
YSB | 1:0732a5dec208 | 54 | wait_ms(5); |
YSB | 0:89073e0112db | 55 | write(CANMessage(GPS_LON_1,CANdata[GPS_LON_1])); |
YSB | 1:0732a5dec208 | 56 | wait_ms(5); |
YSB | 0:89073e0112db | 57 | write(CANMessage(GPS_LON_2,CANdata[GPS_LON_2])); |
YSB | 1:0732a5dec208 | 58 | wait_ms(5); |
YSB | 0:89073e0112db | 59 | write(CANMessage(SCP1000_PRES_TEMP,CANdata[SCP1000_PRES_TEMP])); |
YSB | 1:0732a5dec208 | 60 | wait_ms(5); |
YSB | 2:e0e7269bf1f0 | 61 | write(CANMessage(ANGLE_DATA,CANdata[ANGLE_DATA])); |
YSB | 1:0732a5dec208 | 62 | wait_ms(5); |
YSB | 0:89073e0112db | 63 | } else {} |
YSB | 0:89073e0112db | 64 | } |
YSB | 0:89073e0112db | 65 | |
YSB | 0:89073e0112db | 66 | void myCAN::rxHandler(void) |
YSB | 0:89073e0112db | 67 | { |
YSB | 0:89073e0112db | 68 | if(read(msg)){ |
YSB | 0:89073e0112db | 69 | for(int i=0; i<8; i++) { |
YSB | 0:89073e0112db | 70 | CANdata[msg.id][i]=msg.data[i]; |
YSB | 0:89073e0112db | 71 | } |
YSB | 0:89073e0112db | 72 | } |
YSB | 0:89073e0112db | 73 | } |
YSB | 0:89073e0112db | 74 | |
YSB | 1:0732a5dec208 | 75 | void myCAN::make_logger_senddata(char* time,int NoS,char* latitude,char* longitude,int roll,int temp,int pres) |
YSB | 0:89073e0112db | 76 | { |
YSB | 0:89073e0112db | 77 | for(int i=0; i<8; i++) { |
YSB | 0:89073e0112db | 78 | CANdata[GPS_TIME][i] = time[i]; |
YSB | 0:89073e0112db | 79 | } |
YSB | 0:89073e0112db | 80 | CANdata[GPS_LAT_2][1] = NoS; |
YSB | 0:89073e0112db | 81 | |
YSB | 0:89073e0112db | 82 | for(int i=0; i<8; i++) { |
YSB | 0:89073e0112db | 83 | CANdata[GPS_LAT_1][i] = latitude[i]; |
YSB | 0:89073e0112db | 84 | } |
YSB | 0:89073e0112db | 85 | for(int i=0; i<1; i++) { |
YSB | 0:89073e0112db | 86 | CANdata[GPS_LAT_2][i] = latitude[i+8]; |
YSB | 0:89073e0112db | 87 | } |
YSB | 0:89073e0112db | 88 | |
YSB | 0:89073e0112db | 89 | for(int i=0; i<8; i++) { |
YSB | 0:89073e0112db | 90 | CANdata[GPS_LON_1][i] = longitude[i]; |
YSB | 0:89073e0112db | 91 | } |
YSB | 0:89073e0112db | 92 | for(int i=0; i<2; i++) { |
YSB | 0:89073e0112db | 93 | CANdata[GPS_LON_2][i] = longitude[i+8]; |
YSB | 0:89073e0112db | 94 | } |
YSB | 0:89073e0112db | 95 | |
YSB | 2:e0e7269bf1f0 | 96 | CANdata[SCP1000_PRES_TEMP][0]=(char)((pres & 0xFF0000)>>16); |
YSB | 2:e0e7269bf1f0 | 97 | CANdata[SCP1000_PRES_TEMP][1]=(char)((pres & 0x00FF00)>>8); |
YSB | 2:e0e7269bf1f0 | 98 | CANdata[SCP1000_PRES_TEMP][2]=(char)(pres & 0x0000FF); |
YSB | 2:e0e7269bf1f0 | 99 | CANdata[SCP1000_PRES_TEMP][3]=(char)((temp & 0xFF00)>>8); |
YSB | 2:e0e7269bf1f0 | 100 | CANdata[SCP1000_PRES_TEMP][4]=(char)(temp & 0x00FF); |
YSB | 0:89073e0112db | 101 | |
YSB | 2:e0e7269bf1f0 | 102 | CANdata[ANGLE_DATA][0] = (char)((roll&0xFF00)>>8); |
YSB | 2:e0e7269bf1f0 | 103 | CANdata[ANGLE_DATA][1] = (char)(roll&0x00FF); |
YSB | 0:89073e0112db | 104 | } |
YSB | 0:89073e0112db | 105 | |
YSB | 0:89073e0112db | 106 | char* myCAN::get_time(void) |
YSB | 0:89073e0112db | 107 | { |
YSB | 0:89073e0112db | 108 | for(int i=0; i<8; i++) { |
YSB | 0:89073e0112db | 109 | time[i] = CANdata[GPS_TIME][i]; |
YSB | 0:89073e0112db | 110 | } |
YSB | 0:89073e0112db | 111 | return time; |
YSB | 0:89073e0112db | 112 | } |
YSB | 0:89073e0112db | 113 | |
YSB | 0:89073e0112db | 114 | char* myCAN::get_latitude(void) |
YSB | 0:89073e0112db | 115 | { |
YSB | 0:89073e0112db | 116 | for(int i=0; i<8; i++) { |
YSB | 0:89073e0112db | 117 | latitude[i] = CANdata[GPS_LAT_1][i]; |
YSB | 0:89073e0112db | 118 | } |
YSB | 0:89073e0112db | 119 | for(int i=0; i<1; i++) { |
YSB | 0:89073e0112db | 120 | latitude[i+8] = CANdata[GPS_LAT_2][i]; |
YSB | 0:89073e0112db | 121 | } |
YSB | 0:89073e0112db | 122 | return latitude; |
YSB | 0:89073e0112db | 123 | } |
YSB | 0:89073e0112db | 124 | |
YSB | 0:89073e0112db | 125 | char* myCAN::get_longitude(void) |
YSB | 0:89073e0112db | 126 | { |
YSB | 0:89073e0112db | 127 | for(int i=0; i<8; i++) { |
YSB | 0:89073e0112db | 128 | longitude[i] = CANdata[GPS_LON_1][i]; |
YSB | 0:89073e0112db | 129 | } |
YSB | 0:89073e0112db | 130 | for(int i=0; i<1; i++) { |
YSB | 0:89073e0112db | 131 | longitude[i+8] = CANdata[GPS_LON_2][i]; |
YSB | 0:89073e0112db | 132 | } |
YSB | 0:89073e0112db | 133 | return longitude; |
YSB | 0:89073e0112db | 134 | } |
YSB | 0:89073e0112db | 135 | |
YSB | 0:89073e0112db | 136 | int myCAN::get_NoS(void) |
YSB | 0:89073e0112db | 137 | { |
YSB | 0:89073e0112db | 138 | NoS = CANdata[GPS_LAT_2][1]; |
YSB | 0:89073e0112db | 139 | return NoS; |
YSB | 0:89073e0112db | 140 | } |
YSB | 0:89073e0112db | 141 | |
YSB | 1:0732a5dec208 | 142 | int myCAN::get_roll(void){ |
YSB | 2:e0e7269bf1f0 | 143 | hroll = CANdata[ANGLE_DATA][0]; |
YSB | 2:e0e7269bf1f0 | 144 | lroll = CANdata[ANGLE_DATA][1]; |
YSB | 1:0732a5dec208 | 145 | roll = ((((int)(hroll))<<8) & 0xFF00) | (((int)(lroll))&0x00FF); |
YSB | 1:0732a5dec208 | 146 | return roll; |
YSB | 0:89073e0112db | 147 | } |
YSB | 0:89073e0112db | 148 | |
YSB | 0:89073e0112db | 149 | float myCAN::get_temp(void) |
YSB | 0:89073e0112db | 150 | { |
YSB | 2:e0e7269bf1f0 | 151 | temp = (float)((((int)(CANdata[SCP1000_PRES_TEMP][3]))<<8+(int)(CANdata[SCP1000_PRES_TEMP][4]))/20.0); |
YSB | 0:89073e0112db | 152 | return temp; |
YSB | 0:89073e0112db | 153 | } |
YSB | 0:89073e0112db | 154 | |
YSB | 0:89073e0112db | 155 | int myCAN::get_pres(void) |
YSB | 0:89073e0112db | 156 | { |
YSB | 2:e0e7269bf1f0 | 157 | pres = ((int)(CANdata[SCP1000_PRES_TEMP][0]))<<16 + ((int)(CANdata[SCP1000_PRES_TEMP][1]))<<8+(int)(CANdata[SCP1000_PRES_TEMP][8]); |
YSB | 0:89073e0112db | 158 | return pres; |
YSB | 0:89073e0112db | 159 | } |