Subversion Repositories HomeAutomation

Rev

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