Subversion Repositories HomeAutomation

Rev

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

Rev 322 Rev 323
Line 49... Line 49...
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;
Line 67... Line 71...
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)
Line 105... Line 108...
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 */
Line 118... Line 121...
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 ){
Line 139... Line 142...
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
 
Line 169... Line 172...
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 PD2 */
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
    DDRC |= (1<<DDC0);
181
    SERVO_FEED_DDR |= (1<<SERVO_FEED_PIN);
179
    PORTC &= ~(1<<PC0);
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
 
Line 190... Line 191...
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
                timeStampTurn = 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
                timeStampTurn = 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
                timeStampTurn = 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
 
Line 277... Line 277...
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
            PORTC &= ~(1<<PC0);
282
            SERVO_FEED_PORT &= ~(1<<SERVO_FEED_PIN);
283
        }else{
283
        }else{
284
            PORTC |= (1<<PC0);
284
            SERVO_FEED_PORT |= (1<<SERVO_FEED_PIN);
285
        }
285
        }
286
    }
286
    }
287
    return 0;
287
    return 0;
288
}
288
}
289
 
289
 
Line 291... Line 291...
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
    PORTC &= ~(1<<PC0);
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) {