Rev 322 | Only display areas with differences | Regard whitespace | Details | Blame | Last modification | View Log | SVN | RSS feed
| Rev 322 | Rev 323 | ||
|---|---|---|---|
| 1 | /** |
1 | /** |
| 2 | * CanBlinds. |
2 | * CanBlinds. |
| 3 | * Takes incomming packages and adjusts PWM output. |
3 | * Takes incomming packages and adjusts PWM output. |
| 4 | * Input: -128 to +127 |
4 | * Input: -128 to +127 |
| 5 | * Position msg: <byte0 BLINDS_CMD> <byte1 position> <byte2 servo number> |
5 | * Position msg: <byte0 BLINDS_CMD> <byte1 position> <byte2 servo number> |
| 6 | * Turning msg: <byte0 START/STOP> <byte 1 direction> <byte2 servo number> |
6 | * Turning msg: <byte0 START/STOP> <byte 1 direction> <byte2 servo number> |
| 7 | * |
7 | * |
| 8 | * @date 2007-01-01 |
8 | * @date 2007-01-01 |
| 9 | * @author Erik Larsson |
9 | * @author Erik Larsson |
| 10 | * |
10 | * |
| 11 | */ |
11 | */ |
| 12 | 12 | ||
| 13 | //#define TWO_BUTTON_MODE /* If using two (or just one but nothing more) auxiliary buttons */ |
13 | //#define TWO_BUTTON_MODE /* If using two (or just one but nothing more) auxiliary buttons */ |
| 14 | 14 | ||
| 15 | /*----------------------------------------------------------------------------- |
15 | /*----------------------------------------------------------------------------- |
| 16 | * Includes |
16 | * Includes |
| 17 | *---------------------------------------------------------------------------*/ |
17 | *---------------------------------------------------------------------------*/ |
| 18 | /* system files */ |
18 | /* system files */ |
| 19 | #include <avr/io.h> |
19 | #include <avr/io.h> |
| 20 | #include <avr/interrupt.h> |
20 | #include <avr/interrupt.h> |
| 21 | #include <stdio.h> |
21 | #include <stdio.h> |
| 22 | /* lib files */ |
22 | /* lib files */ |
| 23 | #include <bios.h> |
23 | #include <bios.h> |
| 24 | #include <tc1047.h> |
24 | #include <tc1047.h> |
| 25 | #include <rc_servo.h> |
25 | #include <rc_servo.h> |
| 26 | 26 | ||
| 27 | #include <eqlazer_funcdefs.h> |
27 | #include <eqlazer_funcdefs.h> |
| 28 | 28 | ||
| 29 | /* defines */ |
29 | /* defines */ |
| 30 | #define GROUP_ID 0x01L // FIXME |
30 | #define GROUP_ID 0x01L // FIXME |
| 31 | 31 | ||
| 32 | #define FAILSAFE_MODE 0x0F |
32 | #define FAILSAFE_MODE 0x0F |
| 33 | #define FAILSAFE_TRESHOLD 60 /* Degrees celcius when nodeBlinds goes into failsafe mode */ |
33 | #define FAILSAFE_TRESHOLD 60 /* Degrees celcius when nodeBlinds goes into failsafe mode */ |
| 34 | 34 | ||
| 35 | #define BLINDS_CMD_ABS 0x01 /* Absolute position */ |
35 | #define BLINDS_CMD_ABS 0x01 /* Absolute position */ |
| 36 | #define BLINDS_CMD_REL 0x02 /* Relative position */ |
36 | #define BLINDS_CMD_REL 0x02 /* Relative position */ |
| 37 | #define BLINDS_CMD_START 0x03 /* Start turning RC-servo */ |
37 | #define BLINDS_CMD_START 0x03 /* Start turning RC-servo */ |
| 38 | #define BLINDS_CMD_STOP 0x04 /* Stop turning RC-servo */ |
38 | #define BLINDS_CMD_STOP 0x04 /* Stop turning RC-servo */ |
| 39 | #define BLINDS_CMD_RESET 0x05 |
39 | #define BLINDS_CMD_RESET 0x05 |
| 40 | #define BLINDS_CMD_STATUS 0x06 |
40 | #define BLINDS_CMD_STATUS 0x06 |
| 41 | 41 | ||
| 42 | #define BLINDS_TURN_STOP 0x00 |
42 | #define BLINDS_TURN_STOP 0x00 |
| 43 | #define BLINDS_TURN_LEFT 0x01 |
43 | #define BLINDS_TURN_LEFT 0x01 |
| 44 | #define BLINDS_TURN_RIGHT 0x02 |
44 | #define BLINDS_TURN_RIGHT 0x02 |
| 45 | 45 | ||
| 46 | #define STEPS_PER_TURN_PERIOD 10 /* number of steps for each adjustment */ |
46 | #define STEPS_PER_TURN_PERIOD 10 /* number of steps for each adjustment */ |
| 47 | #define TURN_PERIOD 100 /* ms */ |
47 | #define TURN_PERIOD 100 /* ms */ |
| 48 | #define STATUS_SEND_PERIOD 5000 /* ms */ |
48 | #define STATUS_SEND_PERIOD 5000 /* ms */ |
| 49 | #define FAILSAFE_SEND_PERIOD 10000 /* ms */ |
49 | #define FAILSAFE_SEND_PERIOD 10000 /* ms */ |
| 50 | #define BOUNCE_TIME 10 /* ms */ |
50 | #define BOUNCE_TIME 10 /* ms */ |
| 51 | #define SERVO_FEED_TIME 2000 /* ms */ |
51 | #define SERVO_FEED_TIME 2000 /* ms */ |
| 52 | #define BUTTON1 1 |
52 | #define BUTTON1 1 |
| 53 | #define BUTTON2 2 |
53 | #define BUTTON2 2 |
| - | 54 | ||
| - | 55 | #define SERVO_FEED_PORT PORTC |
|
| - | 56 | #define SERVO_FEED_DDR DDRC |
|
| - | 57 | #define SERVO_FEED_PIN PC0 |
|
| 54 | 58 | ||
| 55 | #define TRUE 1 |
59 | #define TRUE 1 |
| 56 | #define FALSE !TRUE |
60 | #define FALSE !TRUE |
| 57 | 61 | ||
| 58 | /*------------------------------------------------------------------------- |
62 | /*------------------------------------------------------------------------- |
| 59 | * Global variables |
63 | * Global variables |
| 60 | * -----------------------------------------------------------------------*/ |
64 | * -----------------------------------------------------------------------*/ |
| 61 | /*#if defined(TWO_BUTTON_MODE) |
65 | /*#if defined(TWO_BUTTON_MODE) |
| 62 | Can_Message_t txMsg_Button; |
66 | Can_Message_t txMsg_Button; |
| 63 | #endif*/ |
67 | #endif*/ |
| 64 | 68 | ||
| 65 | uint8_t msg_received = FALSE; |
69 | uint8_t msg_received = FALSE; |
| 66 | 70 | ||
| 67 | Can_Message_t txMsg; |
71 | Can_Message_t txMsg; |
| 68 | Can_Message_t rxMsg; |
72 | Can_Message_t rxMsg; |
| 69 | 73 | ||
| 70 | /*---------------------------------------------------------------------------- |
74 | /*---------------------------------------------------------------------------- |
| 71 | * Functions |
75 | * Functions |
| 72 | * -------------------------------------------------------------------------*/ |
76 | * -------------------------------------------------------------------------*/ |
| 73 | void blindsFailsafe(); |
77 | void blindsFailsafe(); |
| 74 | 78 | ||
| 75 | /* Takes care about incoming messages and parses them if this node is adressed */ |
79 | /* Takes care about incoming messages and parses them if this node is adressed */ |
| 76 | void can_receive(Can_Message_t *msg){ |
80 | void can_receive(Can_Message_t *msg){ |
| 77 | uint32_t act; |
81 | uint32_t act; |
| 78 | uint8_t m, act_type, group, node; |
82 | uint8_t m, act_type, group, node; |
| 79 | if( ((msg->Id & CLASS_MASK) >> CLASS_MASK_BITS) == CLASS_ACT ){ |
83 | if( ((msg->Id & CLASS_MASK) >> CLASS_MASK_BITS) == CLASS_ACT ){ |
| 80 | /* Parse message */ |
- | |
| 81 | act = (msg->Id & TYPE_MASK) >> TYPE_MASK_BITS; |
84 | act = (msg->Id & TYPE_MASK) >> TYPE_MASK_BITS; |
| 82 | act_type = (act & ACT_TYPE_MASK) >> ACT_TYPE_BITS; |
85 | act_type = (act & ACT_TYPE_MASK) >> ACT_TYPE_BITS; |
| 83 | group = (act & ACT_GROUP_MASK) >> ACT_GROUP_BITS; |
86 | group = (act & ACT_GROUP_MASK) >> ACT_GROUP_BITS; |
| 84 | node = (act & ACT_NODE_MASK) >> ACT_NODE_BITS; |
87 | node = (act & ACT_NODE_MASK) >> ACT_NODE_BITS; |
| 85 | 88 | ||
| 86 | /* Check if it is command to control this servo */ |
89 | /* Check if it is command to control this servo */ |
| 87 | if(act_type == ACT_TYPE_SERVO){ |
90 | if(act_type == ACT_TYPE_SERVO){ |
| 88 | if(group == GROUP_ID || node == NODE_ID){ |
91 | if(group == GROUP_ID || node == NODE_ID){ |
| 89 | for(m=0;m<msg->DataLength;m++){ |
92 | for(m=0;m<msg->DataLength;m++){ |
| 90 | rxMsg.Data.bytes[m] = msg->Data.bytes[m]; |
93 | rxMsg.Data.bytes[m] = msg->Data.bytes[m]; |
| 91 | } |
94 | } |
| 92 | msg_received = TRUE; |
95 | msg_received = TRUE; |
| 93 | } |
96 | } |
| 94 | } |
97 | } |
| 95 | } |
98 | } |
| 96 | } |
99 | } |
| 97 | 100 | ||
| 98 | /*---------------------------------------------------------------------------- |
101 | /*---------------------------------------------------------------------------- |
| 99 | * Interrupt routines |
102 | * Interrupt routines |
| 100 | * -------------------------------------------------------------------------*/ |
103 | * -------------------------------------------------------------------------*/ |
| 101 | #if defined(TWO_BUTTON_MODE) |
104 | #if defined(TWO_BUTTON_MODE) |
| 102 | #error two button mode is still untested |
105 | #error two button mode is still untested |
| 103 | ISR( PCINT2_vect ) |
106 | ISR( PCINT2_vect ) |
| 104 | { |
107 | { |
| 105 | /* |
108 | /* |
| 106 | * Auxiliary button pressed and released |
109 | * Auxiliary button pressed and released |
| 107 | * Check time between press and release to eliminate bounces |
110 | * Check time between press and release to eliminate bounces |
| 108 | */ |
111 | */ |
| 109 | static uint32_t buttonTime[2]; |
112 | static uint32_t buttonTime[2]; |
| 110 | 113 | ||
| 111 | txMsg_Button.RemoteFlag = 0; |
114 | txMsg_Button.RemoteFlag = 0; |
| 112 | txMsg_Button.ExtendedFlag = 1; |
115 | txMsg_Button.ExtendedFlag = 1; |
| 113 | 116 | ||
| 114 | if( (PIND & (1<<PD1)) == 0){ |
117 | if( (PIND & (1<<PD1)) == 0){ |
| 115 | /* Button pressed */ |
118 | /* Button pressed */ |
| 116 | buttonTime[0] = Timebase_CurrentTime(); |
119 | buttonTime[0] = Timebase_CurrentTime(); |
| 117 | }else if( (PIND & (1<<PD1)) == 1){ |
120 | }else if( (PIND & (1<<PD1)) == 1){ |
| 118 | if( Timebase_PassedTimeMillis( buttonTime[0] ) >= BOUNCE_TIME ){ |
121 | if( Timebase_PassedTimeMillis( buttonTime[0] ) >= BOUNCE_TIME ){ |
| 119 | /* No bounce, send message */ |
122 | /* No bounce, send message */ |
| 120 | txMsg_Button.Id = BUTTON_ID; |
123 | txMsg_Button.Id = BUTTON_ID; |
| 121 | txMsg_Button.DataLength = 1; |
124 | txMsg_Button.DataLength = 1; |
| 122 | txMsg_Button.Data.bytes[0] = BUTTON1; |
125 | txMsg_Button.Data.bytes[0] = BUTTON1; |
| 123 | return; |
126 | return; |
| 124 | }else{ |
127 | }else{ |
| 125 | /* Just bounces, ignore it */ |
128 | /* Just bounces, ignore it */ |
| 126 | return; |
129 | return; |
| 127 | } |
130 | } |
| 128 | } |
131 | } |
| 129 | if( (PIND & (1<<PD2)) == 0){ |
132 | if( (PIND & (1<<PD2)) == 0){ |
| 130 | /* Button pressed */ |
133 | /* Button pressed */ |
| 131 | buttonTime[1] = Timebase_CurrentTime(); |
134 | buttonTime[1] = Timebase_CurrentTime(); |
| 132 | }else if( (PIND & (1<<PD2)) == 1){ |
135 | }else if( (PIND & (1<<PD2)) == 1){ |
| 133 | if( Timebase_PassedTimeMillis( buttonTime[1] ) >= BOUNCE_TIME ){ |
136 | if( Timebase_PassedTimeMillis( buttonTime[1] ) >= BOUNCE_TIME ){ |
| 134 | /* No bounce, send message */ |
137 | /* No bounce, send message */ |
| 135 | txMsg_Button.Id = BUTTON_ID; |
138 | txMsg_Button.Id = BUTTON_ID; |
| 136 | txMsg_Button.DataLength = 1; |
139 | txMsg_Button.DataLength = 1; |
| 137 | txMsg_Button.Data.bytes[0] = BUTTON2; |
140 | txMsg_Button.Data.bytes[0] = BUTTON2; |
| 138 | return; |
141 | return; |
| 139 | }else{ |
142 | }else{ |
| 140 | /* Just bounces, ignore it */ |
143 | /* Just bounces, ignore it */ |
| 141 | return; |
144 | return; |
| 142 | } |
145 | } |
| 143 | } |
146 | } |
| 144 | } |
147 | } |
| 145 | #endif |
148 | #endif |
| 146 | 149 | ||
| 147 | /*----------------------------------------------------------------------------- |
150 | /*----------------------------------------------------------------------------- |
| 148 | * Main Program |
151 | * Main Program |
| 149 | *---------------------------------------------------------------------------*/ |
152 | *---------------------------------------------------------------------------*/ |
| 150 | int main(void) { |
153 | int main(void) { |
| 151 | 154 | ||
| 152 | uint8_t //blindsStatus, /* Position (-128 to 127) */ |
155 | uint8_t //blindsStatus, /* Position (-128 to 127) */ |
| 153 | servoToTurn, |
156 | servoToTurn, |
| 154 | turnDirection = BLINDS_TURN_STOP; /* Specifies direction to turn or stopped */ |
157 | turnDirection = BLINDS_TURN_STOP; /* Specifies direction to turn or stopped */ |
| 155 | uint32_t boardTemperature = 0; |
158 | uint32_t boardTemperature = 0; |
| 156 | uint32_t timeStamp = 0, timeStampTurn = 0, servoFeed_timeStamp = 0; |
159 | uint32_t timeStamp = 0, timeStampTurn = 0, servoFeed_timeStamp = 0; |
| 157 | 160 | ||
| 158 | sei(); |
161 | sei(); |
| 159 | adcTemperatureInit(); |
162 | adcTemperatureInit(); |
| 160 | rcServoInit(); |
163 | rcServoInit(); |
| 161 | 164 | ||
| 162 | #if defined(TWO_BUTTON_MODE) |
165 | #if defined(TWO_BUTTON_MODE) |
| 163 | /* |
166 | /* |
| 164 | * Initialize buttons |
167 | * Initialize buttons |
| 165 | * It is possible to use ie. sensors instead |
168 | * It is possible to use ie. sensors instead |
| 166 | * but then change this |
169 | * but then change this |
| 167 | */ |
170 | */ |
| 168 | /* Set as input and enable pull-up */ |
171 | /* Set as input and enable pull-up */ |
| 169 | DDRD &= ~( (1<<DDD1)|(1<<DDD2) ); |
172 | DDRD &= ~( (1<<DDD1)|(1<<DDD2) ); |
| 170 | PORTD |= (1<<PD1)|(1<<PD2); |
173 | PORTD |= (1<<PD1)|(1<<PD2); |
| 171 | 174 | ||
| 172 | /* Enable IO-pin interrupt */ |
175 | /* Enable IO-pin interrupt */ |
| 173 | PCICR |= (1<<PCIE2); |
176 | PCICR |= (1<<PCIE2); |
| 174 | /* Unmask PD1 and |
177 | /* Unmask PD1 and SERVO_FEED_PIN */ |
| 175 | PCMSK2 |= (1<<PCINT16) | (1<<PCINT17); |
178 | PCMSK2 |= (1<<PCINT16) | (1<<PCINT17); |
| 176 | #endif |
179 | #endif |
| 177 | /* Set output and turn off the servo current feed */ |
180 | /* Set output and turn off the servo current feed */ |
| 178 |
|
181 | SERVO_FEED_DDR |= (1<<SERVO_FEED_PIN); |
| 179 |
|
182 | SERVO_FEED_PORT &= ~(1<<SERVO_FEED_PIN); |
| 180 | - | ||
| 181 | // sei(); |
- | |
| 182 | 183 | ||
| 183 | txMsg.RemoteFlag = 0; |
184 | txMsg.RemoteFlag = 0; |
| 184 | txMsg.ExtendedFlag = 1; |
185 | txMsg.ExtendedFlag = 1; |
| 185 | txMsg.Id = (CLASS_SNS << CLASS_MASK_BITS)|(SNS_ACT_STATUS_SERVO << TYPE_MASK_BITS)|(NODE_ID << SID_MASK_BITS); |
186 | txMsg.Id = (CLASS_SNS << CLASS_MASK_BITS)|(SNS_ACT_STATUS_SERVO << TYPE_MASK_BITS)|(NODE_ID << SID_MASK_BITS); |
| 186 | 187 | ||
| 187 | /* Set up callback for CAN messages */ |
188 | /* Set up callback for CAN messages */ |
| 188 | bios->can_callback = &can_receive; |
189 | bios->can_callback = &can_receive; |
| 189 | 190 | ||
| 190 | /* main loop */ |
191 | /* main loop */ |
| 191 | while (1) { |
192 | while (1) { |
| 192 | /* check if any messages have been received */ |
193 | /* check if any messages have been received */ |
| 193 | if(msg_received){ |
194 | if(msg_received){ |
| 194 | /* This node that control a servo is adressed */ |
195 | /* This node that control a servo is adressed */ |
| 195 | - | ||
| 196 | if( rxMsg.Data.bytes[0] == BLINDS_CMD_ABS ){ |
196 | if( rxMsg.Data.bytes[0] == BLINDS_CMD_ABS ){ |
| 197 | /* Set absolute position to servo */ |
197 | /* Set absolute position to servo */ |
| 198 | setPosition( rxMsg.Data.bytes[1], rxMsg.Data.bytes[2] ); |
198 | setPosition( rxMsg.Data.bytes[1], rxMsg.Data.bytes[2] ); |
| 199 |
|
199 | servoFeed_timeStamp = bios->timebase_get(); |
| 200 | 200 | ||
| 201 | }else if(rxMsg.Data.bytes[0] == BLINDS_CMD_REL){ |
201 | }else if(rxMsg.Data.bytes[0] == BLINDS_CMD_REL){ |
| 202 | /* Set relative position to servo */ |
202 | /* Set relative position to servo */ |
| 203 | alterPosition( rxMsg.Data.bytes[1], rxMsg.Data.bytes[2] ); |
203 | alterPosition( rxMsg.Data.bytes[1], rxMsg.Data.bytes[2] ); |
| 204 |
|
204 | servoFeed_timeStamp = bios->timebase_get(); |
| 205 | 205 | ||
| 206 | }else if(rxMsg.Data.bytes[0] == BLINDS_CMD_START ){ |
206 | }else if(rxMsg.Data.bytes[0] == BLINDS_CMD_START ){ |
| 207 | /* Start turning the servo */ |
207 | /* Start turning the servo */ |
| 208 | turnDirection = rxMsg.Data.bytes[1]; |
208 | turnDirection = rxMsg.Data.bytes[1]; |
| 209 | servoToTurn = rxMsg.Data.bytes[2]; |
209 | servoToTurn = rxMsg.Data.bytes[2]; |
| 210 |
|
210 | servoFeed_timeStamp = bios->timebase_get(); |
| 211 | 211 | ||
| 212 | }else if(rxMsg.Data.bytes[0] == BLINDS_CMD_STOP ){ |
212 | }else if(rxMsg.Data.bytes[0] == BLINDS_CMD_STOP ){ |
| 213 | /* Stop turning */ |
213 | /* Stop turning */ |
| 214 | turnDirection = BLINDS_TURN_STOP; |
214 | turnDirection = BLINDS_TURN_STOP; |
| 215 | 215 | ||
| 216 | }else if(rxMsg.Data.bytes[0] == BLINDS_CMD_STATUS){ |
216 | }else if(rxMsg.Data.bytes[0] == BLINDS_CMD_STATUS){ |
| 217 | /* Send blinds status to CAN */ |
217 | /* Send blinds status to CAN */ |
| 218 | boardTemperature = getTC1047temperature(); |
218 | boardTemperature = getTC1047temperature(); |
| 219 | 219 | ||
| 220 | if( (int32_t)boardTemperature >= FAILSAFE_TRESHOLD ){ |
220 | if( (int32_t)boardTemperature >= FAILSAFE_TRESHOLD ){ |
| 221 | blindsFailsafe(); |
221 | blindsFailsafe(); |
| 222 | }else{ |
222 | }else{ |
| 223 | txMsg.Data.bytes[0] = boardTemperature & 0x00FF; |
223 | txMsg.Data.bytes[0] = boardTemperature & 0x00FF; |
| 224 | txMsg.Data.bytes[1] = (boardTemperature & 0xFF00)>>8; |
224 | txMsg.Data.bytes[1] = (boardTemperature & 0xFF00)>>8; |
| 225 | txMsg.Data.bytes[2] = 0x00; |
225 | txMsg.Data.bytes[2] = 0x00; |
| 226 | #if NUMBER_OF_RCS >0 |
226 | #if NUMBER_OF_RCS >0 |
| 227 | txMsg.Data.bytes[3] = getPosition(1); |
227 | txMsg.Data.bytes[3] = getPosition(1); |
| 228 | #endif |
228 | #endif |
| 229 | #if NUMBER_OF_RCS >1 |
229 | #if NUMBER_OF_RCS >1 |
| 230 | txMsg.Data.bytes[4] = getPosition(2); |
230 | txMsg.Data.bytes[4] = getPosition(2); |
| 231 | #endif |
231 | #endif |
| 232 | #if NUMBER_OF_RCS >2 |
232 | #if NUMBER_OF_RCS >2 |
| 233 | txMsg.Data.bytes[5] = getPosition(3); |
233 | txMsg.Data.bytes[5] = getPosition(3); |
| 234 | #endif |
234 | #endif |
| 235 | txMsg.DataLength = NUMBER_OF_RCS+3; |
235 | txMsg.DataLength = NUMBER_OF_RCS+3; |
| 236 | bios->can_send(&txMsg); |
236 | bios->can_send(&txMsg); |
| 237 | } |
237 | } |
| 238 | } |
238 | } |
| 239 | msg_received = FALSE; |
239 | msg_received = FALSE; |
| 240 | } |
240 | } |
| 241 | 241 | ||
| 242 | if( (bios->timebase_get() - timeStamp) >= STATUS_SEND_PERIOD ){ |
242 | if( (bios->timebase_get() - timeStamp) >= STATUS_SEND_PERIOD ){ |
| 243 | timeStamp = bios->timebase_get(); |
243 | timeStamp = bios->timebase_get(); |
| 244 | /* Send blinds status to CAN */ |
244 | /* Send blinds status to CAN */ |
| 245 | 245 | ||
| 246 | boardTemperature = getTC1047temperature(); |
246 | boardTemperature = getTC1047temperature(); |
| 247 | 247 | ||
| 248 | if( (int32_t)boardTemperature >= FAILSAFE_TRESHOLD ){ |
248 | if( (int32_t)boardTemperature >= FAILSAFE_TRESHOLD ){ |
| 249 | blindsFailsafe(); |
249 | blindsFailsafe(); |
| 250 | }else{ |
250 | }else{ |
| 251 | txMsg.Data.bytes[0] = boardTemperature & 0x00FF; |
251 | txMsg.Data.bytes[0] = boardTemperature & 0x00FF; |
| 252 | txMsg.Data.bytes[1] = (boardTemperature & 0xFF00)>>8; |
252 | txMsg.Data.bytes[1] = (boardTemperature & 0xFF00)>>8; |
| 253 | txMsg.Data.bytes[2] = 0x00; |
253 | txMsg.Data.bytes[2] = 0x00; |
| 254 | #if NUMBER_OF_RCS >0 |
254 | #if NUMBER_OF_RCS >0 |
| 255 | txMsg.Data.bytes[3] = getPosition(1); |
255 | txMsg.Data.bytes[3] = getPosition(1); |
| 256 | #endif |
256 | #endif |
| 257 | #if NUMBER_OF_RCS >1 |
257 | #if NUMBER_OF_RCS >1 |
| 258 | txMsg.Data.bytes[4] = getPosition(2); |
258 | txMsg.Data.bytes[4] = getPosition(2); |
| 259 | #endif |
259 | #endif |
| 260 | #if NUMBER_OF_RCS >2 |
260 | #if NUMBER_OF_RCS >2 |
| 261 | txMsg.Data.bytes[5] = getPosition(3); |
261 | txMsg.Data.bytes[5] = getPosition(3); |
| 262 | #endif |
262 | #endif |
| 263 | txMsg.DataLength = NUMBER_OF_RCS+3; |
263 | txMsg.DataLength = NUMBER_OF_RCS+3; |
| 264 | 264 | ||
| 265 | bios->can_send(&txMsg); |
265 | bios->can_send(&txMsg); |
| 266 | } |
266 | } |
| 267 | } |
267 | } |
| 268 | /* If start turning servo */ |
268 | /* If start turning servo */ |
| 269 | if( turnDirection ){ |
269 | if( turnDirection ){ |
| 270 | if( bios->timebase_get() - timeStampTurn >= TURN_PERIOD ){ |
270 | if( bios->timebase_get() - timeStampTurn >= TURN_PERIOD ){ |
| 271 | timeStampTurn = bios->timebase_get(); |
271 | timeStampTurn = bios->timebase_get(); |
| 272 | /* Clockwise or counterclockwise? */ |
272 | /* Clockwise or counterclockwise? */ |
| 273 | if( turnDirection == BLINDS_TURN_LEFT ){ |
273 | if( turnDirection == BLINDS_TURN_LEFT ){ |
| 274 | alterPosition( -STEPS_PER_TURN_PERIOD , servoToTurn); |
274 | alterPosition( -STEPS_PER_TURN_PERIOD , servoToTurn); |
| 275 | }else if( turnDirection == BLINDS_TURN_RIGHT ){ |
275 | }else if( turnDirection == BLINDS_TURN_RIGHT ){ |
| 276 | alterPosition( STEPS_PER_TURN_PERIOD , servoToTurn); |
276 | alterPosition( STEPS_PER_TURN_PERIOD , servoToTurn); |
| 277 | } |
277 | } |
| 278 | } |
278 | } |
| 279 | } |
279 | } |
| 280 | /* Turn off the servo current feed */ |
280 | /* Turn off the servo current feed */ |
| 281 | if( bios->timebase_get() - servoFeed_timeStamp >= SERVO_FEED_TIME ){ |
281 | if( bios->timebase_get() - servoFeed_timeStamp >= SERVO_FEED_TIME ){ |
| 282 |
|
282 | SERVO_FEED_PORT &= ~(1<<SERVO_FEED_PIN); |
| 283 | }else{ |
283 | }else{ |
| 284 |
|
284 | SERVO_FEED_PORT |= (1<<SERVO_FEED_PIN); |
| 285 | } |
285 | } |
| 286 | } |
286 | } |
| 287 | return 0; |
287 | return 0; |
| 288 | } |
288 | } |
| 289 | 289 | ||
| 290 | void blindsFailsafe() |
290 | void blindsFailsafe() |
| 291 | { |
291 | { |
| 292 | uint8_t relayStatus; |
292 | uint8_t relayStatus; |
| 293 | uint32_t boardTemperature = 0, timeStamp = 0; |
293 | uint32_t boardTemperature = 0, timeStamp = 0; |
| 294 | 294 | ||
| 295 | /* Turn of servo current feed */ |
295 | /* Turn of servo current feed */ |
| 296 |
|
296 | SERVO_FEED_PORT &= ~(1<<SERVO_FEED_PIN); |
| 297 | 297 | ||
| 298 | 298 | ||
| 299 | while(1){ |
299 | while(1){ |
| 300 | /* check if any messages have been received */ |
300 | /* check if any messages have been received */ |
| 301 | while(msg_received) { |
301 | while(msg_received) { |
| 302 | /* This relay is adressed*/ |
302 | /* This relay is adressed*/ |
| 303 | if(rxMsg.Data.bytes[0] == BLINDS_CMD_RESET ){ |
303 | if(rxMsg.Data.bytes[0] == BLINDS_CMD_RESET ){ |
| 304 | /* Return from failsafe mode */ |
304 | /* Return from failsafe mode */ |
| 305 | return; |
305 | return; |
| 306 | } |
306 | } |
| 307 | } |
307 | } |
| 308 | 308 | ||
| 309 | if( bios->timebase_get() - timeStamp >= FAILSAFE_SEND_PERIOD ){ |
309 | if( bios->timebase_get() - timeStamp >= FAILSAFE_SEND_PERIOD ){ |
| 310 | timeStamp = bios->timebase_get(); |
310 | timeStamp = bios->timebase_get(); |
| 311 | /* Send relay status to CAN */ |
311 | /* Send relay status to CAN */ |
| 312 | boardTemperature = getTC1047temperature(); |
312 | boardTemperature = getTC1047temperature(); |
| 313 | 313 | ||
| 314 | txMsg.Data.bytes[0] = boardTemperature & 0x00FF; |
314 | txMsg.Data.bytes[0] = boardTemperature & 0x00FF; |
| 315 | txMsg.Data.bytes[1] = (boardTemperature & 0xFF00)>>8; |
315 | txMsg.Data.bytes[1] = (boardTemperature & 0xFF00)>>8; |
| 316 | txMsg.Data.bytes[2] = FAILSAFE_MODE; |
316 | txMsg.Data.bytes[2] = FAILSAFE_MODE; |
| 317 | #if NUMBER_OF_RCS >0 |
317 | #if NUMBER_OF_RCS >0 |
| 318 | txMsg.Data.bytes[3] = getPosition(1); |
318 | txMsg.Data.bytes[3] = getPosition(1); |
| 319 | #endif |
319 | #endif |
| 320 | #if NUMBER_OF_RCS >1 |
320 | #if NUMBER_OF_RCS >1 |
| 321 | txMsg.Data.bytes[4] = getPosition(2); |
321 | txMsg.Data.bytes[4] = getPosition(2); |
| 322 | #endif |
322 | #endif |
| 323 | #if NUMBER_OF_RCS >2 |
323 | #if NUMBER_OF_RCS >2 |
| 324 | txMsg.Data.bytes[5] = getPosition(3); |
324 | txMsg.Data.bytes[5] = getPosition(3); |
| 325 | #endif |
325 | #endif |
| 326 | txMsg.DataLength = NUMBER_OF_RCS+3; |
326 | txMsg.DataLength = NUMBER_OF_RCS+3; |
| 327 | 327 | ||
| 328 | bios->can_send(&txMsg); |
328 | bios->can_send(&txMsg); |
| 329 | } |
329 | } |
| 330 | } |
330 | } |
| 331 | } |
331 | } |
| 332 | 332 | ||
| 333 | 333 | ||