Subversion Repositories HomeAutomation

Rev

Rev 187 | Rev 192 | Go to most recent revision | Blame | Compare with Previous | Last modification | View Log | SVN | RSS feed

  1. /**
  2.  * CAN Relay.
  3.  *
  4.  *
  5.  * @date    2006-12-17
  6.  * @author  Erik Larsson
  7.  *
  8.  *   Observera! Applikationen är endast testad på labplatta med m88, pot och lysdiod
  9.  *
  10.  *   ADC=Vin*1024/Vref
  11.  *   Vout=( 10 mV/C )( Temperature C ) + 500 mV
  12.  *
  13.  *  Transmitts: DATA: <temperature low byte> <temperature high byte> <relay status> <failsafe mode>
  14.  *
  15.  *
  16.  */
  17.  
  18. #define DEBUG   0 /* Prints debug messages to uart */
  19.  
  20. /*-----------------------------------------------------------------------------
  21.  * Includes
  22.  *---------------------------------------------------------------------------*/
  23. /* system files */
  24. #include <avr/io.h>
  25. #include <avr/interrupt.h>
  26. #include <avr/wdt.h>
  27. #include <stdio.h>
  28. /* lib files */
  29. #include <can.h>
  30. #include <serial.h>
  31. #include <timebase.h>
  32.  
  33. #include <tc1047.h>
  34.  
  35. /* defines */
  36. #define RELAY_OFF           0x01
  37. #define RELAY_ON            0x02
  38. #define FAILSAFE_MODE       0x0F
  39. #define FAILSAFE_TRESHOLD   60  /* Degrees when nodeRelay goes into failsafe mode */
  40.  
  41. #define RELAY_CMD_GET       0x1200
  42. #define RELAY_CMD_SEND      0x1201
  43. #define RELAY_CMD_ON        0x01
  44. #define RELAY_CMD_OFF       0x02
  45. #define RELAY_CMD_TOGGLE    0x03
  46. #define RELAY_CMD_STATUS    0x04
  47. #define RELAY_CMD_RESET     0x05
  48. #define STATUS_SEND_PERIOD  10000 /* milliseconds */
  49. #define FAILSAFE_SEND_PERIOD  10000 /* milliseconds */
  50.  
  51. /*----------------------------------------------------------------------------
  52.  * Functions
  53.  * -------------------------------------------------------------------------*/
  54. uint8_t relayOff();
  55. uint8_t relayOn();
  56. void relayFailsafe();
  57.  
  58. /*-----------------------------------------------------------------------------
  59.  * Main Program
  60.  *---------------------------------------------------------------------------*/
  61. int main(void) {
  62.  
  63.     uint8_t relayStatus;
  64.     uint32_t boardTemperature = 0;
  65.  
  66.     adcTemperatureInit();
  67.  
  68.     Timebase_Init();
  69. #if DEBUG
  70.     Serial_Init();
  71. #endif
  72.     sei();
  73.  
  74.     /* Turn relay off */
  75.     relayStatus = relayOff();
  76.  
  77. #if DEBUG
  78.     printf("\n------------------------------------------------------------\n");
  79.     printf(  "   CAN: Relay\n");
  80.     printf(  "------------------------------------------------------------\n");
  81.  
  82.  
  83.     printf("CanInit...");
  84.     if (Can_Init() != CAN_OK) {
  85.         printf("FAILED!\n");
  86.     }else{
  87.         printf("OK!\n");
  88.     }
  89. #else
  90.     Can_Init();
  91. #endif
  92.     uint32_t timeStamp = 0;
  93.  
  94.     Can_Message_t txMsg;
  95.     Can_Message_t rxMsg;
  96.     txMsg.Id = RELAY_CMD_SEND;
  97.     txMsg.RemoteFlag = 0;
  98.     txMsg.ExtendedFlag = 1;
  99.     txMsg.DataLength = 3;
  100.  
  101.     /* main loop */
  102.     while (1) {
  103.         /* service the CAN routines */
  104.         Can_Service();
  105.  
  106.         /* check if any messages have been received */
  107.         while (Can_Receive(&rxMsg) == CAN_OK) {
  108.             /* This relay is adressed*/
  109.             if( rxMsg.Id == RELAY_CMD_GET ){
  110.  
  111.                 if( rxMsg.Data.bytes[0] == RELAY_CMD_ON ){
  112.                     /* Turn relay on */
  113.                     relayStatus = relayOn();
  114.  
  115.                 }else if(rxMsg.Data.bytes[0] == RELAY_CMD_OFF){
  116.                     /* Turn relay off */
  117.                     relayStatus = relayOff();
  118.  
  119.                 }else if(rxMsg.Data.bytes[0] == RELAY_CMD_TOGGLE ){
  120.                     /* Toggle relay */
  121.  
  122.                     if(relayStatus == RELAY_OFF){
  123.                         relayStatus = relayOn();
  124.                     }else{
  125.                         relayStatus = relayOff();
  126.                     }
  127.  
  128.                 }else if(rxMsg.Data.bytes[0] == RELAY_CMD_STATUS){
  129.                     /* Send relay status to CAN */
  130.  
  131.                     boardTemperature = getTC1047temperature();
  132.  
  133.                     if( (int32_t)boardTemperature >= FAILSAFE_TRESHOLD ){
  134.                         relayFailsafe();
  135.                     }else{
  136.                         txMsg.Data.bytes[0] = boardTemperature & 0x00FF;
  137.                         txMsg.Data.bytes[1] = (boardTemperature & 0xFF00)>>8;
  138.                         txMsg.Data.bytes[2] = relayStatus;
  139.  
  140.                         Can_Send( &txMsg );
  141.                     }
  142.                 }
  143.             }
  144.         }
  145.  
  146.         if( Timebase_PassedTimeMillis(timeStamp) >= STATUS_SEND_PERIOD ){
  147.             timeStamp = Timebase_CurrentTime();
  148.             /* Send relay status to CAN */
  149.  
  150.             boardTemperature = getTC1047temperature();
  151.  
  152.             if( (int32_t)boardTemperature >= FAILSAFE_TRESHOLD ){
  153.                 relayFailsafe();
  154.             }else{
  155.                 txMsg.Data.bytes[0] = boardTemperature & 0x00FF;
  156.                 txMsg.Data.bytes[1] = (boardTemperature & 0xFF00)>>8;
  157.                 txMsg.Data.bytes[2] = relayStatus;
  158.  
  159.                 Can_Send( &txMsg );
  160. #if DEBUG
  161.                 printf("Periodic message sent...\n");
  162.                 printf("Temperature: %d\nRelay status: %u\n", boardTemperature, relayStatus);
  163. #endif
  164.             }
  165.         }
  166.     }
  167.     return 0;
  168. }
  169.  
  170. uint8_t relayOff()
  171. {
  172.     /* Set as output and sink */
  173.     PORTC &= ~(1<<PC1);
  174.     DDRC |= (1<<DDC1);
  175.  
  176. #if DEBUG
  177.     printf("relay turned off\n");
  178. #endif
  179.  
  180.     return RELAY_OFF;
  181. }
  182.  
  183. uint8_t relayOn()
  184. {
  185.     /* Set as input and enable pull-up */
  186.     DDRC &= ~(1<<DDC1);
  187.     PORTC |= (1<<PC1);
  188.  
  189. #if DEBUG
  190.     printf("relay turned on\n");
  191. #endif
  192.  
  193.     return RELAY_ON;
  194. }
  195.  
  196. void relayFailsafe()
  197. {
  198.     uint8_t relayStatus;
  199.     uint32_t boardTemperature = 0, timeStamp = 0;
  200.  
  201.     Can_Message_t txMsg;
  202.     Can_Message_t rxMsg;
  203.     txMsg.Id = RELAY_CMD_SEND;
  204.     txMsg.RemoteFlag = 0;
  205.     txMsg.ExtendedFlag = 1;
  206.     txMsg.DataLength = 4;
  207.  
  208.     relayStatus = relayOff();
  209.  
  210.     while(1){
  211.         /* service the CAN routines */
  212.         Can_Service();
  213.  
  214.         /* check if any messages have been received */
  215.         while (Can_Receive(&rxMsg) == CAN_OK) {
  216.             /* This relay is adressed*/
  217.             if( rxMsg.Id == RELAY_CMD_GET ){
  218.                 if( rxMsg.Data.bytes[0] == RELAY_CMD_RESET ){
  219.                     /* Return from failsafe mode */
  220.                     return 0;
  221.                 }
  222.             }
  223.         }
  224.  
  225.         if( Timebase_PassedTimeMillis(timeStamp) >= FAILSAFE_SEND_PERIOD ){
  226.             timeStamp = Timebase_CurrentTime();
  227.             /* Send relay status to CAN */
  228. #if DEBUG
  229.             printf("Failsafe mode!\n");
  230. #endif
  231.             boardTemperature = getTC1047temperature();
  232.  
  233.             txMsg.Data.bytes[0] = boardTemperature & 0x00FF;
  234.             txMsg.Data.bytes[1] = (boardTemperature & 0xFF00)>>8;
  235.             txMsg.Data.bytes[2] = relayStatus;
  236.             txMsg.Data.bytes[3] = FAILSAFE_MODE;
  237.  
  238.             Can_Send( &txMsg );
  239.         }
  240.     }
  241. }
  242.  
  243.