Rev 187 | Only display areas with differences | Regard whitespace | Details | Blame | Last modification | View Log | SVN | RSS feed
| Rev 187 | Rev 190 | ||
|---|---|---|---|
| 1 | /** |
1 | /** |
| 2 | * CAN Relay. |
2 | * CAN Relay. |
| 3 | * |
3 | * |
| 4 | * |
4 | * |
| 5 | * @date 2006-12-17 |
5 | * @date 2006-12-17 |
| 6 | * @author Erik Larsson |
6 | * @author Erik Larsson |
| 7 | * |
7 | * |
| 8 | * Observera! Applikationen är endast testad på labplatta med m88, pot och lysdiod |
8 | * Observera! Applikationen är endast testad på labplatta med m88, pot och lysdiod |
| 9 | * |
9 | * |
| 10 | * ADC=Vin*1024/Vref |
10 | * ADC=Vin*1024/Vref |
| 11 | * Vout=( 10 mV/C )( Temperature C ) + 500 mV |
11 | * Vout=( 10 mV/C )( Temperature C ) + 500 mV |
| 12 | * |
12 | * |
| 13 | * Transmitts: DATA: <temperature low byte> <temperature high byte> <relay status> |
13 | * Transmitts: DATA: <temperature low byte> <temperature high byte> <relay status> <failsafe mode> |
| 14 | * |
- | |
| 15 | * TODO |
- | |
| 16 | * fixa så adcn inte pollas hela tiden, det tar för mycket tid |
- | |
| 17 | * kunna använda Vref = 1.1 |
- | |
| 18 | * |
14 | * |
| 19 | * |
15 | * |
| 20 | */ |
16 | */ |
| 21 | 17 | ||
| 22 | #define DEBUG 0 /* Prints debug messages to uart */ |
18 | #define DEBUG 0 /* Prints debug messages to uart */ |
| 23 | 19 | ||
| 24 | /*----------------------------------------------------------------------------- |
20 | /*----------------------------------------------------------------------------- |
| 25 | * Includes |
21 | * Includes |
| 26 | *---------------------------------------------------------------------------*/ |
22 | *---------------------------------------------------------------------------*/ |
| 27 | /* system files */ |
23 | /* system files */ |
| 28 | #include <avr/io.h> |
24 | #include <avr/io.h> |
| 29 | #include <avr/interrupt.h> |
25 | #include <avr/interrupt.h> |
| 30 | #include <avr/wdt.h> |
26 | #include <avr/wdt.h> |
| 31 | #include <stdio.h> |
27 | #include <stdio.h> |
| 32 | /* lib files */ |
28 | /* lib files */ |
| 33 | #include <can.h> |
29 | #include <can.h> |
| 34 | #include <serial.h> |
30 | #include <serial.h> |
| 35 | #include <timebase.h> |
31 | #include <timebase.h> |
| 36 | 32 | ||
| 37 | #include <tc1047.h> |
33 | #include <tc1047.h> |
| 38 | 34 | ||
| 39 | /* defines */ |
35 | /* defines */ |
| 40 | #define RELAY_OFF 0x01 |
36 | #define RELAY_OFF 0x01 |
| 41 | #define RELAY_ON 0x02 |
37 | #define RELAY_ON 0x02 |
| 42 | #define FAILSAFE_MODE 0x0F |
38 | #define FAILSAFE_MODE 0x0F |
| 43 | #define FAILSAFE_TRESHOLD 60 /* Degrees when nodeRelay goes into failsafe mode */ |
39 | #define FAILSAFE_TRESHOLD 60 /* Degrees when nodeRelay goes into failsafe mode */ |
| 44 | 40 | ||
| 45 | #define RELAY_CMD_GET 0x1200 |
41 | #define RELAY_CMD_GET 0x1200 |
| 46 | #define RELAY_CMD_SEND 0x1201 |
42 | #define RELAY_CMD_SEND 0x1201 |
| 47 | #define RELAY_CMD_ON 0x01 |
43 | #define RELAY_CMD_ON 0x01 |
| 48 | #define RELAY_CMD_OFF 0x02 |
44 | #define RELAY_CMD_OFF 0x02 |
| 49 | #define RELAY_CMD_TOGGLE 0x03 |
45 | #define RELAY_CMD_TOGGLE 0x03 |
| 50 | #define RELAY_CMD_STATUS 0x04 |
46 | #define RELAY_CMD_STATUS 0x04 |
| 51 | #define RELAY_CMD_RESET 0x05 |
47 | #define RELAY_CMD_RESET 0x05 |
| 52 | #define STATUS_SEND_PERIOD 10000 /* milliseconds */ |
48 | #define STATUS_SEND_PERIOD 10000 /* milliseconds */ |
| 53 | #define FAILSAFE_SEND_PERIOD 10000 /* milliseconds */ |
49 | #define FAILSAFE_SEND_PERIOD 10000 /* milliseconds */ |
| 54 | 50 | ||
| 55 | /*---------------------------------------------------------------------------- |
51 | /*---------------------------------------------------------------------------- |
| 56 | * Functions |
52 | * Functions |
| 57 | * -------------------------------------------------------------------------*/ |
53 | * -------------------------------------------------------------------------*/ |
| 58 | uint8_t relayOff(); |
54 | uint8_t relayOff(); |
| 59 | uint8_t relayOn(); |
55 | uint8_t relayOn(); |
| 60 | void relayFailsafe(); |
56 | void relayFailsafe(); |
| 61 | 57 | ||
| 62 | /*----------------------------------------------------------------------------- |
58 | /*----------------------------------------------------------------------------- |
| 63 | * Main Program |
59 | * Main Program |
| 64 | *---------------------------------------------------------------------------*/ |
60 | *---------------------------------------------------------------------------*/ |
| 65 | int main(void) { |
61 | int main(void) { |
| 66 | 62 | ||
| 67 | uint8_t relayStatus; |
63 | uint8_t relayStatus; |
| 68 | uint32_t boardTemperature = 0; |
64 | uint32_t boardTemperature = 0; |
| 69 | 65 | ||
| 70 | adcTemperatureInit(); |
66 | adcTemperatureInit(); |
| 71 | 67 | ||
| 72 | Timebase_Init(); |
68 | Timebase_Init(); |
| 73 | #if DEBUG |
69 | #if DEBUG |
| 74 | Serial_Init(); |
70 | Serial_Init(); |
| 75 | #endif |
71 | #endif |
| 76 | sei(); |
72 | sei(); |
| 77 | 73 | ||
| 78 | /* Turn relay off */ |
74 | /* Turn relay off */ |
| 79 | relayStatus = relayOff(); |
75 | relayStatus = relayOff(); |
| 80 | 76 | ||
| 81 | #if DEBUG |
77 | #if DEBUG |
| 82 | printf("\n------------------------------------------------------------\n"); |
78 | printf("\n------------------------------------------------------------\n"); |
| 83 | printf( " CAN: Relay\n"); |
79 | printf( " CAN: Relay\n"); |
| 84 | printf( "------------------------------------------------------------\n"); |
80 | printf( "------------------------------------------------------------\n"); |
| 85 | 81 | ||
| 86 | 82 | ||
| 87 | printf("CanInit..."); |
83 | printf("CanInit..."); |
| 88 | if (Can_Init() != CAN_OK) { |
84 | if (Can_Init() != CAN_OK) { |
| 89 | printf("FAILED!\n"); |
85 | printf("FAILED!\n"); |
| 90 | }else{ |
86 | }else{ |
| 91 | printf("OK!\n"); |
87 | printf("OK!\n"); |
| 92 | } |
88 | } |
| 93 | #else |
89 | #else |
| 94 | Can_Init(); |
90 | Can_Init(); |
| 95 | #endif |
91 | #endif |
| 96 | uint32_t timeStamp = 0; |
92 | uint32_t timeStamp = 0; |
| 97 | 93 | ||
| 98 | Can_Message_t txMsg; |
94 | Can_Message_t txMsg; |
| 99 | Can_Message_t rxMsg; |
95 | Can_Message_t rxMsg; |
| 100 | txMsg.Id = RELAY_CMD_SEND; |
96 | txMsg.Id = RELAY_CMD_SEND; |
| 101 | txMsg.RemoteFlag = 0; |
97 | txMsg.RemoteFlag = 0; |
| 102 | txMsg.ExtendedFlag = 1; |
98 | txMsg.ExtendedFlag = 1; |
| 103 | txMsg.DataLength = 3; |
99 | txMsg.DataLength = 3; |
| 104 | 100 | ||
| 105 | /* main loop */ |
101 | /* main loop */ |
| 106 | while (1) { |
102 | while (1) { |
| 107 | /* service the CAN routines */ |
103 | /* service the CAN routines */ |
| 108 | Can_Service(); |
104 | Can_Service(); |
| 109 | 105 | ||
| 110 | /* check if any messages have been received */ |
106 | /* check if any messages have been received */ |
| 111 | while (Can_Receive(&rxMsg) == CAN_OK) { |
107 | while (Can_Receive(&rxMsg) == CAN_OK) { |
| 112 | /* This relay is adressed*/ |
108 | /* This relay is adressed*/ |
| 113 | if( rxMsg.Id == RELAY_CMD_GET ){ |
109 | if( rxMsg.Id == RELAY_CMD_GET ){ |
| 114 | 110 | ||
| 115 | if( rxMsg.Data.bytes[0] == RELAY_CMD_ON ){ |
111 | if( rxMsg.Data.bytes[0] == RELAY_CMD_ON ){ |
| 116 | /* Turn relay on */ |
112 | /* Turn relay on */ |
| 117 | relayStatus = relayOn(); |
113 | relayStatus = relayOn(); |
| 118 | 114 | ||
| 119 | }else if(rxMsg.Data.bytes[0] == RELAY_CMD_OFF){ |
115 | }else if(rxMsg.Data.bytes[0] == RELAY_CMD_OFF){ |
| 120 | /* Turn relay off */ |
116 | /* Turn relay off */ |
| 121 | relayStatus = relayOff(); |
117 | relayStatus = relayOff(); |
| 122 | 118 | ||
| 123 | }else if(rxMsg.Data.bytes[0] == RELAY_CMD_TOGGLE ){ |
119 | }else if(rxMsg.Data.bytes[0] == RELAY_CMD_TOGGLE ){ |
| 124 | /* Toggle relay */ |
120 | /* Toggle relay */ |
| 125 | 121 | ||
| 126 | if(relayStatus == RELAY_OFF){ |
122 | if(relayStatus == RELAY_OFF){ |
| 127 | relayStatus = relayOn(); |
123 | relayStatus = relayOn(); |
| 128 | }else{ |
124 | }else{ |
| 129 | relayStatus = relayOff(); |
125 | relayStatus = relayOff(); |
| 130 | } |
126 | } |
| 131 | 127 | ||
| 132 | }else if(rxMsg.Data.bytes[0] == RELAY_CMD_STATUS){ |
128 | }else if(rxMsg.Data.bytes[0] == RELAY_CMD_STATUS){ |
| 133 | /* Send relay status to CAN */ |
129 | /* Send relay status to CAN */ |
| 134 | 130 | ||
| 135 | boardTemperature = getTC1047temperature(); |
131 | boardTemperature = getTC1047temperature(); |
| 136 | 132 | ||
| 137 | if( boardTemperature >= FAILSAFE_TRESHOLD ){ |
133 | if( (int32_t)boardTemperature >= FAILSAFE_TRESHOLD ){ |
| 138 | relayFailsafe(); |
134 | relayFailsafe(); |
| 139 | }else{ |
135 | }else{ |
| 140 | txMsg.Data.bytes[0] = boardTemperature & 0x00FF; |
136 | txMsg.Data.bytes[0] = boardTemperature & 0x00FF; |
| 141 | txMsg.Data.bytes[1] = (boardTemperature & 0xFF00)>>8; |
137 | txMsg.Data.bytes[1] = (boardTemperature & 0xFF00)>>8; |
| 142 | txMsg.Data.bytes[2] = relayStatus; |
138 | txMsg.Data.bytes[2] = relayStatus; |
| 143 | 139 | ||
| 144 | Can_Send( &txMsg ); |
140 | Can_Send( &txMsg ); |
| 145 | } |
141 | } |
| 146 | } |
142 | } |
| 147 | } |
143 | } |
| 148 | } |
144 | } |
| 149 | 145 | ||
| 150 | if( Timebase_PassedTimeMillis(timeStamp) >= STATUS_SEND_PERIOD ){ |
146 | if( Timebase_PassedTimeMillis(timeStamp) >= STATUS_SEND_PERIOD ){ |
| 151 | timeStamp = Timebase_CurrentTime(); |
147 | timeStamp = Timebase_CurrentTime(); |
| 152 | /* Send relay status to CAN */ |
148 | /* Send relay status to CAN */ |
| 153 | 149 | ||
| 154 | boardTemperature = getTC1047temperature(); |
150 | boardTemperature = getTC1047temperature(); |
| 155 | 151 | ||
| 156 | if( boardTemperature >= FAILSAFE_TRESHOLD ){ |
152 | if( (int32_t)boardTemperature >= FAILSAFE_TRESHOLD ){ |
| 157 | relayFailsafe(); |
153 | relayFailsafe(); |
| 158 | }else{ |
154 | }else{ |
| 159 | txMsg.Data.bytes[0] = boardTemperature & 0x00FF; |
155 | txMsg.Data.bytes[0] = boardTemperature & 0x00FF; |
| 160 | txMsg.Data.bytes[1] = (boardTemperature & 0xFF00)>>8; |
156 | txMsg.Data.bytes[1] = (boardTemperature & 0xFF00)>>8; |
| 161 | txMsg.Data.bytes[2] = relayStatus; |
157 | txMsg.Data.bytes[2] = relayStatus; |
| 162 | 158 | ||
| 163 | Can_Send( &txMsg ); |
159 | Can_Send( &txMsg ); |
| 164 | #if DEBUG |
160 | #if DEBUG |
| 165 | printf("Periodic message sent...\n"); |
161 | printf("Periodic message sent...\n"); |
| 166 | printf("Temperature: %d\nRelay status: %u\n", boardTemperature, relayStatus); |
162 | printf("Temperature: %d\nRelay status: %u\n", boardTemperature, relayStatus); |
| 167 | #endif |
163 | #endif |
| 168 | } |
164 | } |
| 169 | } |
165 | } |
| 170 | } |
166 | } |
| 171 | return 0; |
167 | return 0; |
| 172 | } |
168 | } |
| 173 | 169 | ||
| 174 | uint8_t relayOff() |
170 | uint8_t relayOff() |
| 175 | { |
171 | { |
| 176 | /* Set as output and sink */ |
172 | /* Set as output and sink */ |
| 177 | PORTC &= ~(1<<PC1); |
173 | PORTC &= ~(1<<PC1); |
| 178 | DDRC |= (1<<DDC1); |
174 | DDRC |= (1<<DDC1); |
| 179 | 175 | ||
| 180 | #if DEBUG |
176 | #if DEBUG |
| 181 | printf("relay turned off\n"); |
177 | printf("relay turned off\n"); |
| 182 | #endif |
178 | #endif |
| 183 | 179 | ||
| 184 | return RELAY_OFF; |
180 | return RELAY_OFF; |
| 185 | } |
181 | } |
| 186 | 182 | ||
| 187 | uint8_t relayOn() |
183 | uint8_t relayOn() |
| 188 | { |
184 | { |
| 189 | /* Set as input and enable pull-up */ |
185 | /* Set as input and enable pull-up */ |
| 190 | DDRC &= ~(1<<DDC1); |
186 | DDRC &= ~(1<<DDC1); |
| 191 | PORTC |= (1<<PC1); |
187 | PORTC |= (1<<PC1); |
| 192 | 188 | ||
| 193 | #if DEBUG |
189 | #if DEBUG |
| 194 | printf("relay turned on\n"); |
190 | printf("relay turned on\n"); |
| 195 | #endif |
191 | #endif |
| 196 | 192 | ||
| 197 | return RELAY_ON; |
193 | return RELAY_ON; |
| 198 | } |
194 | } |
| 199 | 195 | ||
| 200 | void relayFailsafe() |
196 | void relayFailsafe() |
| 201 | { |
197 | { |
| 202 | uint8_t relayStatus; |
198 | uint8_t relayStatus; |
| 203 | uint32_t boardTemperature = 0, timeStamp = 0; |
199 | uint32_t boardTemperature = 0, timeStamp = 0; |
| 204 | 200 | ||
| 205 | Can_Message_t txMsg; |
201 | Can_Message_t txMsg; |
| 206 | Can_Message_t rxMsg; |
202 | Can_Message_t rxMsg; |
| 207 | txMsg.Id = RELAY_CMD_SEND; |
203 | txMsg.Id = RELAY_CMD_SEND; |
| 208 | txMsg.RemoteFlag = 0; |
204 | txMsg.RemoteFlag = 0; |
| 209 | txMsg.ExtendedFlag = 1; |
205 | txMsg.ExtendedFlag = 1; |
| 210 | txMsg.DataLength = 4; |
206 | txMsg.DataLength = 4; |
| 211 | 207 | ||
| 212 | relayStatus = relayOff(); |
208 | relayStatus = relayOff(); |
| 213 | 209 | ||
| 214 | while(1){ |
210 | while(1){ |
| 215 | /* service the CAN routines */ |
211 | /* service the CAN routines */ |
| 216 | Can_Service(); |
212 | Can_Service(); |
| 217 | 213 | ||
| 218 | /* check if any messages have been received */ |
214 | /* check if any messages have been received */ |
| 219 | while (Can_Receive(&rxMsg) == CAN_OK) { |
215 | while (Can_Receive(&rxMsg) == CAN_OK) { |
| 220 | /* This relay is adressed*/ |
216 | /* This relay is adressed*/ |
| 221 | if( rxMsg.Id == RELAY_CMD_GET ){ |
217 | if( rxMsg.Id == RELAY_CMD_GET ){ |
| 222 | if( rxMsg.Data.bytes[0] == RELAY_CMD_RESET ){ |
218 | if( rxMsg.Data.bytes[0] == RELAY_CMD_RESET ){ |
| 223 | /* Return from failsafe mode */ |
219 | /* Return from failsafe mode */ |
| 224 | return 0; |
220 | return 0; |
| 225 | } |
221 | } |
| 226 | } |
222 | } |
| 227 | } |
223 | } |
| 228 | 224 | ||
| 229 | if( Timebase_PassedTimeMillis(timeStamp) >= FAILSAFE_SEND_PERIOD ){ |
225 | if( Timebase_PassedTimeMillis(timeStamp) >= FAILSAFE_SEND_PERIOD ){ |
| 230 | timeStamp = Timebase_CurrentTime(); |
226 | timeStamp = Timebase_CurrentTime(); |
| 231 | /* Send relay status to CAN */ |
227 | /* Send relay status to CAN */ |
| - | 228 | #if DEBUG |
|
| - | 229 | printf("Failsafe mode!\n"); |
|
| 232 | 230 | #endif |
|
| 233 | boardTemperature = getTC1047temperature(); |
231 | boardTemperature = getTC1047temperature(); |
| 234 | 232 | ||
| 235 | txMsg.Data.bytes[0] = boardTemperature & 0x00FF; |
233 | txMsg.Data.bytes[0] = boardTemperature & 0x00FF; |
| 236 | txMsg.Data.bytes[1] = (boardTemperature & 0xFF00)>>8; |
234 | txMsg.Data.bytes[1] = (boardTemperature & 0xFF00)>>8; |
| 237 | txMsg.Data.bytes[2] = relayStatus; |
235 | txMsg.Data.bytes[2] = relayStatus; |
| 238 | txMsg.Data.bytes[3] = FAILSAFE_MODE; |
236 | txMsg.Data.bytes[3] = FAILSAFE_MODE; |
| 239 | 237 | ||
| 240 | Can_Send( &txMsg ); |
238 | Can_Send( &txMsg ); |
| 241 | } |
239 | } |
| 242 | } |
240 | } |
| 243 | } |
241 | } |
| 244 | 242 | ||
| 245 | 243 | ||