Subversion Repositories HomeAutomation

Rev

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