for CAN communication between mbed
Dependents: EM_Logger EM_Mission
myCAN.cpp@1:0732a5dec208, 2013-08-02 (annotated)
- Committer:
- YSB
- Date:
- Fri Aug 02 10:50:57 2013 +0000
- Revision:
- 1:0732a5dec208
- Parent:
- 0:89073e0112db
- Child:
- 2:e0e7269bf1f0
hyjuk
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 | 1:0732a5dec208 | 49 | wait_ms(5); |
YSB | 0:89073e0112db | 50 | write(CANMessage(GPS_LAT_1,CANdata[GPS_LAT_1])); |
YSB | 1:0732a5dec208 | 51 | wait_ms(5); |
YSB | 0:89073e0112db | 52 | write(CANMessage(GPS_LAT_2,CANdata[GPS_LAT_2])); |
YSB | 1:0732a5dec208 | 53 | wait_ms(5); |
YSB | 0:89073e0112db | 54 | write(CANMessage(GPS_LON_1,CANdata[GPS_LON_1])); |
YSB | 1:0732a5dec208 | 55 | wait_ms(5); |
YSB | 0:89073e0112db | 56 | write(CANMessage(GPS_LON_2,CANdata[GPS_LON_2])); |
YSB | 1:0732a5dec208 | 57 | wait_ms(5); |
YSB | 0:89073e0112db | 58 | write(CANMessage(SCP1000_PRES_TEMP,CANdata[SCP1000_PRES_TEMP])); |
YSB | 1:0732a5dec208 | 59 | wait_ms(5); |
YSB | 0:89073e0112db | 60 | write(CANMessage(ADXL345_XYZ,CANdata[ADXL345_XYZ])); |
YSB | 1:0732a5dec208 | 61 | wait_ms(5); |
YSB | 0:89073e0112db | 62 | write(CANMessage(ITG3200_XYZ,CANdata[ITG3200_XYZ])); |
YSB | 1:0732a5dec208 | 63 | wait_ms(5); |
YSB | 0:89073e0112db | 64 | } else {} |
YSB | 0:89073e0112db | 65 | } |
YSB | 0:89073e0112db | 66 | |
YSB | 0:89073e0112db | 67 | void myCAN::rxHandler(void) |
YSB | 0:89073e0112db | 68 | { |
YSB | 0:89073e0112db | 69 | if(read(msg)){ |
YSB | 0:89073e0112db | 70 | for(int i=0; i<8; i++) { |
YSB | 0:89073e0112db | 71 | CANdata[msg.id][i]=msg.data[i]; |
YSB | 0:89073e0112db | 72 | } |
YSB | 0:89073e0112db | 73 | } |
YSB | 0:89073e0112db | 74 | } |
YSB | 0:89073e0112db | 75 | |
YSB | 1:0732a5dec208 | 76 | void myCAN::make_logger_senddata(char* time,int NoS,char* latitude,char* longitude,int roll,int temp,int pres) |
YSB | 0:89073e0112db | 77 | { |
YSB | 0:89073e0112db | 78 | for(int i=0; i<8; i++) { |
YSB | 0:89073e0112db | 79 | CANdata[GPS_TIME][i] = time[i]; |
YSB | 0:89073e0112db | 80 | } |
YSB | 0:89073e0112db | 81 | CANdata[GPS_LAT_2][1] = NoS; |
YSB | 0:89073e0112db | 82 | |
YSB | 0:89073e0112db | 83 | for(int i=0; i<8; i++) { |
YSB | 0:89073e0112db | 84 | CANdata[GPS_LAT_1][i] = latitude[i]; |
YSB | 0:89073e0112db | 85 | } |
YSB | 0:89073e0112db | 86 | for(int i=0; i<1; i++) { |
YSB | 0:89073e0112db | 87 | CANdata[GPS_LAT_2][i] = latitude[i+8]; |
YSB | 0:89073e0112db | 88 | } |
YSB | 0:89073e0112db | 89 | |
YSB | 0:89073e0112db | 90 | for(int i=0; i<8; i++) { |
YSB | 0:89073e0112db | 91 | CANdata[GPS_LON_1][i] = longitude[i]; |
YSB | 0:89073e0112db | 92 | } |
YSB | 0:89073e0112db | 93 | for(int i=0; i<2; i++) { |
YSB | 0:89073e0112db | 94 | CANdata[GPS_LON_2][i] = longitude[i+8]; |
YSB | 0:89073e0112db | 95 | } |
YSB | 0:89073e0112db | 96 | |
YSB | 1:0732a5dec208 | 97 | //CANdata[SCP1000_PRES_TEMP][0]=(char)((temp & 0xF0)>>4); |
YSB | 1:0732a5dec208 | 98 | //CANdata[SCP1000_PRES_TEMP][1]=(char)(temp & 0x0F); |
YSB | 1:0732a5dec208 | 99 | //CANdata[SCP1000_PRES_TEMP][2]=(char)((pres & 0xF0)>>4); |
YSB | 1:0732a5dec208 | 100 | //CANdata[SCP1000_PRES_TEMP][3]=(char)(pres & 0x0F); |
YSB | 0:89073e0112db | 101 | |
YSB | 1:0732a5dec208 | 102 | CANdata[ADXL345_XYZ][0] = (char)((roll&0xFF00)>>8); |
YSB | 1:0732a5dec208 | 103 | CANdata[ADXL345_XYZ][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 | 1:0732a5dec208 | 143 | hroll = CANdata[ADXL345_XYZ][0]; |
YSB | 1:0732a5dec208 | 144 | lroll = CANdata[ADXL345_XYZ][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 | 0:89073e0112db | 151 | temp = (float)(((int)(CANdata[SCP1000_PRES_TEMP][0])<<4+(int)(CANdata[SCP1000_PRES_TEMP][1]))/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 | 0:89073e0112db | 157 | pres = (int)(CANdata[SCP1000_PRES_TEMP][2])<<4+(int)(CANdata[SCP1000_PRES_TEMP][3]); |
YSB | 0:89073e0112db | 158 | return pres; |
YSB | 0:89073e0112db | 159 | } |