for CAN communication between mbed
Dependents: EM_Logger EM_Mission
myCAN.cpp@0:89073e0112db, 2013-07-21 (annotated)
- Committer:
- YSB
- Date:
- Sun Jul 21 07:02:45 2013 +0000
- Revision:
- 0:89073e0112db
- Child:
- 1:0732a5dec208
- Child:
- 3:3804d83c7377
latest version
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 | 0:89073e0112db | 47 | } else if(user == LOGGER) { |
YSB | 0:89073e0112db | 48 | write(CANMessage(GPS_TIME,CANdata[GPS_TIME])); |
YSB | 0:89073e0112db | 49 | write(CANMessage(GPS_LAT_1,CANdata[GPS_LAT_1])); |
YSB | 0:89073e0112db | 50 | write(CANMessage(GPS_LAT_2,CANdata[GPS_LAT_2])); |
YSB | 0:89073e0112db | 51 | write(CANMessage(GPS_LON_1,CANdata[GPS_LON_1])); |
YSB | 0:89073e0112db | 52 | write(CANMessage(GPS_LON_2,CANdata[GPS_LON_2])); |
YSB | 0:89073e0112db | 53 | write(CANMessage(SCP1000_PRES_TEMP,CANdata[SCP1000_PRES_TEMP])); |
YSB | 0:89073e0112db | 54 | write(CANMessage(ADXL345_XYZ,CANdata[ADXL345_XYZ])); |
YSB | 0:89073e0112db | 55 | write(CANMessage(ITG3200_XYZ,CANdata[ITG3200_XYZ])); |
YSB | 0:89073e0112db | 56 | } else {} |
YSB | 0:89073e0112db | 57 | } |
YSB | 0:89073e0112db | 58 | |
YSB | 0:89073e0112db | 59 | void myCAN::rxHandler(void) |
YSB | 0:89073e0112db | 60 | { |
YSB | 0:89073e0112db | 61 | if(read(msg)){ |
YSB | 0:89073e0112db | 62 | for(int i=0; i<8; i++) { |
YSB | 0:89073e0112db | 63 | CANdata[msg.id][i]=msg.data[i]; |
YSB | 0:89073e0112db | 64 | } |
YSB | 0:89073e0112db | 65 | } |
YSB | 0:89073e0112db | 66 | } |
YSB | 0:89073e0112db | 67 | |
YSB | 0:89073e0112db | 68 | void myCAN::make_logger_senddata(char* time,int NoS,char* latitude,char* longitude,int a_z,int temp,int pres) |
YSB | 0:89073e0112db | 69 | { |
YSB | 0:89073e0112db | 70 | for(int i=0; i<8; i++) { |
YSB | 0:89073e0112db | 71 | CANdata[GPS_TIME][i] = time[i]; |
YSB | 0:89073e0112db | 72 | } |
YSB | 0:89073e0112db | 73 | CANdata[GPS_LAT_2][1] = NoS; |
YSB | 0:89073e0112db | 74 | |
YSB | 0:89073e0112db | 75 | for(int i=0; i<8; i++) { |
YSB | 0:89073e0112db | 76 | CANdata[GPS_LAT_1][i] = latitude[i]; |
YSB | 0:89073e0112db | 77 | } |
YSB | 0:89073e0112db | 78 | for(int i=0; i<1; i++) { |
YSB | 0:89073e0112db | 79 | CANdata[GPS_LAT_2][i] = latitude[i+8]; |
YSB | 0:89073e0112db | 80 | } |
YSB | 0:89073e0112db | 81 | |
YSB | 0:89073e0112db | 82 | for(int i=0; i<8; i++) { |
YSB | 0:89073e0112db | 83 | CANdata[GPS_LON_1][i] = longitude[i]; |
YSB | 0:89073e0112db | 84 | } |
YSB | 0:89073e0112db | 85 | for(int i=0; i<2; i++) { |
YSB | 0:89073e0112db | 86 | CANdata[GPS_LON_2][i] = longitude[i+8]; |
YSB | 0:89073e0112db | 87 | } |
YSB | 0:89073e0112db | 88 | |
YSB | 0:89073e0112db | 89 | CANdata[SCP1000_PRES_TEMP][0]=(char)((temp & 0xF0)>>4); |
YSB | 0:89073e0112db | 90 | CANdata[SCP1000_PRES_TEMP][1]=(char)(temp & 0x0F); |
YSB | 0:89073e0112db | 91 | CANdata[SCP1000_PRES_TEMP][2]=(char)((pres & 0xF0)>>4); |
YSB | 0:89073e0112db | 92 | CANdata[SCP1000_PRES_TEMP][3]=(char)(pres & 0x0F); |
YSB | 0:89073e0112db | 93 | |
YSB | 0:89073e0112db | 94 | CANdata[GPS_LAT_2][2] = a_z; |
YSB | 0:89073e0112db | 95 | } |
YSB | 0:89073e0112db | 96 | |
YSB | 0:89073e0112db | 97 | char* myCAN::get_time(void) |
YSB | 0:89073e0112db | 98 | { |
YSB | 0:89073e0112db | 99 | for(int i=0; i<8; i++) { |
YSB | 0:89073e0112db | 100 | time[i] = CANdata[GPS_TIME][i]; |
YSB | 0:89073e0112db | 101 | } |
YSB | 0:89073e0112db | 102 | return time; |
YSB | 0:89073e0112db | 103 | } |
YSB | 0:89073e0112db | 104 | |
YSB | 0:89073e0112db | 105 | char* myCAN::get_latitude(void) |
YSB | 0:89073e0112db | 106 | { |
YSB | 0:89073e0112db | 107 | for(int i=0; i<8; i++) { |
YSB | 0:89073e0112db | 108 | latitude[i] = CANdata[GPS_LAT_1][i]; |
YSB | 0:89073e0112db | 109 | } |
YSB | 0:89073e0112db | 110 | for(int i=0; i<1; i++) { |
YSB | 0:89073e0112db | 111 | latitude[i+8] = CANdata[GPS_LAT_2][i]; |
YSB | 0:89073e0112db | 112 | } |
YSB | 0:89073e0112db | 113 | return latitude; |
YSB | 0:89073e0112db | 114 | } |
YSB | 0:89073e0112db | 115 | |
YSB | 0:89073e0112db | 116 | char* myCAN::get_longitude(void) |
YSB | 0:89073e0112db | 117 | { |
YSB | 0:89073e0112db | 118 | for(int i=0; i<8; i++) { |
YSB | 0:89073e0112db | 119 | longitude[i] = CANdata[GPS_LON_1][i]; |
YSB | 0:89073e0112db | 120 | } |
YSB | 0:89073e0112db | 121 | for(int i=0; i<1; i++) { |
YSB | 0:89073e0112db | 122 | longitude[i+8] = CANdata[GPS_LON_2][i]; |
YSB | 0:89073e0112db | 123 | } |
YSB | 0:89073e0112db | 124 | return longitude; |
YSB | 0:89073e0112db | 125 | } |
YSB | 0:89073e0112db | 126 | |
YSB | 0:89073e0112db | 127 | int myCAN::get_NoS(void) |
YSB | 0:89073e0112db | 128 | { |
YSB | 0:89073e0112db | 129 | NoS = CANdata[GPS_LAT_2][1]; |
YSB | 0:89073e0112db | 130 | return NoS; |
YSB | 0:89073e0112db | 131 | } |
YSB | 0:89073e0112db | 132 | float myCAN::get_a_x(void) |
YSB | 0:89073e0112db | 133 | { |
YSB | 0:89073e0112db | 134 | |
YSB | 0:89073e0112db | 135 | return acel[0]; |
YSB | 0:89073e0112db | 136 | } |
YSB | 0:89073e0112db | 137 | float myCAN::get_a_y(void) |
YSB | 0:89073e0112db | 138 | { |
YSB | 0:89073e0112db | 139 | |
YSB | 0:89073e0112db | 140 | return acel[1]; |
YSB | 0:89073e0112db | 141 | } |
YSB | 0:89073e0112db | 142 | |
YSB | 0:89073e0112db | 143 | int myCAN::get_a_z(void) |
YSB | 0:89073e0112db | 144 | { |
YSB | 0:89073e0112db | 145 | acel[2] = CANdata[GPS_LAT_2][2]; |
YSB | 0:89073e0112db | 146 | return acel[2]; |
YSB | 0:89073e0112db | 147 | } |
YSB | 0:89073e0112db | 148 | |
YSB | 0:89073e0112db | 149 | int myCAN::get_j_x(void) |
YSB | 0:89073e0112db | 150 | { |
YSB | 0:89073e0112db | 151 | |
YSB | 0:89073e0112db | 152 | return jail[0]; |
YSB | 0:89073e0112db | 153 | } |
YSB | 0:89073e0112db | 154 | |
YSB | 0:89073e0112db | 155 | int myCAN::get_j_y(void) |
YSB | 0:89073e0112db | 156 | { |
YSB | 0:89073e0112db | 157 | |
YSB | 0:89073e0112db | 158 | return jail[1]; |
YSB | 0:89073e0112db | 159 | } |
YSB | 0:89073e0112db | 160 | |
YSB | 0:89073e0112db | 161 | int myCAN::get_j_z(void) |
YSB | 0:89073e0112db | 162 | { |
YSB | 0:89073e0112db | 163 | |
YSB | 0:89073e0112db | 164 | return jail[2]; |
YSB | 0:89073e0112db | 165 | } |
YSB | 0:89073e0112db | 166 | |
YSB | 0:89073e0112db | 167 | float myCAN::get_temp(void) |
YSB | 0:89073e0112db | 168 | { |
YSB | 0:89073e0112db | 169 | temp = (float)(((int)(CANdata[SCP1000_PRES_TEMP][0])<<4+(int)(CANdata[SCP1000_PRES_TEMP][1]))/20.0); |
YSB | 0:89073e0112db | 170 | return temp; |
YSB | 0:89073e0112db | 171 | } |
YSB | 0:89073e0112db | 172 | |
YSB | 0:89073e0112db | 173 | int myCAN::get_pres(void) |
YSB | 0:89073e0112db | 174 | { |
YSB | 0:89073e0112db | 175 | pres = (int)(CANdata[SCP1000_PRES_TEMP][2])<<4+(int)(CANdata[SCP1000_PRES_TEMP][3]); |
YSB | 0:89073e0112db | 176 | return pres; |
YSB | 0:89073e0112db | 177 | } |