Subversion Repositories HomeAutomation

Rev

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