Subversion Repositories HomeAutomation

Rev

Rev 483 | Rev 493 | Go to most recent revision | Only display areas with differences | Regard whitespace | Details | Blame | Last modification | View Log | SVN | RSS feed

Rev 483 Rev 486
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 <config.h>
26
#include <config.h>
27
#include <bios.h>
27
#include <bios.h>
28
#include <drivers/timer/timebase.h>
28
#include <drivers/timer/timebase.h>
29
//#include <funcdefs.h>
29
//#include <funcdefs.h>
30
 
30
 
31
/* defines */
31
/* defines */
32
#define RELAY_OFF           0x01
32
#define RELAY_OFF           0x01
33
#define RELAY_ON            0x02
33
#define RELAY_ON            0x02
34
#define FAILSAFE_MODE       0x0F
34
#define FAILSAFE_MODE       0x0F
35
#define FAILSAFE_TRESHOLD   0xFF  /* Degrees when nodeRelay goes into failsafe mode */
35
#define FAILSAFE_TRESHOLD   0xFF  /* Degrees when nodeRelay goes into failsafe mode */
36
 
36
 
37
#define RELAY_CMD_ON        0x01
37
#define RELAY_CMD_ON        0x01
38
#define RELAY_CMD_OFF       0x02
38
#define RELAY_CMD_OFF       0x02
39
#define RELAY_CMD_TOGGLE    0x03
39
#define RELAY_CMD_TOGGLE    0x03
40
#define RELAY_CMD_STATUS    0x04
40
#define RELAY_CMD_STATUS    0x04
41
#define RELAY_CMD_RESET     0x05
41
#define RELAY_CMD_RESET     0x05
42
#define STATUS_SEND_PERIOD  10000UL /* milliseconds */
42
#define STATUS_SEND_PERIOD  10000UL /* milliseconds */
43
#define FAILSAFE_SEND_PERIOD    10000UL /* milliseconds */
43
#define FAILSAFE_SEND_PERIOD    10000UL /* milliseconds */
44
#define BOUNCE_TIME         10 /* milliseconds */
44
#define BOUNCE_TIME         10 /* milliseconds */
45
#define BUTTON1             1
45
#define BUTTON1             1
46
#define BUTTON2             2
46
#define BUTTON2             2
-
 
47
 
-
 
48
#define APP_TYPE    CAN_APPTYPES_RELAY
-
 
49
#define APP_VERSION 0x0001
47
 
50
 
48
/*-------------------------------------------------------------------------
51
/*-------------------------------------------------------------------------
49
 * Global variables
52
 * Global variables
50
 * -----------------------------------------------------------------------*/
53
 * -----------------------------------------------------------------------*/
51
uint8_t relayStatus, failsafe_flag = 0;
54
uint8_t relayStatus, failsafe_flag = 0;
52
uint32_t boardTemperature = 0;
55
uint32_t boardTemperature = 0;
53
 
56
 
54
volatile Can_Message_t rxMsg; // Message storage
57
volatile Can_Message_t rxMsg; // Message storage
55
volatile uint8_t rxMsgFull;   // Synchronization flag
58
volatile uint8_t rxMsgFull;   // Synchronization flag
56
 
59
 
57
 
60
 
58
/*----------------------------------------------------------------------------
61
/*----------------------------------------------------------------------------
59
 * Functions
62
 * Functions
60
 * -------------------------------------------------------------------------*/
63
 * -------------------------------------------------------------------------*/
61
uint8_t relayOff();
64
uint8_t relayOff();
62
uint8_t relayOn();
65
uint8_t relayOn();
63
void relayFailsafe();
66
void relayFailsafe();
64
void sendStatus();
67
void sendStatus();
65
 
68
 
66
void can_receive(Can_Message_t *msg)
69
void can_receive(Can_Message_t *msg)
67
{ // CAN callback function
70
{ // CAN callback function
68
    if (!rxMsgFull) {
71
    if (!rxMsgFull) {
69
        memcpy((void*)&rxMsg, msg, sizeof(rxMsg));
72
        memcpy((void*)&rxMsg, msg, sizeof(rxMsg));
70
        rxMsgFull = 1;
73
        rxMsgFull = 1;
71
    }
74
    }
72
}
75
}
73
 
76
 
74
/*----------------------------------------------------------------------------
77
/*----------------------------------------------------------------------------
75
 * Interrupt routines // FIXME ordna och testa tryckknappar/rotary encoder
78
 * Interrupt routines // FIXME ordna och testa tryckknappar/rotary encoder
76
 * -------------------------------------------------------------------------*/
79
 * -------------------------------------------------------------------------*/
77
#if TWO_BUTTON_MODE
80
#if TWO_BUTTON_MODE
78
ISR( PCINT2_vect )
81
ISR( PCINT2_vect )
79
{
82
{
80
 
83
 
81
}
84
}
82
#endif
85
#endif
83
 
86
 
84
/*-----------------------------------------------------------------------------
87
/*-----------------------------------------------------------------------------
85
 * Main Program
88
 * Main Program
86
 *---------------------------------------------------------------------------*/
89
 *---------------------------------------------------------------------------*/
87
int main(void) {
90
int main(void) {
88
 
91
 
89
    uint32_t timeStamp = Timebase_CurrentTime(), temp = timeStamp;
92
    uint32_t timeStamp = Timebase_CurrentTime(), temp = timeStamp;
90
 
93
 
91
    adcTemperatureInit();
94
    adcTemperatureInit();
92
 
95
 
93
    // Set up callback for CAN messages
96
    // Set up callback for CAN messages
94
    BIOS_CanCallback = &can_receive;
97
    BIOS_CanCallback = &can_receive;
95
 
98
 
96
    sei();
99
    sei();
97
   
100
   
98
    Timebase_Init();
101
    Timebase_Init();
-
 
102
 
-
 
103
    Can_Message_t txMsg;
-
 
104
    txMsg.Id = (CAN_NMT_APP_START << CAN_SHIFT_NMT_TYPE) | (NODE_ID << CAN_SHIFT_NMT_SID);
-
 
105
    txMsg.DataLength = 4;
-
 
106
    txMsg.RemoteFlag = 0;
-
 
107
    txMsg.ExtendedFlag = 1;
-
 
108
    txMsg.Data.words[0] = APP_TYPE;
-
 
109
    txMsg.Data.words[1] = APP_VERSION;
-
 
110
    BIOS_CanSend(&txMsg);
99
 
111
 
100
    // Turn relay off
112
    // Turn relay off
101
    relayStatus = relayOff();
113
    relayStatus = relayOff();
102
 
114
 
103
    // Main loop
115
    // Main loop
104
    while (1) {
116
    while (1) {
105
 
117
 
106
        temp = Timebase_CurrentTime();
118
        temp = Timebase_CurrentTime();
107
        if( temp - timeStamp >= STATUS_SEND_PERIOD ){
119
        if( temp - timeStamp >= STATUS_SEND_PERIOD ){
108
            timeStamp = temp;
120
            timeStamp = temp;
109
            // Send relay status to CAN
121
            // Send relay status to CAN
110
            sendStatus();
122
            sendStatus();
111
        }
123
        }
112
 
124
 
113
        if (rxMsgFull) {
125
        if (rxMsgFull) {
114
            uint8_t acttype;
126
            uint8_t acttype;
115
            uint16_t servoid;
127
            uint16_t servoid;
116
       
128
       
117
            if ( ((rxMsg.Id & CAN_MASK_CLASS)>>CAN_SHIFT_CLASS) == CAN_ACT){
129
            if ( ((rxMsg.Id & CAN_MASK_CLASS)>>CAN_SHIFT_CLASS) == CAN_ACT){
118
                // Actuator package
130
                // Actuator package
119
                acttype =(uint8_t)((rxMsg.Id & CAN_MASK_ACT_FUNCC) >> CAN_SHIFT_ACT_FUNCC);
131
                acttype =(uint8_t)((rxMsg.Id & CAN_MASK_ACT_FUNCC) >> CAN_SHIFT_ACT_FUNCC);
120
                servoid = (uint16_t)((rxMsg.Id & CAN_MASK_ACT_NID) >> CAN_SHIFT_ACT_NID);
132
                servoid = (uint16_t)((rxMsg.Id & CAN_MASK_ACT_NID) >> CAN_SHIFT_ACT_NID);
121
               
133
               
122
                if ((acttype == ACT_FUNCC_RELAY) && (servoid == NODE_ID)) {
134
                if ((acttype == ACT_FUNCC_RELAY) && (servoid == NODE_ID)) {
123
                    // This is a message for this relay
135
                    // This is a message for this relay
124
                    if (rxMsg.Data.bytes[0] == RELAY_CMD_RESET) {
136
                    if (rxMsg.Data.bytes[0] == RELAY_CMD_RESET) {
125
                        // Return to normal operation
137
                        // Return to normal operation
126
                        failsafe_flag = 0;
138
                        failsafe_flag = 0;
127
                    }
139
                    }
128
                   
140
                   
129
                    if (!failsafe_flag) {
141
                    if (!failsafe_flag) {
130
                        if (rxMsg.Data.bytes[0] == RELAY_CMD_ON ){
142
                        if (rxMsg.Data.bytes[0] == RELAY_CMD_ON ){
131
                            // Turn relay on
143
                            // Turn relay on
132
                            relayStatus = relayOn();
144
                            relayStatus = relayOn();
133
           
145
           
134
                        } else if (rxMsg.Data.bytes[0] == RELAY_CMD_OFF ){
146
                        } else if (rxMsg.Data.bytes[0] == RELAY_CMD_OFF ){
135
                            // Turn relay off
147
                            // Turn relay off
136
                            relayStatus = relayOff();
148
                            relayStatus = relayOff();
137
           
149
           
138
                        } else if (rxMsg.Data.bytes[0] == RELAY_CMD_TOGGLE ){
150
                        } else if (rxMsg.Data.bytes[0] == RELAY_CMD_TOGGLE ){
139
                                // Toggle relay
151
                                // Toggle relay
140
                                if(relayStatus == RELAY_OFF){
152
                                if(relayStatus == RELAY_OFF){
141
                                    relayStatus = relayOn();
153
                                    relayStatus = relayOn();
142
                                }else{
154
                                }else{
143
                                    relayStatus = relayOff();
155
                                    relayStatus = relayOff();
144
                                }
156
                                }
145
           
157
           
146
                        }else if(rxMsg.Data.bytes[0] == RELAY_CMD_STATUS ){
158
                        }else if(rxMsg.Data.bytes[0] == RELAY_CMD_STATUS ){
147
                            // Send relay status to CAN
159
                            // Send relay status to CAN
148
                            sendStatus();
160
                            sendStatus();
149
                        }
161
                        }
150
                    }
162
                    }
151
                }
163
                }
152
            }
164
            }
153
            rxMsgFull = 0; //  
165
            rxMsgFull = 0; //  
154
 
166
 
155
        }
167
        }
156
       
168
       
157
    }
169
    }
158
    return 0;
170
    return 0;
159
}
171
}
160
 
172
 
161
uint8_t relayOff()
173
uint8_t relayOff()
162
{
174
{
163
    // Turn off relay
175
    // Turn off relay
164
    PORTC &= ~(1<<PC1);
176
    PORTC &= ~(1<<PC1);
165
    DDRC |= (1<<DDC1);
177
    DDRC |= (1<<DDC1);
166
 
178
 
167
    return RELAY_OFF;
179
    return RELAY_OFF;
168
}
180
}
169
 
181
 
170
uint8_t relayOn()
182
uint8_t relayOn()
171
{
183
{
172
    // Turn on relay
184
    // Turn on relay
173
    DDRC &= ~(1<<DDC1);
185
    DDRC &= ~(1<<DDC1);
174
    PORTC |= (1<<PC1);
186
    PORTC |= (1<<PC1);
175
 
187
 
176
    return RELAY_ON;
188
    return RELAY_ON;
177
}
189
}
178
 
190
 
179
void relayFailsafe()
191
void relayFailsafe()
180
{
192
{
181
    uint32_t failsafe_timeStamp, temp;
193
    uint32_t failsafe_timeStamp, temp;
182
    relayStatus = relayOff();
194
    relayStatus = relayOff();
183
    failsafe_flag = 1;
195
    failsafe_flag = 1;
184
 
196
 
185
    while(1){
197
    while(1){
186
 
198
 
187
        if( !failsafe_flag ){
199
        if( !failsafe_flag ){
188
            // Return from failsafe mode
200
            // Return from failsafe mode
189
            return;
201
            return;
190
        }
202
        }
191
// TODO skicka med period
203
// TODO skicka med period
192
        sendStatus( );
204
        sendStatus( );
193
    }
205
    }
194
}
206
}
195
 
207
 
196
void sendStatus()
208
void sendStatus()
197
{
209
{
198
    uint32_t temperature;
210
    uint32_t temperature;
199
    Can_Message_t statusMsg;
211
    Can_Message_t statusMsg;
200
    statusMsg.RemoteFlag = 0;
212
    statusMsg.RemoteFlag = 0;
201
    statusMsg.ExtendedFlag = 1;
213
    statusMsg.ExtendedFlag = 1;
202
    statusMsg.DataLength = 4;
214
    statusMsg.DataLength = 4;
203
    statusMsg.Id = ( CAN_SNS<<CAN_SHIFT_CLASS )|( SNS_FUNCC_RELAY_STATUS<<CAN_SHIFT_ACT_FUNCC )|( NODE_ID<<CAN_SHIFT_SNS_SID );
215
    statusMsg.Id = ( CAN_SNS<<CAN_SHIFT_CLASS )|( SNS_FUNCC_RELAY_STATUS<<CAN_SHIFT_ACT_FUNCC )|( NODE_ID<<CAN_SHIFT_SNS_SID );
204
   
216
   
205
    temperature = getTC1047temperature();
217
    temperature = getTC1047temperature();
206
    if( (int32_t)temperature >= FAILSAFE_TRESHOLD && !failsafe_flag ){
218
    if( (int32_t)temperature >= FAILSAFE_TRESHOLD && !failsafe_flag ){
207
        // Goto failsafe mode
219
        // Goto failsafe mode
208
        relayFailsafe();
220
        relayFailsafe();
209
    }else{
221
    }else{
210
        // Transmitt node status
222
        // Transmitt node status
211
        statusMsg.Data.bytes[0] = temperature & 0x00FF;
223
        statusMsg.Data.bytes[0] = temperature & 0x00FF;
212
        statusMsg.Data.bytes[1] = (temperature & 0xFF00)>>8;
224
        statusMsg.Data.bytes[1] = (temperature & 0xFF00)>>8;
213
        statusMsg.Data.bytes[2] = relayStatus;
225
        statusMsg.Data.bytes[2] = relayStatus;
214
        statusMsg.Data.bytes[3] = failsafe_flag;
226
        statusMsg.Data.bytes[3] = failsafe_flag;
215
 
227
 
216
        BIOS_CanSend( &statusMsg );
228
        BIOS_CanSend( &statusMsg );
217
    }
229
    }
218
}
230
}
219
 
231