Subversion Repositories HomeAutomation

Rev

Rev 246 | Rev 317 | Go to most recent revision | Show entire file | Regard whitespace | Details | Blame | Last modification | View Log | SVN | RSS feed

Rev 246 Rev 249
Line 4... Line 4...
4
 * Input: -128 to +127
4
 * Input: -128 to +127
5
 * Position msg: <byte0 BLINDS_CMD> <byte1 position>
5
 * Position msg: <byte0 BLINDS_CMD> <byte1 position> <byte2 servo number>
6
 * Turning msg: <byte0 START/STOP> <byte 1 direction>
6
 * Turning msg: <byte0 START/STOP> <byte 1 direction> <byte2 servo number>
7
 *
7
 *
8
 * @date    2001-01-01
8
 * @date    2001-01-01
9
 * @author  Erik Larsson
9
 * @author  Erik Larsson
-
 
10
 *
-
 
11
 * TODO fixa en ordentlig failsafe som kan bryta matningen till servona
10
 */
12
 */
11
 
13
 
12
#define DEBUG           0 /* Prints debug messages to uart */
-
 
13
#define TWO_BUTTON_MODE 0 /* If using two (or just one but nothing more) auxiliary buttons */
14
#define TWO_BUTTON_MODE 0 /* If using two (or just one but nothing more) auxiliary buttons */
14
 
15
 
15
/*-----------------------------------------------------------------------------
16
/*-----------------------------------------------------------------------------
16
 * Includes
17
 * Includes
17
 *---------------------------------------------------------------------------*/
18
 *---------------------------------------------------------------------------*/
Line 20... Line 21...
20
#include <avr/interrupt.h>
21
#include <avr/interrupt.h>
21
#include <avr/wdt.h>
22
#include <avr/wdt.h>
22
#include <stdio.h>
23
#include <stdio.h>
23
/* lib files */
24
/* lib files */
24
#include <can.h>
25
#include <can.h>
25
#if DEBUG
-
 
26
#include <serial.h>
-
 
27
#endif
-
 
28
#include <timebase.h>
26
#include <timebase.h>
29
 
27
 
30
#include <tc1047.h>
28
#include <tc1047.h>
31
#include <rc_servo.h>
29
#include <rc_servo.h>
32
 
30
 
Line 72... Line 70...
72
/*----------------------------------------------------------------------------
70
/*----------------------------------------------------------------------------
73
 * Interrupt routines
71
 * Interrupt routines
74
 * -------------------------------------------------------------------------*/
72
 * -------------------------------------------------------------------------*/
75
#if TWO_BUTTON_MODE
73
#if TWO_BUTTON_MODE
76
ISR( PCINT2_vect )
74
ISR( PCINT2_vect )
77
{
75
{
78
    /*
76
    /*
79
     * Auxiliary button pressed and released
77
     * Auxiliary button pressed and released
80
     * Check time between press and release to eliminate bounces
78
     * Check time between press and release to eliminate bounces
81
     */
79
     */
82
    static uint32_t buttonTime[2];
80
    static uint32_t buttonTime[2];
83
 
81
 
84
    txMsg_Button.RemoteFlag = 0;
82
    txMsg_Button.RemoteFlag = 0;
85
    txMsg_Button.ExtendedFlag = 1;
83
    txMsg_Button.ExtendedFlag = 1;
86
 
84
 
87
    if( (PIND & (1<<PD6)) == 0){
85
    if( (PIND & (1<<PD6)) == 0){
88
        /* Button pressed */
86
        /* Button pressed */
89
        buttonTime[0] = Timebase_CurrentTime();
87
        buttonTime[0] = Timebase_CurrentTime();
90
#if DEBUG
-
 
91
        printf("Button 1 pressed\n");
-
 
92
#endif
-
 
93
    }else if( (PIND & (1<<PD6)) == 1){
88
    }else if( (PIND & (1<<PD6)) == 1){
94
        if( Timebase_PassedTimeMillis( buttonTime[0] ) >= BOUNCE_TIME ){
89
        if( Timebase_PassedTimeMillis( buttonTime[0] ) >= BOUNCE_TIME ){
95
            /* No bounce, send message */
90
            /* No bounce, send message */
96
            txMsg_Button.Id = BUTTON_ID;
91
            txMsg_Button.Id = BUTTON_ID;
97
            txMsg_Button.DataLength = 1;
92
            txMsg_Button.DataLength = 1;
98
            txMsg_Button.Data.bytes[0] = BUTTON1;
93
            txMsg_Button.Data.bytes[0] = BUTTON1;
99
#if DEBUG
-
 
100
            printf("Button 1 pressed: message sent\n");
-
 
101
#endif
-
 
102
            return;
94
            return;
103
        }else{
95
        }else{
104
            /* Just bounces, ignore it */
96
            /* Just bounces, ignore it */
105
#if DEBUG
-
 
106
            printf("Button 1 just bounced\n");
-
 
107
#endif
-
 
108
            return;
97
            return;
109
        }
98
        }
110
    }
99
    }
111
    if( (PIND & (1<<PD7)) == 0){
100
    if( (PIND & (1<<PD7)) == 0){
112
        /* Button pressed */
101
        /* Button pressed */
113
        buttonTime[1] = Timebase_CurrentTime();
102
        buttonTime[1] = Timebase_CurrentTime();
114
#if DEBUG
-
 
115
        printf("Button 2 pressed\n");
-
 
116
#endif
-
 
117
    }else if( (PIND & (1<<PD7)) == 1){
103
    }else if( (PIND & (1<<PD7)) == 1){
118
        if( Timebase_PassedTimeMillis( buttonTime[1] ) >= BOUNCE_TIME ){
104
        if( Timebase_PassedTimeMillis( buttonTime[1] ) >= BOUNCE_TIME ){
119
            /* No bounce, send message */
105
            /* No bounce, send message */
120
            txMsg_Button.Id = BUTTON_ID;
106
            txMsg_Button.Id = BUTTON_ID;
121
            txMsg_Button.DataLength = 1;
107
            txMsg_Button.DataLength = 1;
122
            txMsg_Button.Data.bytes[0] = BUTTON2;
108
            txMsg_Button.Data.bytes[0] = BUTTON2;
123
#if DEBUG
-
 
124
            printf("Button 2 pressed: message sent\n");
-
 
125
#endif
-
 
126
            return;
109
            return;
127
        }else{
110
        }else{
128
            /* Just bounces, ignore it */
111
            /* Just bounces, ignore it */
129
#if DEBUG
-
 
130
            printf("Button 2 just bounced\n");
-
 
131
#endif
-
 
132
            return;
112
            return;
133
        }
113
        }
134
    }
114
    }
135
}
115
}
136
#endif
116
#endif
137
 
117
 
138
/*-----------------------------------------------------------------------------
118
/*-----------------------------------------------------------------------------
139
 * Main Program
119
 * Main Program
140
 *---------------------------------------------------------------------------*/
120
 *---------------------------------------------------------------------------*/
141
int main(void) {
121
int main(void) {
142
 
122
 
143
    uint8_t blindsStatus, /* Position (-128 to 127) */
123
    uint8_t blindsStatus, /* Position (-128 to 127) */
-
 
124
            servoToTurn,
144
            turnDirection = BLINDS_TURN_STOP; /* Specifies direction to turn or stopped */
125
            turnDirection = BLINDS_TURN_STOP; /* Specifies direction to turn or stopped */
145
    uint32_t boardTemperature = 0;
126
    uint32_t boardTemperature = 0;
146
    uint32_t timeStamp = 0, timeStampTurn = 0;
127
    uint32_t timeStamp = 0, timeStampTurn = 0;
147
 
128
 
148
    adcTemperatureInit();
129
    adcTemperatureInit();
149
    Timebase_Init();
130
    Timebase_Init();
150
    rcServoInit();
131
    rcServoInit();
151
#if DEBUG
-
 
152
    Serial_Init();
-
 
153
#endif
-
 
154
 
132
 
155
#if TWO_BUTTON_MODE
133
#if TWO_BUTTON_MODE
156
    /*
134
    /*
157
     * Initialize buttons
135
     * Initialize buttons
158
     * It is possible to use ie. sensors instead
136
     * It is possible to use ie. sensors instead
Line 166... Line 144...
166
    PCICR |= (1<<PCIE1);
144
    PCICR |= (1<<PCIE1);
167
    /* Unmask PD6 and PD7 */
145
    /* Unmask PD6 and PD7 */
168
    PCMSK2 |= (1<<PCINT22) | (1<<PCINT23);
146
    PCMSK2 |= (1<<PCINT22) | (1<<PCINT23);
169
#endif
147
#endif
170
    sei();
148
    sei();
171
#if DEBUG
-
 
172
    printf("\n------------------------------------------------------------\n");
-
 
173
    printf(  "   CAN: Blinds\n");
-
 
174
    printf(  "------------------------------------------------------------\n");
-
 
175
 
-
 
176
    printf("CanInit...");
-
 
177
    if (Can_Init() != CAN_OK) {
-
 
178
        printf("FAILED!\n");
-
 
179
    }else{
-
 
180
        printf("OK!\n");
-
 
181
    }
-
 
182
#else
-
 
183
    Can_Init();
149
    Can_Init();
184
#endif
-
 
185
 
150
 
186
    Can_Message_t txMsg;
151
    Can_Message_t txMsg;
187
    Can_Message_t rxMsg;
152
    Can_Message_t rxMsg;
188
    txMsg.Id = BLINDS_CMD_SEND;
153
    txMsg.Id = BLINDS_CMD_SEND;
189
    txMsg.RemoteFlag = 0;
154
    txMsg.RemoteFlag = 0;
Line 200... Line 165...
200
            /* This node that control a servo is adressed */
165
            /* This node that control a servo is adressed */
201
            if( rxMsg.Id == BLINDS_CMD_GET ){
166
            if( rxMsg.Id == BLINDS_CMD_GET ){
202
 
167
 
203
                if( rxMsg.Data.bytes[0] == BLINDS_CMD_ABS ){
168
                if( rxMsg.Data.bytes[0] == BLINDS_CMD_ABS ){
204
                    /* Set absolute position to servo */
169
                    /* Set absolute position to servo */
205
                    setPosition( rxMsg.Data.bytes[1] );
170
                    setPosition( rxMsg.Data.bytes[1], rxMsg.Data.bytes[2] );
206
 
171
 
207
                }else if(rxMsg.Data.bytes[0] == BLINDS_CMD_REL){
172
                }else if(rxMsg.Data.bytes[0] == BLINDS_CMD_REL){
208
                    /* Set relative position to servo */
173
                    /* Set relative position to servo */
209
                    alterPosition( rxMsg.Data.bytes[1] );
174
                    alterPosition( rxMsg.Data.bytes[1], rxMsg.Data.bytes[2] );
210
 
175
 
211
                }else if(rxMsg.Data.bytes[0] == BLINDS_CMD_START ){
176
                }else if(rxMsg.Data.bytes[0] == BLINDS_CMD_START ){
212
                    /* Start turning the servo */
177
                    /* Start turning the servo */
213
                    turnDirection = rxMsg.Data.bytes[1];
178
                    turnDirection = rxMsg.Data.bytes[1];
-
 
179
                    servoToTurn = rxMsg.Data.bytes[2];
214
 
180
 
215
                }else if(rxMsg.Data.bytes[0] == BLINDS_CMD_STOP ){
181
                }else if(rxMsg.Data.bytes[0] == BLINDS_CMD_STOP ){
216
                    /* Stop turning */
182
                    /* Stop turning */
217
                    turnDirection = BLINDS_TURN_STOP;
183
                    turnDirection = BLINDS_TURN_STOP;
218
 
184
 
Line 223... Line 189...
223
                    if( (int32_t)boardTemperature >= FAILSAFE_TRESHOLD ){
189
                    if( (int32_t)boardTemperature >= FAILSAFE_TRESHOLD ){
224
                        blindsFailsafe();
190
                        blindsFailsafe();
225
                    }else{
191
                    }else{
226
                        txMsg.Data.bytes[0] = boardTemperature & 0x00FF;
192
                        txMsg.Data.bytes[0] = boardTemperature & 0x00FF;
227
                        txMsg.Data.bytes[1] = (boardTemperature & 0xFF00)>>8;
193
                        txMsg.Data.bytes[1] = (boardTemperature & 0xFF00)>>8;
-
 
194
                        txMsg.Data.bytes[2] = 0x00;
-
 
195
#if NUMBER_OF_RCS >0
228
                        txMsg.Data.bytes[2] = getPosition();
196
                        txMsg.Data.bytes[3] = getPosition(1);
-
 
197
#endif
-
 
198
#if NUMBER_OF_RCS >1
-
 
199
                        txMsg.Data.bytes[4] = getPosition(2);
-
 
200
#endif
-
 
201
#if NUMBER_OF_RCS >2
-
 
202
                        txMsg.Data.bytes[5] = getPosition(3);
229
 
203
#endif
-
 
204
                        txMsg.DataLength = NUMBER_OF_RCS+3;
230
                        Can_Send( &txMsg );
205
                        Can_Send( &txMsg );
231
                    }
206
                    }
232
                }
207
                }
233
            }
208
            }
234
        }
209
        }
235
 
210
 
236
        if( Timebase_PassedTimeMillis(timeStamp) >= STATUS_SEND_PERIOD ){
211
        if( Timebase_PassedTimeMillis(timeStamp) >= STATUS_SEND_PERIOD ){
237
            timeStamp = Timebase_CurrentTime();
212
            timeStamp = Timebase_CurrentTime();
238
            /* Send blinds status to CAN */
213
            /* Send blinds status to CAN */
239
 
214
 
240
            boardTemperature = getTC1047temperature();
215
            boardTemperature = getTC1047temperature();
241
 
216
 
242
            if( (int32_t)boardTemperature >= FAILSAFE_TRESHOLD ){
217
            if( (int32_t)boardTemperature >= FAILSAFE_TRESHOLD ){
243
                blindsFailsafe();
218
                blindsFailsafe();
244
            }else{
219
            }else{
245
                txMsg.Data.bytes[0] = boardTemperature & 0x00FF;
220
                txMsg.Data.bytes[0] = boardTemperature & 0x00FF;
246
                txMsg.Data.bytes[1] = (boardTemperature & 0xFF00)>>8;
221
                txMsg.Data.bytes[1] = (boardTemperature & 0xFF00)>>8;
247
                txMsg.Data.bytes[2] = getPosition();
222
                txMsg.Data.bytes[2] = 0x00;
248
 
-
 
249
                Can_Send( &txMsg );
-
 
250
#if DEBUG
223
#if NUMBER_OF_RCS >0
251
                printf("Periodic message sent...\n");
224
                txMsg.Data.bytes[3] = getPosition(1);
252
                printf("Temperature: %d\nBlinds status: %u\n", boardTemperature, getPosition());
-
 
253
#endif
225
#endif
-
 
226
#if NUMBER_OF_RCS >1
-
 
227
                txMsg.Data.bytes[4] = getPosition(2);
-
 
228
#endif
-
 
229
#if NUMBER_OF_RCS >2
-
 
230
                txMsg.Data.bytes[5] = getPosition(3);
-
 
231
#endif
-
 
232
                txMsg.DataLength = NUMBER_OF_RCS+3;
-
 
233
 
-
 
234
                Can_Send( &txMsg );
254
            }
235
            }
255
        }
236
        }
256
        /* If start turning servo */
237
        /* If start turning servo */
257
        if( turnDirection ){
238
        if( turnDirection ){
258
            if( Timebase_PassedTimeMillis( timeStampTurn ) >= TURN_PERIOD ){
239
            if( Timebase_PassedTimeMillis( timeStampTurn ) >= TURN_PERIOD ){
259
                timeStampTurn = Timebase_CurrentTime();
240
                timeStampTurn = Timebase_CurrentTime();
260
                /* Clockwise or counterclockwise? */
241
                /* Clockwise or counterclockwise? */
261
                if( turnDirection == BLINDS_TURN_LEFT ){
242
                if( turnDirection == BLINDS_TURN_LEFT ){
262
                    alterPosition( -STEPS_PER_TURN_PERIOD );
243
                    alterPosition( -STEPS_PER_TURN_PERIOD , servoToTurn);
263
                }else if( turnDirection == BLINDS_TURN_RIGHT ){
244
                }else if( turnDirection == BLINDS_TURN_RIGHT ){
264
                    alterPosition( STEPS_PER_TURN_PERIOD );
245
                    alterPosition( STEPS_PER_TURN_PERIOD , servoToTurn);
265
                }
246
                }
266
            }
247
            }
267
        }
248
        }
268
    }
249
    }
269
    return 0;
250
    return 0;
270
}
251
}
271
 
252
 
272
void blindsFailsafe() // TODO fixa failsafe, vad ska hända med PWM?
253
void blindsFailsafe() // TODO fixa failsafe, vad ska hända med PWM?
273
{
254
{
274
    uint8_t relayStatus;
255
    uint8_t relayStatus;
275
    uint32_t boardTemperature = 0, timeStamp = 0;
256
    uint32_t boardTemperature = 0, timeStamp = 0;
276
 
257
 
277
    Can_Message_t txMsg;
258
    Can_Message_t txMsg;
278
    Can_Message_t rxMsg;
259
    Can_Message_t rxMsg;
279
    txMsg.Id = BLINDS_CMD_SEND;
260
    txMsg.Id = BLINDS_CMD_SEND;
280
    txMsg.RemoteFlag = 0;
261
    txMsg.RemoteFlag = 0;
281
    txMsg.ExtendedFlag = 1;
262
    txMsg.ExtendedFlag = 1;
282
    txMsg.DataLength = 4;
263
    txMsg.DataLength = 4;
283
 
264
 
284
    while(1){
265
    while(1){
285
        /* service the CAN routines */
266
        /* service the CAN routines */
286
        Can_Service();
267
        Can_Service();
287
 
268
 
288
        /* check if any messages have been received */
269
        /* check if any messages have been received */
289
        while (Can_Receive(&rxMsg) == CAN_OK) {
270
        while (Can_Receive(&rxMsg) == CAN_OK) {
290
            /* This relay is adressed*/
271
            /* This relay is adressed*/
291
            if( rxMsg.Id == BLINDS_CMD_GET ){
272
            if( rxMsg.Id == BLINDS_CMD_GET ){
292
                if( rxMsg.Data.bytes[0] == BLINDS_CMD_RESET ){
273
                if( rxMsg.Data.bytes[0] == BLINDS_CMD_RESET ){
293
                    /* Return from failsafe mode */
274
                    /* Return from failsafe mode */
294
                    return;
275
                    return;
295
                }
276
                }
296
            }
277
            }
297
        }
278
        }
298
 
279
 
299
        if( Timebase_PassedTimeMillis(timeStamp) >= FAILSAFE_SEND_PERIOD ){
280
        if( Timebase_PassedTimeMillis(timeStamp) >= FAILSAFE_SEND_PERIOD ){
300
            timeStamp = Timebase_CurrentTime();
281
            timeStamp = Timebase_CurrentTime();
301
            /* Send relay status to CAN */
282
            /* Send relay status to CAN */
302
#if DEBUG
-
 
303
            printf("Failsafe mode!\n");
-
 
304
#endif
-
 
305
            boardTemperature = getTC1047temperature();
283
            boardTemperature = getTC1047temperature();
306
 
284
 
307
            txMsg.Data.bytes[0] = boardTemperature & 0x00FF;
285
            txMsg.Data.bytes[0] = boardTemperature & 0x00FF;
308
            txMsg.Data.bytes[1] = (boardTemperature & 0xFF00)>>8;
286
            txMsg.Data.bytes[1] = (boardTemperature & 0xFF00)>>8;
-
 
287
            txMsg.Data.bytes[2] = FAILSAFE_MODE;
-
 
288
#if NUMBER_OF_RCS >0
309
            txMsg.Data.bytes[2] = getPosition();
289
            txMsg.Data.bytes[3] = getPosition(1);
-
 
290
#endif
-
 
291
#if NUMBER_OF_RCS >1
-
 
292
            txMsg.Data.bytes[4] = getPosition(2);
-
 
293
#endif
-
 
294
#if NUMBER_OF_RCS >2
310
            txMsg.Data.bytes[3] = FAILSAFE_MODE;
295
            txMsg.Data.bytes[5] = getPosition(3);
-
 
296
#endif
-
 
297
            txMsg.DataLength = NUMBER_OF_RCS+3;
311
 
298
 
312
            Can_Send( &txMsg );
299
            Can_Send( &txMsg );
313
        }
300
        }
314
    }
301
    }
315
}
302
}