Subversion Repositories HomeAutomation

Rev

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

Rev 360 Rev 391
1
/**
1
/**
2
 * CAN Relay.
2
 * CAN Relay.
3
 *
3
 *
4
 *
4
 *
5
 * @date    2006-12-17
5
 * @date    2006-12-17
6
 * @author  Erik Larsson
6
 * @author  Erik Larsson
7
 *
7
 *
8
 *   ADC=Vin*1024/Vref
8
 *   ADC=Vin*1024/Vref
9
 *   Vout=( 10 mV/C )( Temperature C ) + 500 mV
9
 *   Vout=( 10 mV/C )( Temperature C ) + 500 mV
10
 *
10
 *
11
 *  Transmitts: DATA: <temperature low byte> <temperature high byte> <relay status> <failsafe mode>
11
 *  Transmitts: DATA: <temperature low byte> <temperature high byte> <relay status> <failsafe mode>
12
 *
12
 *
13
 *
13
 *
14
 */
14
 */
15
 
15
 
16
#define TWO_BUTTON_MODE 0 /* If using two (or just one but nothing more) auxiliary buttons */
16
#define TWO_BUTTON_MODE 0 /* If using two (or just one but nothing more) auxiliary buttons */
17
 
17
 
18
/*-----------------------------------------------------------------------------
18
/*-----------------------------------------------------------------------------
19
 * Includes
19
 * Includes
20
 *---------------------------------------------------------------------------*/
20
 *---------------------------------------------------------------------------*/
21
/* system files */
21
/* system files */
22
#include <avr/io.h>
22
#include <avr/io.h>
23
#include <avr/interrupt.h>
23
#include <avr/interrupt.h>
24
#include <stdio.h>
24
#include <stdio.h>
25
/* lib files */
25
/* lib files */
26
#include <bios.h>
26
#include <bios.h>
-
 
27
#include <config.h>
-
 
28
#include <drivers/timer/timebase.h>
27
#include <tc1047.h>
29
#include <tc1047.h>
28
#include <funcdefs.h>
30
#include <funcdefs.h>
29
 
31
 
30
/* defines */
32
/* defines */
31
#define GROUP_ID        0x01UL
33
#define GROUP_ID        0x01UL
32
 
34
 
33
#define RELAY_OFF           0x01
35
#define RELAY_OFF           0x01
34
#define RELAY_ON            0x02
36
#define RELAY_ON            0x02
35
#define FAILSAFE_MODE       0x0F
37
#define FAILSAFE_MODE       0x0F
36
#define FAILSAFE_TRESHOLD   60  /* Degrees when nodeRelay goes into failsafe mode */
38
#define FAILSAFE_TRESHOLD   0xFF  /* Degrees when nodeRelay goes into failsafe mode */
37
 
39
 
38
#define RELAY_CMD_ON        0x01
40
#define RELAY_CMD_ON        0x01
39
#define RELAY_CMD_OFF       0x02
41
#define RELAY_CMD_OFF       0x02
40
#define RELAY_CMD_TOGGLE    0x03
42
#define RELAY_CMD_TOGGLE    0x03
41
#define RELAY_CMD_STATUS    0x04
43
#define RELAY_CMD_STATUS    0x04
42
#define RELAY_CMD_RESET     0x05
44
#define RELAY_CMD_RESET     0x05
43
#define STATUS_SEND_PERIOD  10000UL /* milliseconds */
45
#define STATUS_SEND_PERIOD  10000UL /* milliseconds */
44
#define FAILSAFE_SEND_PERIOD    10000UL /* milliseconds */
46
#define FAILSAFE_SEND_PERIOD    10000UL /* milliseconds */
45
#define BOUNCE_TIME         10 /* milliseconds */
47
#define BOUNCE_TIME         10 /* milliseconds */
46
#define BUTTON1             1
48
#define BUTTON1             1
47
#define BUTTON2             2
49
#define BUTTON2             2
48
 
-
 
49
#define TRUE    1
-
 
50
#define FALSE   0
-
 
51
 
50
 
52
/*-------------------------------------------------------------------------
51
/*-------------------------------------------------------------------------
53
 * Global variables
52
 * Global variables
54
 * -----------------------------------------------------------------------*/
53
 * -----------------------------------------------------------------------*/
55
uint8_t relayStatus, failsafe_flag = FALSE, rxMsg_Data_bytes[8], msg_received = FALSE;
54
uint8_t relayStatus, failsafe_flag = 0, rxMsg_Data_bytes[8], msg_received = 0;
56
uint32_t boardTemperature = 0;
55
uint32_t boardTemperature = 0;
57
 
56
 
58
 
57
 
59
/*----------------------------------------------------------------------------
58
/*----------------------------------------------------------------------------
60
 * Functions
59
 * Functions
61
 * -------------------------------------------------------------------------*/
60
 * -------------------------------------------------------------------------*/
62
uint8_t relayOff();
61
uint8_t relayOff();
63
uint8_t relayOn();
62
uint8_t relayOn();
64
void relayFailsafe();
63
void relayFailsafe();
65
void sendStatus();
64
void sendStatus();
66
 
65
 
67
void can_receive(Can_Message_t *msg)
66
void can_receive(Can_Message_t *msg)
68
{ // CAN callback function
67
{ // CAN callback function
69
    uint32_t act;
68
    uint32_t act;
70
    uint8_t m, act_type, group, node;
69
    uint8_t m, act_type, group, node;
71
 
70
 
72
    if( ((msg->Id & CLASS_MASK)>>CLASS_MASK_BITS) == CLASS_ACT){
71
    if( ((msg->Id & CLASS_MASK)>>CLASS_MASK_BITS) == CLASS_ACT){
73
        // Actuator package
72
        // Actuator package
74
        act = (msg->Id & TYPE_MASK) >> TYPE_MASK_BITS;
73
        act = (msg->Id & TYPE_MASK) >> TYPE_MASK_BITS;
75
        act_type =(uint8_t)((act & ACT_TYPE_MASK) >> ACT_TYPE_BITS);
74
        act_type =(uint8_t)((act & ACT_TYPE_MASK) >> ACT_TYPE_BITS);
76
        group = (uint8_t)((act & ACT_GROUP_MASK) >> ACT_GROUP_BITS);
75
        group = (uint8_t)((act & ACT_GROUP_MASK) >> ACT_GROUP_BITS);
77
        node = (uint8_t)((act & ACT_NODE_MASK) >> ACT_NODE_BITS);
76
        node = (uint8_t)((act & ACT_NODE_MASK) >> ACT_NODE_BITS);
78
 
77
 
79
        if( act_type == ACT_TYPE_RELAY && (group == GROUP_ID || node == NODE_ID) ){
78
        if( act_type == ACT_TYPE_RELAY && (group == GROUP_ID || node == NODE_ID) ){
80
            // This is a message for this node
79
            // This is a message for this node
81
 
80
 
82
            if(rxMsg_Data_bytes[0] == RELAY_CMD_RESET){
81
            if(rxMsg_Data_bytes[0] == RELAY_CMD_RESET){
83
                // Return to normal operation
82
                // Return to normal operation
84
                failsafe_flag = 0;
83
                failsafe_flag = 0;
85
            }
84
            }
86
 
85
 
87
            for(m=0;m<(msg->DataLength);m++){
86
            for(m=0;m<(msg->DataLength);m++){
88
                rxMsg_Data_bytes[m] = msg->Data.bytes[m];
87
                rxMsg_Data_bytes[m] = msg->Data.bytes[m];
89
            }
88
            }
90
            msg_received = TRUE; // Tell application that message has arrived
89
            msg_received = 1; // Tell application that message has arrived
91
        }
90
        }
92
    }
91
    }
93
}
92
}
94
 
93
 
95
/*----------------------------------------------------------------------------
94
/*----------------------------------------------------------------------------
96
 * Interrupt routines // FIXME ordna och testa tryckknappar/rotary encoder
95
 * Interrupt routines // FIXME ordna och testa tryckknappar/rotary encoder
97
 * -------------------------------------------------------------------------*/
96
 * -------------------------------------------------------------------------*/
98
#if TWO_BUTTON_MODE
97
#if TWO_BUTTON_MODE
99
ISR( PCINT2_vect )
98
ISR( PCINT2_vect )
100
{
99
{
101
 
100
 
102
}
101
}
103
#endif
102
#endif
104
 
103
 
105
/*-----------------------------------------------------------------------------
104
/*-----------------------------------------------------------------------------
106
 * Main Program
105
 * Main Program
107
 *---------------------------------------------------------------------------*/
106
 *---------------------------------------------------------------------------*/
108
int main(void) {
107
int main(void) {
109
 
108
 
110
    uint32_t timeStamp = bios->timebase_get(), temp = timeStamp;
109
    uint32_t timeStamp = Timebase_CurrentTime(), temp = timeStamp;
111
 
110
 
112
    Can_Message_t txMsg;
111
    Can_Message_t txMsg;
113
    txMsg.RemoteFlag = 0;
112
    txMsg.RemoteFlag = 0;
114
    txMsg.ExtendedFlag = 1;
113
    txMsg.ExtendedFlag = 1;
115
    txMsg.DataLength = 4;
114
    txMsg.DataLength = 4;
116
 
115
 
117
    adcTemperatureInit();
116
    adcTemperatureInit();
118
 
117
 
119
    // Set up callback for CAN messages
118
    // Set up callback for CAN messages
120
    bios->can_callback = &can_receive;
119
    BIOS_CanCallback = &can_receive;
121
 
120
 
122
    sei();
121
    sei();
-
 
122
   
-
 
123
    Timebase_Init();
123
 
124
 
124
    // Turn relay off
125
    // Turn relay off
125
    relayStatus = relayOff();
126
    relayStatus = relayOff();
126
//  txMsg.Id = 0x8000110; // FIXME
127
//  txMsg.Id = 0x8000110; // FIXME
127
//  bios->can_send(&txMsg);
128
//  bios->can_send(&txMsg);
128
 
129
 
129
    // Main loop
130
    // Main loop
130
    while (1) {
131
    while (1) {
131
 
132
 
132
        if(msg_received && !failsafe_flag ){
133
        if(msg_received && !failsafe_flag ){
133
 
134
 
134
            if( rxMsg_Data_bytes[0] == RELAY_CMD_ON ){
135
            if( rxMsg_Data_bytes[0] == RELAY_CMD_ON ){
135
                // Turn relay on
136
                // Turn relay on
136
                relayStatus = relayOn();
137
                relayStatus = relayOn();
137
 
138
 
138
            }else if(rxMsg_Data_bytes[0] == RELAY_CMD_OFF ){
139
            }else if(rxMsg_Data_bytes[0] == RELAY_CMD_OFF ){
139
                // Turn relay off
140
                // Turn relay off
140
                relayStatus = relayOff();
141
                relayStatus = relayOff();
141
 
142
 
142
            }else if(rxMsg_Data_bytes[0] == RELAY_CMD_TOGGLE ){
143
            }else if(rxMsg_Data_bytes[0] == RELAY_CMD_TOGGLE ){
143
                    // Toggle relay
144
                    // Toggle relay
144
                    if(relayStatus == RELAY_OFF){
145
                    if(relayStatus == RELAY_OFF){
145
                        relayStatus = relayOn();
146
                        relayStatus = relayOn();
146
                    }else{
147
                    }else{
147
                        relayStatus = relayOff();
148
                        relayStatus = relayOff();
148
                    }
149
                    }
149
 
150
 
150
            }else if(rxMsg_Data_bytes[0] == RELAY_CMD_STATUS ){
151
            }else if(rxMsg_Data_bytes[0] == RELAY_CMD_STATUS ){
151
                // Send relay status to CAN
152
                // Send relay status to CAN
152
                sendStatus();
153
                sendStatus();
153
            }
154
            }
154
            msg_received = FALSE;
155
            msg_received = 0;
155
        }
156
        }
156
 
157
 
157
        temp = bios->timebase_get();
158
        temp = Timebase_CurrentTime();
158
        if( temp - timeStamp >= STATUS_SEND_PERIOD ){
159
        if( temp - timeStamp >= STATUS_SEND_PERIOD ){
159
            timeStamp = temp;
160
            timeStamp = temp;
160
/*          txMsg.Id = 0x8000111;
161
/*          txMsg.Id = 0x8000111;
161
            txMsg.Data.bytes[0] = timeStamp & 0x000000FF;
162
            txMsg.Data.bytes[0] = timeStamp & 0x000000FF;
162
            txMsg.Data.bytes[0] = (timeStamp & 0x0000FF00)>>8;
163
            txMsg.Data.bytes[0] = (timeStamp & 0x0000FF00)>>8;
163
            txMsg.Data.bytes[0] = (timeStamp & 0x00FF0000)>>16;
164
            txMsg.Data.bytes[0] = (timeStamp & 0x00FF0000)>>16;
164
            txMsg.Data.bytes[0] = (timeStamp & 0xFF000000)>>24;
165
            txMsg.Data.bytes[0] = (timeStamp & 0xFF000000)>>24;
165
            bios->can_send(&txMsg);
166
            bios->can_send(&txMsg);
166
*/          // Send relay status to CAN
167
*/          // Send relay status to CAN
167
            sendStatus();
168
            sendStatus();
168
        }
169
        }
169
    }
170
    }
170
    return 0;
171
    return 0;
171
}
172
}
172
 
173
 
173
uint8_t relayOff()
174
uint8_t relayOff()
174
{
175
{
175
    // Turn off relay
176
    // Turn off relay
176
    PORTC &= ~(1<<PC1);
177
    PORTC &= ~(1<<PC1);
177
    DDRC |= (1<<DDC1);
178
    DDRC |= (1<<DDC1);
178
 
179
 
179
    return RELAY_OFF;
180
    return RELAY_OFF;
180
}
181
}
181
 
182
 
182
uint8_t relayOn()
183
uint8_t relayOn()
183
{
184
{
184
    // Turn on relay
185
    // Turn on relay
185
    DDRC &= ~(1<<DDC1);
186
    DDRC &= ~(1<<DDC1);
186
    PORTC |= (1<<PC1);
187
    PORTC |= (1<<PC1);
187
 
188
 
188
    return RELAY_ON;
189
    return RELAY_ON;
189
}
190
}
190
 
191
 
191
void relayFailsafe()
192
void relayFailsafe()
192
{
193
{
193
    uint32_t failsafe_timeStamp, temp;
194
    uint32_t failsafe_timeStamp, temp;
194
    relayStatus = relayOff();
195
    relayStatus = relayOff();
195
    failsafe_flag = 1;
196
    failsafe_flag = 1;
196
 
197
 
197
    while(1){
198
    while(1){
198
 
199
 
199
        if( !failsafe_flag ){
200
        if( !failsafe_flag ){
200
            // Return from failsafe mode
201
            // Return from failsafe mode
201
            return;
202
            return;
202
        }
203
        }
203
// TODO skicka med period
204
// TODO skicka med period
204
        sendStatus( );
205
        sendStatus( );
205
    }
206
    }
206
}
207
}
207
 
208
 
208
void sendStatus()
209
void sendStatus()
209
{
210
{
210
    uint32_t temperature;
211
    uint32_t temperature;
211
    Can_Message_t statusMsg;
212
    Can_Message_t statusMsg;
212
    statusMsg.RemoteFlag = 0;
213
    statusMsg.RemoteFlag = 0;
213
    statusMsg.ExtendedFlag = 1;
214
    statusMsg.ExtendedFlag = 1;
214
    statusMsg.DataLength = 4;
215
    statusMsg.DataLength = 4;
215
    statusMsg.Id = ( CLASS_SNS<<CLASS_MASK_BITS )|( SNS_ACT_STATUS_RELAY<<TYPE_MASK_BITS )|(NODE_ID);
216
    statusMsg.Id = ( CLASS_SNS<<CLASS_MASK_BITS )|( SNS_ACT_STATUS_RELAY<<TYPE_MASK_BITS )|(NODE_ID);
216
 
217
 
217
    temperature = getTC1047temperature();
218
    temperature = getTC1047temperature();
218
    if( (int32_t)temperature >= FAILSAFE_TRESHOLD && !failsafe_flag ){
219
    if( (int32_t)temperature >= FAILSAFE_TRESHOLD && !failsafe_flag ){
219
        // Goto failsafe mode
220
        // Goto failsafe mode
220
        relayFailsafe();
221
        relayFailsafe();
221
    }else{
222
    }else{
222
        // Transmitt node status
223
        // Transmitt node status
223
        statusMsg.Data.bytes[0] = temperature & 0x00FF;
224
        statusMsg.Data.bytes[0] = temperature & 0x00FF;
224
        statusMsg.Data.bytes[1] = (temperature & 0xFF00)>>8;
225
        statusMsg.Data.bytes[1] = (temperature & 0xFF00)>>8;
225
        statusMsg.Data.bytes[2] = relayStatus;
226
        statusMsg.Data.bytes[2] = relayStatus;
226
        statusMsg.Data.bytes[3] = failsafe_flag;
227
        statusMsg.Data.bytes[3] = failsafe_flag;
227
 
228
 
228
        bios->can_send( &statusMsg );
229
        BIOS_CanSend( &statusMsg );
229
    }
230
    }
230
}
231
}
231
 
232