Subversion Repositories HomeAutomation

Rev

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

  1.  
  2. #include "sns_power.h"
  3.  
  4. uint32_t volatile PreviusTimerValue, lastMeasurment;
  5. uint16_t volatile EnergyCounter;
  6. uint8_t volatile tmpCounter;
  7. uint8_t sns_power_ReportInterval = (uint8_t)sns_power_SEND_PERIOD;
  8. uint16_t volatile MeasurmentBuffer[32];
  9. uint8_t volatile MeasurmentBufferPointer;
  10.  
  11. #ifdef sns_power_USEEEPROM
  12. #include "sns_power_eeprom.h"
  13. struct eeprom_sns_power EEMEM eeprom_sns_power =
  14. {
  15.     {
  16.         ///TODO: Define initialization values on the EEPROM variables here, this will generate a *.eep file that can be used to store this values to the node, can in future be done with a EEPROM module and the make-scrips. Write the values in the exact same order as the struct is defined in the *.h file.
  17.         (uint8_t)sns_power_SEND_PERIOD, // reportInterval
  18.     },
  19.     0   // crc, must be a correct value, but this will also be handled by the EEPROM module or make scripts
  20. };
  21. #endif
  22.  
  23. /*********************************************************************//**
  24. Function: SIGNAL(POWER_SNS_PCINT_vector)
  25. Purpose:  Executed when pin change on ROTARY_CH1 is seen.
  26. Input:    -
  27. Returns:  -
  28. **************************************************************************/
  29. SIGNAL(POWER_SNS_PCINT_vector) {
  30.     if (gpio_get_state(POWER_SNS_PIN) == 1)
  31.     {
  32.         if (Timer_GetTicks() - PreviusTimerValue >= 1)
  33.         {
  34.             lastMeasurment =  Timer_GetTicks() - PreviusTimerValue;
  35.             PreviusTimerValue = Timer_GetTicks();
  36.             tmpCounter++;
  37.             if(tmpCounter >= 10) {
  38.                 EnergyCounter++;
  39.                 tmpCounter = 0;
  40.             }
  41.         }
  42.     }
  43. } /* SIGNAL(POWER_SNS_PCINT_vector) */
  44.  
  45. void sns_power_Init(void)
  46. {
  47. #ifdef sns_power_USEEEPROM
  48.     if (EEDATA_OK)
  49.     {
  50.       ///TODO: Use stored data to set initial values for the module
  51.       sns_power_ReportInterval = eeprom_read_byte(EEDATA.reportInterval);
  52.     } else
  53.     {   //The CRC of the EEPROM is not correct, store default values and update CRC
  54.       eeprom_write_byte_crc(EEDATA.reportInterval, sns_power_SEND_PERIOD, WITHOUT_CRC);
  55.       EEDATA_UPDATE_CRC;
  56.       sns_power_ReportInterval = eeprom_read_byte(EEDATA.reportInterval);
  57.     }
  58. #endif  
  59.     ///TODO: Initialize hardware etc here
  60.     gpio_set_in(POWER_SNS_PIN); // Set to input
  61.     gpio_set_pin(POWER_SNS_PIN);    // Enable pull-up
  62.    
  63.     // Enable IO-pin interrupt
  64.     PCICR |= (1<<POWER_SNS_PCIE);
  65.     POWER_SNS_PCMSK |= (1<<POWER_SNS_PCINT);
  66.     MeasurmentBufferPointer = 0;
  67.     Timer_SetTimeout(sns_power_SEND_TIMER, sns_power_ReportInterval*1000 , TimerTypeFreeRunning, 0);
  68. }
  69.  
  70. void sns_power_Process(void)
  71. {
  72.     if (lastMeasurment != 0) {
  73.           MeasurmentBufferPointer++;
  74.           MeasurmentBuffer[MeasurmentBufferPointer] = (uint16_t) lastMeasurment;//(uint16_t) 360000/lastMeasurment;
  75.           lastMeasurment=0;
  76.     }
  77.     StdCan_Msg_t txMsg;
  78.     ///TODO: Stuff that needs doing is done here
  79.     if (Timer_Expired(sns_power_SEND_TIMER)) {
  80.         //4 times average
  81.         uint32_t Avg4 = MeasurmentBuffer[MeasurmentBufferPointer] + MeasurmentBuffer[MeasurmentBufferPointer-1] + MeasurmentBuffer[MeasurmentBufferPointer-2] + MeasurmentBuffer[MeasurmentBufferPointer-3];
  82.         Avg4 = (360000/(Avg4/4));
  83.         //32 times average
  84.         uint32_t Avg32 = 0;
  85.         for (uint8_t i = 0; i<32;i++) {
  86.              Avg32 += MeasurmentBuffer[i];
  87.         }
  88.         Avg32 /= 32;
  89.         Avg32 = (360000/Avg32);
  90.  
  91.         StdCan_Set_class(txMsg.Header, CAN_MODULE_CLASS_SNS); ///TODO: Change this to the actual class type
  92.         StdCan_Set_direction(txMsg.Header, DIRECTIONFLAG_FROM_OWNER);
  93.         txMsg.Header.ModuleType = CAN_MODULE_TYPE_SNS_POWER; ///TODO: Change this to the actual module type
  94.         txMsg.Header.ModuleId = sns_power_ID;
  95.         txMsg.Header.Command = CAN_MODULE_CMD_PHYSICAL_ELECTRICPOWER;
  96.         txMsg.Length = 8;
  97.         uint32_t tmp = 360000/MeasurmentBuffer[MeasurmentBufferPointer];
  98.         txMsg.Data[0] = (uint8_t)((tmp>>8) & 0xff);
  99.         txMsg.Data[1] = (uint8_t)(tmp & 0xff);
  100.         txMsg.Data[2] = (uint8_t)((EnergyCounter>>8) & 0xff);
  101.         txMsg.Data[3] = (uint8_t)(EnergyCounter & 0xff);
  102.         txMsg.Data[4] = (uint8_t)((Avg4>>8) & 0xff);
  103.         txMsg.Data[5] = (uint8_t)((Avg4) & 0xff);
  104.         txMsg.Data[6] = (uint8_t)((Avg32>>8) & 0xff);
  105.         txMsg.Data[7] = (uint8_t)(Avg32 & 0xff);
  106.         while (StdCan_Put(&txMsg) != StdCan_Ret_OK);
  107.     }
  108. }
  109.  
  110. void sns_power_HandleMessage(StdCan_Msg_t *rxMsg)
  111. {
  112.     if (    StdCan_Ret_class(rxMsg->Header) == CAN_MODULE_CLASS_SNS &&
  113.         StdCan_Ret_direction(rxMsg->Header) == DIRECTIONFLAG_TO_OWNER &&
  114.         rxMsg->Header.ModuleType == CAN_MODULE_TYPE_SNS_POWER &&
  115.         rxMsg->Header.ModuleId == sns_power_ID)
  116.     {
  117.         switch (rxMsg->Header.Command)
  118.         {
  119.         case CAN_MODULE_CMD_GLOBAL_REPORT_INTERVAL:
  120.         if (rxMsg->Length > 0)
  121.         {
  122.             sns_power_ReportInterval = rxMsg->Data[0];
  123.             Timer_SetTimeout(sns_power_SEND_TIMER, sns_power_ReportInterval*1000 , TimerTypeFreeRunning, 0);
  124.         }
  125.         StdCan_Msg_t txMsg;
  126.         StdCan_Set_class(txMsg.Header, CAN_MODULE_CLASS_SNS);
  127.         StdCan_Set_direction(txMsg.Header, DIRECTIONFLAG_FROM_OWNER);
  128.         txMsg.Header.ModuleType = CAN_MODULE_TYPE_SNS_POWER;
  129.         txMsg.Header.ModuleId = sns_power_ID;
  130.         txMsg.Header.Command = CAN_MODULE_CMD_GLOBAL_REPORT_INTERVAL;
  131.         txMsg.Length = 1;
  132.         txMsg.Data[0] = sns_power_ReportInterval;
  133.         StdCan_Put(&txMsg);
  134.         break;
  135.         case CAN_MODULE_CMD_POWER_SETENERGY:
  136.         if (rxMsg->Length = 2)
  137.         {
  138.             EnergyCounter = rxMsg->Data[1];
  139.             EnergyCounter += ((uint16_t)rxMsg->Data[0])<<8;
  140.         }
  141.         break;
  142.         }
  143.     }
  144. }
  145.  
  146. void sns_power_List(uint8_t ModuleSequenceNumber)
  147. {
  148.     StdCan_Msg_t txMsg;
  149.    
  150.     StdCan_Set_class(txMsg.Header, CAN_MODULE_CLASS_SNS); ///TODO: Change this to the actual class type
  151.     StdCan_Set_direction(txMsg.Header, DIRECTIONFLAG_FROM_OWNER);
  152.     txMsg.Header.ModuleType = CAN_MODULE_TYPE_SNS_POWER; ///TODO: Change this to the actual module type
  153.     txMsg.Header.ModuleId = sns_power_ID;
  154.     txMsg.Header.Command = CAN_MODULE_CMD_GLOBAL_LIST;
  155.     txMsg.Length = 6;
  156.  
  157.     txMsg.Data[0] = NODE_HW_ID_BYTE0;
  158.     txMsg.Data[1] = NODE_HW_ID_BYTE1;
  159.     txMsg.Data[2] = NODE_HW_ID_BYTE2;
  160.     txMsg.Data[3] = NODE_HW_ID_BYTE3;
  161.    
  162.     txMsg.Data[4] = NUMBER_OF_MODULES;
  163.     txMsg.Data[5] = ModuleSequenceNumber;
  164.    
  165.     while (StdCan_Put(&txMsg) != StdCan_Ret_OK);
  166. }
  167.