Subversion Repositories HomeAutomation

Rev

Rev 323 | Only display areas with differences | Regard whitespace | Details | Blame | Last modification | View Log | SVN | RSS feed

Rev 323 Rev 350
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 <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
 
54
 
55
#define SERVO_FEED_PORT         PORTC
55
#define SERVO_FEED_PORT         PORTC
56
#define SERVO_FEED_DDR          DDRC
56
#define SERVO_FEED_DDR          DDRC
57
#define SERVO_FEED_PIN          PC0
57
#define SERVO_FEED_PIN          PC0
58
 
58
 
59
#define TRUE 1
59
#define TRUE 1
60
#define FALSE !TRUE
60
#define FALSE !TRUE
61
 
61
 
62
/*-------------------------------------------------------------------------
62
/*-------------------------------------------------------------------------
63
 * Global variables
63
 * Global variables
64
 * -----------------------------------------------------------------------*/
64
 * -----------------------------------------------------------------------*/
65
/*#if defined(TWO_BUTTON_MODE)
65
/*#if defined(TWO_BUTTON_MODE)
66
Can_Message_t txMsg_Button;
66
Can_Message_t txMsg_Button;
67
#endif*/
67
#endif*/
68
 
68
 
69
uint8_t msg_received = FALSE;
69
uint8_t msg_received = FALSE;
70
 
70
 
71
Can_Message_t txMsg;
71
Can_Message_t txMsg;
72
Can_Message_t rxMsg;
72
Can_Message_t rxMsg;
73
 
73
 
74
/*----------------------------------------------------------------------------
74
/*----------------------------------------------------------------------------
75
 * Functions
75
 * Functions
76
 * -------------------------------------------------------------------------*/
76
 * -------------------------------------------------------------------------*/
77
void blindsFailsafe();
77
void blindsFailsafe();
78
 
78
 
79
/* 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 */
80
void can_receive(Can_Message_t *msg){
80
void can_receive(Can_Message_t *msg){
81
    uint32_t act;
81
    uint32_t act;
82
    uint8_t m, act_type, group, node;
82
    uint8_t m, act_type, group, node;
83
    if( ((msg->Id & CLASS_MASK) >> CLASS_MASK_BITS) == CLASS_ACT ){
83
    if( ((msg->Id & CLASS_MASK) >> CLASS_MASK_BITS) == CLASS_ACT ){
84
        act = (msg->Id & TYPE_MASK) >> TYPE_MASK_BITS;
84
        act = (msg->Id & TYPE_MASK) >> TYPE_MASK_BITS;
85
        act_type = (act & ACT_TYPE_MASK) >> ACT_TYPE_BITS;
85
        act_type = (act & ACT_TYPE_MASK) >> ACT_TYPE_BITS;
86
        group = (act & ACT_GROUP_MASK) >> ACT_GROUP_BITS;
86
        group = (act & ACT_GROUP_MASK) >> ACT_GROUP_BITS;
87
        node = (act & ACT_NODE_MASK) >> ACT_NODE_BITS;
87
        node = (act & ACT_NODE_MASK) >> ACT_NODE_BITS;
88
 
88
 
89
        /* Check if it is command to control this servo */
89
        /* Check if it is command to control this servo */
90
        if(act_type == ACT_TYPE_SERVO){
90
        if(act_type == ACT_TYPE_SERVO){
91
            if(group == GROUP_ID || node == NODE_ID){
91
            if(group == GROUP_ID || node == NODE_ID){
92
                for(m=0;m<msg->DataLength;m++){
92
                for(m=0;m<msg->DataLength;m++){
93
                    rxMsg.Data.bytes[m] = msg->Data.bytes[m];
93
                    rxMsg.Data.bytes[m] = msg->Data.bytes[m];
94
                }
94
                }
95
                msg_received = TRUE;
95
                msg_received = TRUE;
96
            }
96
            }
97
        }
97
        }
98
    }
98
    }
99
}
99
}
100
 
100
 
101
/*----------------------------------------------------------------------------
101
/*----------------------------------------------------------------------------
102
 * Interrupt routines
102
 * Interrupt routines
103
 * -------------------------------------------------------------------------*/
103
 * -------------------------------------------------------------------------*/
104
#if defined(TWO_BUTTON_MODE)
104
#if defined(TWO_BUTTON_MODE)
105
#error two button mode is still untested
105
#error two button mode is still untested
106
ISR( PCINT2_vect )
106
ISR( PCINT2_vect )
107
{
107
{
108
    /*
108
    /*
109
     * Auxiliary button pressed and released
109
     * Auxiliary button pressed and released
110
     * Check time between press and release to eliminate bounces
110
     * Check time between press and release to eliminate bounces
111
     */
111
     */
112
    static uint32_t buttonTime[2];
112
    static uint32_t buttonTime[2];
113
 
113
 
114
    txMsg_Button.RemoteFlag = 0;
114
    txMsg_Button.RemoteFlag = 0;
115
    txMsg_Button.ExtendedFlag = 1;
115
    txMsg_Button.ExtendedFlag = 1;
116
 
116
 
117
    if( (PIND & (1<<PD1)) == 0){
117
    if( (PIND & (1<<PD1)) == 0){
118
        /* Button pressed */
118
        /* Button pressed */
119
        buttonTime[0] = Timebase_CurrentTime();
119
        buttonTime[0] = Timebase_CurrentTime();
120
    }else if( (PIND & (1<<PD1)) == 1){
120
    }else if( (PIND & (1<<PD1)) == 1){
121
        if( Timebase_PassedTimeMillis( buttonTime[0] ) >= BOUNCE_TIME ){
121
        if( Timebase_PassedTimeMillis( buttonTime[0] ) >= BOUNCE_TIME ){
122
            /* No bounce, send message */
122
            /* No bounce, send message */
123
            txMsg_Button.Id = BUTTON_ID;
123
            txMsg_Button.Id = BUTTON_ID;
124
            txMsg_Button.DataLength = 1;
124
            txMsg_Button.DataLength = 1;
125
            txMsg_Button.Data.bytes[0] = BUTTON1;
125
            txMsg_Button.Data.bytes[0] = BUTTON1;
126
            return;
126
            return;
127
        }else{
127
        }else{
128
            /* Just bounces, ignore it */
128
            /* Just bounces, ignore it */
129
            return;
129
            return;
130
        }
130
        }
131
    }
131
    }
132
    if( (PIND & (1<<PD2)) == 0){
132
    if( (PIND & (1<<PD2)) == 0){
133
        /* Button pressed */
133
        /* Button pressed */
134
        buttonTime[1] = Timebase_CurrentTime();
134
        buttonTime[1] = Timebase_CurrentTime();
135
    }else if( (PIND & (1<<PD2)) == 1){
135
    }else if( (PIND & (1<<PD2)) == 1){
136
        if( Timebase_PassedTimeMillis( buttonTime[1] ) >= BOUNCE_TIME ){
136
        if( Timebase_PassedTimeMillis( buttonTime[1] ) >= BOUNCE_TIME ){
137
            /* No bounce, send message */
137
            /* No bounce, send message */
138
            txMsg_Button.Id = BUTTON_ID;
138
            txMsg_Button.Id = BUTTON_ID;
139
            txMsg_Button.DataLength = 1;
139
            txMsg_Button.DataLength = 1;
140
            txMsg_Button.Data.bytes[0] = BUTTON2;
140
            txMsg_Button.Data.bytes[0] = BUTTON2;
141
            return;
141
            return;
142
        }else{
142
        }else{
143
            /* Just bounces, ignore it */
143
            /* Just bounces, ignore it */
144
            return;
144
            return;
145
        }
145
        }
146
    }
146
    }
147
}
147
}
148
#endif
148
#endif
149
 
149
 
150
/*-----------------------------------------------------------------------------
150
/*-----------------------------------------------------------------------------
151
 * Main Program
151
 * Main Program
152
 *---------------------------------------------------------------------------*/
152
 *---------------------------------------------------------------------------*/
153
int main(void) {
153
int main(void) {
154
 
154
 
155
    uint8_t //blindsStatus, /* Position (-128 to 127) */
155
    uint8_t //blindsStatus, /* Position (-128 to 127) */
156
            servoToTurn,
156
            servoToTurn,
157
            turnDirection = BLINDS_TURN_STOP; /* Specifies direction to turn or stopped */
157
            turnDirection = BLINDS_TURN_STOP; /* Specifies direction to turn or stopped */
158
    uint32_t boardTemperature = 0;
158
    uint32_t boardTemperature = 0;
159
    uint32_t timeStamp = 0, timeStampTurn = 0, servoFeed_timeStamp = 0;
159
    uint32_t timeStamp = 0, timeStampTurn = 0, servoFeed_timeStamp = 0;
160
 
160
 
161
    sei();
161
    sei();
162
    adcTemperatureInit();
162
    adcTemperatureInit();
163
    rcServoInit();
163
    rcServoInit();
164
 
164
 
165
#if defined(TWO_BUTTON_MODE)
165
#if defined(TWO_BUTTON_MODE)
166
    /*
166
    /*
167
     * Initialize buttons
167
     * Initialize buttons
168
     * It is possible to use ie. sensors instead
168
     * It is possible to use ie. sensors instead
169
     * but then change this
169
     * but then change this
170
     */
170
     */
171
    /* Set as input and enable pull-up */
171
    /* Set as input and enable pull-up */
172
    DDRD &= ~( (1<<DDD1)|(1<<DDD2) );
172
    DDRD &= ~( (1<<DDD1)|(1<<DDD2) );
173
    PORTD |= (1<<PD1)|(1<<PD2);
173
    PORTD |= (1<<PD1)|(1<<PD2);
174
 
174
 
175
    /* Enable IO-pin interrupt */
175
    /* Enable IO-pin interrupt */
176
    PCICR |= (1<<PCIE2);
176
    PCICR |= (1<<PCIE2);
177
    /* Unmask PD1 and SERVO_FEED_PIN */
177
    /* Unmask PD1 and SERVO_FEED_PIN */
178
    PCMSK2 |= (1<<PCINT16) | (1<<PCINT17);
178
    PCMSK2 |= (1<<PCINT16) | (1<<PCINT17);
179
#endif
179
#endif
180
    /* Set output and turn off the servo current feed */
180
    /* Set output and turn off the servo current feed */
181
    SERVO_FEED_DDR |= (1<<SERVO_FEED_PIN);
181
    SERVO_FEED_DDR |= (1<<SERVO_FEED_PIN);
182
    SERVO_FEED_PORT &= ~(1<<SERVO_FEED_PIN);
182
    SERVO_FEED_PORT &= ~(1<<SERVO_FEED_PIN);
183
 
183
 
184
    txMsg.RemoteFlag = 0;
184
    txMsg.RemoteFlag = 0;
185
    txMsg.ExtendedFlag = 1;
185
    txMsg.ExtendedFlag = 1;
186
    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);
187
 
187
 
188
    /* Set up callback for CAN messages */
188
    /* Set up callback for CAN messages */
189
    bios->can_callback = &can_receive;
189
    bios->can_callback = &can_receive;
190
 
190
 
191
    /* main loop */
191
    /* main loop */
192
    while (1) {
192
    while (1) {
193
        /* check if any messages have been received */
193
        /* check if any messages have been received */
194
        if(msg_received){
194
        if(msg_received){
195
            /* This node that control a servo is adressed */
195
            /* This node that control a servo is adressed */
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
                servoFeed_timeStamp = bios->timebase_get();
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
                servoFeed_timeStamp = bios->timebase_get();
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
                servoFeed_timeStamp = bios->timebase_get();
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
            SERVO_FEED_PORT &= ~(1<<SERVO_FEED_PIN);
282
            SERVO_FEED_PORT &= ~(1<<SERVO_FEED_PIN);
283
        }else{
283
        }else{
284
            SERVO_FEED_PORT |= (1<<SERVO_FEED_PIN);
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
    SERVO_FEED_PORT &= ~(1<<SERVO_FEED_PIN);
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