Subversion Repositories HomeAutomation

Rev

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