Subversion Repositories HomeAutomation

Rev

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

Rev 496 Rev 501
Line 7... Line 7...
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
 
-
 
13
//#define TWO_BUTTON_MODE /* If using two (or just one but nothing more) auxiliary buttons */
-
 
14
 
12
 
15
/*-----------------------------------------------------------------------------
13
/*-----------------------------------------------------------------------------
16
 * Includes
14
 * Includes
17
 *---------------------------------------------------------------------------*/
15
 *---------------------------------------------------------------------------*/
18
/* system files */
16
/* system files */
19
#include <avr/io.h>
17
#include <avr/io.h>
20
#include <avr/interrupt.h>
18
#include <avr/interrupt.h>
21
#include <inttypes.h>
19
#include <inttypes.h>
22
#include <stdio.h>
20
#include <stdio.h>
-
 
21
 
23
/* lib files */
22
/* lib files */
24
#include <bios.h>
23
#include <bios.h>
25
#include <config.h>
24
#include <config.h>
-
 
25
#include <settings.h>
-
 
26
 
26
#include <drivers/timer/timebase.h>
27
#include <drivers/timer/timebase.h>
27
 
-
 
28
//#include <funcdefs.h>
28
#include <drivers/sensor/tc1047/tc1047.h>
-
 
29
#include <drivers/actuator/rc-servo/rc_servo.h>
29
 
30
 
-
 
31
/*-----------------------------------------------------------------------------
30
/* defines */
32
 * Defines
31
#define SERVOID_1 0x1
-
 
32
#define SERVOID_2 0x2
-
 
33
#define SERVOID_3 0x3
-
 
34
 
-
 
35
//#define GROUP_ID  0x01L // FIXME
33
 *---------------------------------------------------------------------------*/
36
 
-
 
37
#define FAILSAFE_MODE       0x0F
34
#define FAILSAFE_MODE       0x0F
38
#define FAILSAFE_TRESHOLD   50  // Degrees celcius when nodeBlinds goes into failsafe mode
-
 
39
 
-
 
40
#define BLINDS_CMD_ABS      0x01 // Absolute position
35
#define BLINDS_CMD_ABS      0x01 // Absolute position
41
#define BLINDS_CMD_REL      0x02 // Relative position
36
#define BLINDS_CMD_REL      0x02 // Relative position
42
#define BLINDS_CMD_START    0x03 // Start turning RC-servo
37
#define BLINDS_CMD_START    0x03 // Start turning RC-servo
43
#define BLINDS_CMD_STOP     0x04 // Stop turning RC-servo
38
#define BLINDS_CMD_STOP     0x04 // Stop turning RC-servo
44
#define BLINDS_CMD_RESET    0x05
39
#define BLINDS_CMD_RESET    0x05
45
#define BLINDS_CMD_STATUS   0x06
40
#define BLINDS_CMD_STATUS   0x06
46
 
41
 
47
#define BLINDS_TURN_STOP    0x00
42
#define BLINDS_TURN_STOP    0x00
48
#define BLINDS_TURN_LEFT    0x01
43
#define BLINDS_TURN_CCW     0x01
49
#define BLINDS_TURN_RIGHT   0x02
44
#define BLINDS_TURN_CW      0x02
50
 
45
 
51
#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
52
#define TURN_PERIOD             100 // ms
47
#define TURN_PERIOD             100 // ms
53
#define STATUS_SEND_PERIOD      5000 // ms
48
#define STATUS_SEND_PERIOD      5000 // ms
54
#define FAILSAFE_SEND_PERIOD    10000 // ms
49
#define FAILSAFE_SEND_PERIOD    10000 // ms
55
#define BOUNCE_TIME             10 // ms
50
#define BOUNCE_TIME             10 // ms
56
#define SERVO_FEED_TIME         5000 // ms
-
 
57
#define BUTTON1                 1
51
#define BUTTON1                 1
58
#define BUTTON2                 2
52
#define BUTTON2                 2
59
 
53
 
60
#define SERVO_FEED_PORT         PORTD
54
#define SERVO_FEED_PORT         PORTD
61
#define SERVO_FEED_DDR          DDRD
55
#define SERVO_FEED_DDR          DDRD
Line 77... Line 71...
77
volatile Can_Message_t rxMsg;
71
volatile Can_Message_t rxMsg;
78
 
72
 
79
/*----------------------------------------------------------------------------
73
/*----------------------------------------------------------------------------
80
 * Functions
74
 * Functions
81
 * -------------------------------------------------------------------------*/
75
 * -------------------------------------------------------------------------*/
82
void blindsFailsafe();
76
void blindsFailsafe(void);
83
 
77
 
84
void can_receive(Can_Message_t *msg){
78
void can_receive(Can_Message_t *msg){
85
// CAN callback function
79
// CAN callback function
86
    if(!rxMsgFull){
80
    if(!rxMsgFull){
87
        memcpy((void*)&rxMsg, msg, sizeof(rxMsg));
81
        memcpy((void*)&rxMsg, msg, sizeof(rxMsg));
Line 139... Line 133...
139
 
133
 
140
/*-----------------------------------------------------------------------------
134
/*-----------------------------------------------------------------------------
141
 * Main Program
135
 * Main Program
142
 *---------------------------------------------------------------------------*/
136
 *---------------------------------------------------------------------------*/
143
int main(void) {
137
int main(void) {
144
    uint32_t act;
-
 
145
    int8_t m;
138
    uint8_t temp_id;
146
 
-
 
147
    uint8_t acttype, temp_id;
139
    uint16_t acttype, servoid;
148
    uint16_t servoid;
-
 
149
 
140
 
150
    uint8_t //blindsStatus, /* Position (-128 to 127) */
141
    uint8_t //blindsStatus, /* Position (-128 to 127) */
151
            servoToTurn,
142
            servoToTurn,
152
            turnDirection = BLINDS_TURN_STOP; /* Specifies direction to turn or stopped */
143
            turnDirection = BLINDS_TURN_STOP; /* Specifies direction to turn or stopped */
153
    uint32_t boardTemperature = 0;
144
    uint32_t boardTemperature = 0;
Line 210... Line 201...
210
                        temp_id = 1; // First servo
201
                        temp_id = 1; // First servo
211
                    }else if(servoid == SERVOID_2){
202
                    }else if(servoid == SERVOID_2){
212
                        temp_id = 2; // and so on
203
                        temp_id = 2; // and so on
213
                    }else if(servoid == SERVOID_3){
204
                    }else if(servoid == SERVOID_3){
214
                        temp_id = 3;
205
                        temp_id = 3;
-
 
206
                    }else{
-
 
207
                        temp_id = 0;
215
                    }
208
                    }
216
 
209
 
217
                    if(rxMsg.DataLength == 1 || (rxMsg.DataLength == 2 && rxMsg.Data.bytes[1] == BLINDS_CMD_ABS)){
210
                    if(rxMsg.DataLength == 1 || (rxMsg.DataLength == 2 && rxMsg.Data.bytes[1] == BLINDS_CMD_ABS)){
218
                        // Set absolute position to servo
211
                        // Set absolute position to servo
219
 
212
 
220
                        setPosition( rxMsg.Data.bytes[0], temp_id );
213
                        setPosition( rxMsg.Data.bytes[0], temp_id );
221
                        servoFeed_timeStamp = Timebase_CurrentTime();
214
                        servoFeed_timeStamp = Timebase_CurrentTime();
222
 
215
 
223
                    }else if(rxMsg.DataLength == 2){
216
                    }else if(rxMsg.DataLength == 2){
224
                        if(rxMsg.Data.bytes[1] == BLINDS_CMD_REL){
217
                        if(rxMsg.Data.bytes[1] == BLINDS_CMD_REL){
225
                            // Set relative position to servo
218
                            // Set relative position to servo
226
 
219
 
227
                            alterPosition( rxMsg.Data.bytes[0], temp_id );
220
                            alterPosition( rxMsg.Data.bytes[0], temp_id );
228
                            servoFeed_timeStamp = Timebase_CurrentTime();
221
                            servoFeed_timeStamp = Timebase_CurrentTime();
229
 
222
 
230
                        }else if(rxMsg.Data.bytes[1] == BLINDS_CMD_START ){
223
                        }else if(rxMsg.Data.bytes[1] == BLINDS_CMD_START ){
231
                            // Start turning the servo
224
                            // Start turning the servo
232
 
225
 
233
                            turnDirection = rxMsg.Data.bytes[0]; // FIXME riktning beroende på positivt eller negativt tal?
226
                            turnDirection = (rxMsg.Data.bytes[0]&0x80)?BLINDS_TURN_CW:BLINDS_TURN_CCW; // FIXME riktning beroende på positivt eller negativt tal?
234
                            servoToTurn = temp_id;
227
                            servoToTurn = temp_id;
235
                            servoFeed_timeStamp = Timebase_CurrentTime();
228
                            servoFeed_timeStamp = Timebase_CurrentTime();
236
 
229
 
237
                        }else if(rxMsg.Data.bytes[0] == BLINDS_CMD_STOP ){
230
                        }else if(rxMsg.Data.bytes[0] == BLINDS_CMD_STOP ){
238
                            // Stop turning
231
                            // Stop turning
Line 255... Line 248...
255
                            txMsg.DataLength = 6;
248
                            txMsg.DataLength = 6;
256
                            BIOS_CanSend(&txMsg);
249
                            BIOS_CanSend(&txMsg);
257
                        }*/
250
                        }*/
258
                    }
251
                    }
259
                }
252
                }
260
            }
253
            }
261
 
254
 
262
            rxMsgFull = 0;
255
            rxMsgFull = 0;
263
        }
256
        }
264
 
257
 
265
        if( (Timebase_CurrentTime() - timeStamp)  >= STATUS_SEND_PERIOD ){
258
        if( (Timebase_CurrentTime() - timeStamp)  >= STATUS_SEND_PERIOD ){
266
            timeStamp = Timebase_CurrentTime();
259
            timeStamp = Timebase_CurrentTime();
267
            // Send blinds status to CAN periodically
260
            // Send blinds status to CAN periodically
268
 
261
 
269
            boardTemperature = getTC1047temperature();
262
            boardTemperature = getTC1047temperature();
270
 
263
 
271
            if( (int32_t)boardTemperature >= FAILSAFE_TRESHOLD ){
264
            if( (int32_t)boardTemperature >= FAILSAFE_TRESHOLD ){
272
                blindsFailsafe();
265
                blindsFailsafe();
Line 278... Line 271...
278
                txMsg.Data.bytes[4] = getPosition(2);
271
                txMsg.Data.bytes[4] = getPosition(2);
279
                txMsg.Data.bytes[5] = getPosition(3);
272
                txMsg.Data.bytes[5] = getPosition(3);
280
                txMsg.DataLength = 6;
273
                txMsg.DataLength = 6;
281
 
274
 
282
                BIOS_CanSend(&txMsg);
275
                BIOS_CanSend(&txMsg);
283
            }
276
            }
284
        }
277
        }
285
        // If start turning servo
278
        // If start turning servo
286
/*      if( turnDirection ){
279
        if( turnDirection ){
287
            if( Timebase_CurrentTime() - timeStampTurn >= TURN_PERIOD ){
280
            if( Timebase_CurrentTime() - timeStampTurn >= TURN_PERIOD ){
288
                timeStampTurn = Timebase_CurrentTime();
281
                timeStampTurn = Timebase_CurrentTime();
289
                // Clockwise or counterclockwise?
282
                // Clockwise or counterclockwise?
290
                if( turnDirection == BLINDS_TURN_LEFT ){
283
                if( turnDirection == BLINDS_TURN_CCW ){
291
                    alterPosition( -STEPS_PER_TURN_PERIOD , servoToTurn);
284
                    alterPosition( -STEPS_PER_TURN_PERIOD , servoToTurn);
292
                }else if( turnDirection == BLINDS_TURN_RIGHT ){
285
                }else if( turnDirection == BLINDS_TURN_CW ){
293
                    alterPosition( STEPS_PER_TURN_PERIOD , servoToTurn);
286
                    alterPosition( STEPS_PER_TURN_PERIOD , servoToTurn);
-
 
287
                }
-
 
288
                servoFeed_timeStamp = Timebase_CurrentTime();
294
                }
289
            }
295
            }
290
        }
296
        }
291
 
297
        // Turn off the servo current feed
292
        // Turn off the servo current feed after timeout
298
        if( Timebase_CurrentTime() - servoFeed_timeStamp >= SERVO_FEED_TIME ){
293
        if( Timebase_CurrentTime() - servoFeed_timeStamp >= SERVO_FEED_TIME ){
299
            SERVO_FEED_PORT &= ~(1<<SERVO_FEED_PIN);
294
            SERVO_FEED_PORT &= ~(1<<SERVO_FEED_PIN);
300
            servoDisable();
295
            servoDisable();
301
        }else{
296
        }else{
302
            SERVO_FEED_PORT |= (1<<SERVO_FEED_PIN);
297
            SERVO_FEED_PORT |= (1<<SERVO_FEED_PIN);
303
            servoEnable();
298
            servoEnable();
304
        }*/
299
        }
305
    }
300
    }
306
    return 0;
301
    return 0;
307
}
302
}
308
 
303
 
309
void blindsFailsafe()
304
void blindsFailsafe()