Pārlūkot izejas kodu

Merge branch 'master' into AW-Regular

Folus Wen 2 gadi atpakaļ
vecāks
revīzija
de477865b3
1 mainītis faili ar 310 papildinājumiem un 3 dzēšanām
  1. 310 3
      EVSE/Modularization/Module_PowerSharing.c

+ 310 - 3
EVSE/Modularization/Module_PowerSharing.c

@@ -6,6 +6,36 @@
  */
 #include "Module_PowerSharing.h"
 
+//-----------------------------------------------------------------------------
+//#define MODPS_FW_VER_TYPE_FORMAL	//V0.XX
+#define MODPS_FW_VER_TYPE_TEST		//T0.XX
+
+#define MODPS_FW_VER_NUM	(1)    //V/T0.01
+#define MODPS_FW_DATE		("20220730")
+
+//-----------------------------------------------------------------------------
+#if (MODPS_FW_VER_NUM >= 1)
+#define MODIFY_MODPS_BALANCE_CHECK_LOOP
+#define FUNC_MODPS_APPEND_LOG
+#endif
+
+//-----------------------------------------------------------------------------
+
+char Version_And_Date[2][10] = { 0 };
+
+void Init_FW_Info(void)
+{
+#ifdef MODPS_FW_VER_TYPE_FORMAL
+    sprintf(Version_And_Date[0], "V%4.2f", MODPS_FW_VER_NUM * 0.01);
+#endif
+
+#ifdef MODPS_FW_VER_TYPE_TEST
+    sprintf(Version_And_Date[0], "T%4.2f", MODPS_FW_VER_NUM * 0.01);
+#endif
+
+    sprintf(Version_And_Date[1], "%s", MODPS_FW_DATE);
+}
+
 struct SysConfigAndInfo		*ShmSysConfigAndInfo;
 struct StatusCodeData 		*ShmStatusCodeData;
 struct OCPP16Data			*ShmOCPP16Data;
@@ -735,18 +765,18 @@ int conn_update_status(int socketFd, Connector_Info *connectorInfo, uint8_t conn
 int conn_getOnHandCurrent(void)
 {
 	int result = 0;
-
 	for(uint8_t idx=0;idx<CONNECTION_LIMIT;idx++)
 	{
 		if(ShmPowerSharing->Connection_Info[idx].isSocketConnected)
 		{
 			for(uint8_t gun_index=0;gun_index<ShmPowerSharing->Connection_Info[idx].connectorCount;gun_index++)
 			{
-				result += (ShmPowerSharing->Connection_Info[idx].connectorInfo[gun_index].connectorType == CONNECTOR_TYPE_AC_THREE?ShmPowerSharing->Connection_Info[idx].connectorInfo[gun_index].availableSharingCurrent*3:ShmPowerSharing->Connection_Info[idx].connectorInfo[gun_index].availableSharingCurrent);
+				result += (ShmPowerSharing->Connection_Info[idx].connectorInfo[gun_index].connectorType == CONNECTOR_TYPE_AC_THREE?
+						ShmPowerSharing->Connection_Info[idx].connectorInfo[gun_index].availableSharingCurrent*3:
+						ShmPowerSharing->Connection_Info[idx].connectorInfo[gun_index].availableSharingCurrent);
 			}
 		}
 	}
-
 	result = checkChargingProfileLimit() - result;
 
 	if(ShmPowerSharing->onHandCurrent != result)
@@ -819,6 +849,11 @@ int tcpSocketServer(void)
 	serverInfo.sin_addr.s_addr = htonl(INADDR_ANY);
 	serverInfo.sin_port = htons(LISTEN_PORT_TCP);
 
+
+#ifdef FUNC_MODULE_POWER_SHARING_APPEND_LOG
+	DEBUG_INFO("==========[ tcpSocketServer begin ]==========\n");
+#endif
+
 	if(bind(sockFd, (struct sockaddr *)&serverInfo, sizeof(serverInfo)) < 0)
 		DEBUG_ERROR("TCP server socket bind fail.\n");
 
@@ -1240,6 +1275,272 @@ int tcpSocketClient(void)
 //==========================================
 // Local loading balance check
 //==========================================
+#ifdef MODIFY_MODPS_BALANCE_CHECK_LOOP
+
+Connector_Info* GetIsetMaxGun(void)
+{
+	struct CONNECTION_INFO* pDev = NULL;
+	Connector_Info* pGun = NULL;
+	uint16_t* pIset = NULL;
+	uint16_t* pIout = NULL;
+	uint16_t Imin = 0;
+	uint16_t IsetMax = 6;
+	Connector_Info* pGunIsetMax = NULL;
+
+	for (uint8_t i = 0; i < CONNECTION_LIMIT; i++)
+	{
+		pDev = &ShmPowerSharing->Connection_Info[i];
+		if (!pDev->isSocketConnected)
+		{
+			continue;
+		}
+		for (uint8_t j = 0; j < pDev->connectorCount; j++)
+		{
+			pGun = &pDev->connectorInfo[j];
+			if (pGun->isGunConnected)
+			{
+				pIset = &pGun->availableSharingCurrent;
+				pIout = &pGun->presentOutputCurrent;
+				Imin = pGun->connectorType==CONNECTOR_TYPE_DC ? SHARE_MIN_DC : SHARE_MIN_AC;
+				if (*pIset > Imin && *pIout > Imin && *pIset > IsetMax)
+				{
+					IsetMax = *pIset;
+					pGunIsetMax = pGun;
+				}
+			}
+		}
+	}
+
+//	if (pGunIsetMax)
+//	{
+//		DEBUG_INFO("GunIsetMax: %d / %d (%d)\n", pGunIsetMax->presentOutputCurrent,
+//				pGunIsetMax->availableSharingCurrent, pGunIsetMax->connectorType);
+//	}
+	return pGunIsetMax;
+}
+
+Connector_Info* GetIsetMinGun(void)
+{
+	struct CONNECTION_INFO* pDev = NULL;
+	Connector_Info* pGun = NULL;
+	uint16_t* pIset = NULL;
+	uint16_t* pIout = NULL;
+	uint16_t Imin = 0;
+	uint16_t IsetMin = 1000;
+	Connector_Info* pGunIsetMin = NULL;
+
+	for (uint8_t i = 0; i < CONNECTION_LIMIT; i++)
+	{
+		pDev = &ShmPowerSharing->Connection_Info[i];
+		if (!pDev->isSocketConnected)
+		{
+			continue;
+		}
+		for (uint8_t j = 0; j < pDev->connectorCount; j++)
+		{
+			pGun = &pDev->connectorInfo[j];
+			if (pGun->isGunConnected)
+			{
+				pIset = &pGun->availableSharingCurrent;
+				pIout = &pGun->presentOutputCurrent;
+				Imin = pGun->connectorType==CONNECTOR_TYPE_DC ? SHARE_MIN_DC : SHARE_MIN_AC;
+				if (*pIset >= Imin && *pIout >= Imin && *pIset < IsetMin)
+				{
+					IsetMin = *pIset;
+					pGunIsetMin = pGun;
+				}
+			}
+		}
+	}
+
+//	if (pGunIsetMin)
+//	{
+//		DEBUG_INFO("GunIsetMin: %d / %d (%d)\n", pGunIsetMin->presentOutputCurrent,
+//				pGunIsetMin->availableSharingCurrent, pGunIsetMin->connectorType);
+//	}
+	return pGunIsetMin;
+}
+
+int balance_check_loop(void)
+{
+	DEBUG_INFO("==========[ balance_check_loop begin ]==========\n");
+	for(;;)
+	{
+		// Get connection info
+		conn_getConectedQuantity();
+		conn_getOnHandCurrent();
+		conn_getConectedConnector();
+
+		// Check conn heart beat
+		for(uint8_t idx=0;idx<CONNECTION_LIMIT;idx++)
+		{
+			if(ShmPowerSharing->Connection_Info[idx].isSocketConnected &&
+			   (getDiffSecNow(ShmPowerSharing->Connection_Info[idx].timer[POWERSHARING_TMR_IDX_HEARTBEAT]) > TIMEOUT_SPEC_HEARTBEAT))
+			{
+				DEBUG_INFO("SocketFd-%d heart beat is over %d seconds.\n", ShmPowerSharing->Connection_Info[idx].socketFd, TIMEOUT_SPEC_HEARTBEAT);
+				for(uint8_t gun_index=0;gun_index<ShmPowerSharing->Connection_Info[idx].connectorCount;gun_index++)
+					ShmPowerSharing->Connection_Info[idx].connectorInfo[gun_index].isGunConnected = FALSE;
+				ShmPowerSharing->Connection_Info[idx].isSocketConnected = FALSE;
+			}
+		}
+
+		// Check available power
+		if(ShmPowerSharing->isDetectNewConnected || (ShmPowerSharing->onHandCurrent < 0))
+		{
+			for(uint8_t idx=0;idx<CONNECTION_LIMIT;idx++)
+			{
+				for(uint8_t gun_index=0;gun_index<ShmPowerSharing->Connection_Info[idx].connectorCount;gun_index++)
+				{
+					if(ShmPowerSharing->Connection_Info[idx].isSocketConnected &&
+					   ShmPowerSharing->Connection_Info[idx].connectorInfo[gun_index].isGunConnected)
+					{
+						ShmPowerSharing->Connection_Info[idx].connectorInfo[gun_index].availableSharingCurrent = (ShmPowerSharing->Connection_Info[idx].connectorInfo[gun_index].connectorType==CONNECTOR_TYPE_DC?SHARE_MIN_DC:SHARE_MIN_AC);
+					}
+					else
+					{
+						ShmPowerSharing->Connection_Info[idx].connectorInfo[gun_index].availableSharingCurrent = 0;
+					}
+				}
+			}
+
+			if(ShmPowerSharing->onHandCurrent < 0)
+				DEBUG_INFO("On hand current < 0 re-allocate available current to each connection.\n");
+
+			if(ShmPowerSharing->isDetectNewConnected)
+			{
+				DEBUG_INFO("Detect gun connected re-allocate available current to each connection.\n");
+				ShmPowerSharing->isDetectNewConnected = NO;
+			}
+		}
+
+		struct CONNECTION_INFO* pDev = NULL;
+		Connector_Info* pGun = NULL;
+		uint16_t* pIset = NULL;
+		uint16_t* pIout = NULL;
+		uint16_t Imin = 0;
+
+		for(uint8_t idx = 0; idx < CONNECTION_LIMIT; idx++)
+		{
+			pDev = &ShmPowerSharing->Connection_Info[idx];
+			for(uint8_t gun_index = 0; gun_index < pDev->connectorCount; gun_index++)
+			{
+				pGun = &pDev->connectorInfo[gun_index];
+				pIset = &pGun->availableSharingCurrent;
+				pIout = &pGun->presentOutputCurrent;
+				Imin = (pGun->connectorType == CONNECTOR_TYPE_DC ? SHARE_MIN_DC : SHARE_MIN_AC);
+
+				if(pDev->isSocketConnected && pGun->isGunConnected)
+				{
+					if((getDiffSecNow(pGun->tmrCheckCapacity) >= INTERVAL_SPEC_CHECK_CAPACITY))
+					{
+						int16_t Idiff = *pIset - *pIout;
+						if(Idiff > 3)
+						{
+							int32_t IsetAdd = - (Idiff >> 1);
+							uint16_t IsetNext = *pIset + IsetAdd;
+							if(IsetNext >= Imin)
+								*pIset = IsetNext;
+							else
+								*pIset = Imin;
+						}
+						else if((abs(Idiff) <= 1) && (ShmPowerSharing->onHandCurrent > 0))
+						{
+							int AvgCurr = ShmPowerSharing->onHandCurrent /
+								(ShmPowerSharing->connectedConnectorQty == 0 ? 1 : ShmPowerSharing->connectedConnectorQty);
+							*pIset += (pGun->connectorType == CONNECTOR_TYPE_AC_THREE ? AvgCurr / 3 : AvgCurr);
+						}
+						else
+						{
+						}
+
+						Connector_Info* pGunIsetMax = GetIsetMaxGun();
+						Connector_Info* pGunIsetMin = GetIsetMinGun();
+						if (pGun == pGunIsetMax && pGunIsetMax == pGunIsetMin)
+						{
+							if (pGun->connectorType == CONNECTOR_TYPE_AC_THREE)
+							{
+								if (ShmPowerSharing->onHandCurrent >= 3)
+								{
+									*pIset += 1;
+									conn_getOnHandCurrent();
+								}
+							}
+							else
+							{
+								for (uint8_t i = 1; i <= 1; i++)
+								{
+									if (ShmPowerSharing->onHandCurrent > 0)
+									{
+										*pIset += 1;
+										conn_getOnHandCurrent();
+									}
+									else
+									{
+										break;
+									}
+								}
+							}
+						}
+						else if (pGun == pGunIsetMin)
+						{
+							if (pGun->connectorType == CONNECTOR_TYPE_AC_THREE)
+							{
+								if (ShmPowerSharing->onHandCurrent >= 3)
+								{
+									*pIset += 1;
+									conn_getOnHandCurrent();
+								}
+							}
+							else
+							{
+								for (uint8_t i = 1; i <= 1; i++)
+								{
+									if (ShmPowerSharing->onHandCurrent > 0)
+									{
+										*pIset += 1;
+										conn_getOnHandCurrent();
+									}
+									else
+									{
+										break;
+									}
+								}
+							}
+						}
+						else if (pGun == pGunIsetMax)
+						{
+							if (pGun->connectorType == CONNECTOR_TYPE_AC_THREE)
+							{
+								*pIset -= 1;
+								conn_getOnHandCurrent();
+							}
+							else
+							{
+								*pIset -= 1;
+								conn_getOnHandCurrent();
+							}
+						}
+						refreshStartTimer(&ShmPowerSharing->Connection_Info[idx].connectorInfo[gun_index].tmrCheckCapacity);
+					}
+				}
+				else
+				{
+					if(*pIset != 0)
+					{
+						DEBUG_INFO("Dupfd-%d on conn-%d available current reset to 0A\n", ShmPowerSharing->Connection_Info[idx].socketFd, idx);
+					}
+					*pIset = 0;
+				}
+			}
+		}
+
+		usleep(100000);
+	}
+
+	return FAIL;
+}
+#else //MODIFY_MODPS_BALANCE_CHECK_LOOP
+
 int balance_check_loop(void)
 {
 	for(;;)
@@ -1334,6 +1635,7 @@ int balance_check_loop(void)
 
 	return FAIL;
 }
+#endif //MODIFY_MODPS_BALANCE_CHECK_LOOP
 
 //==========================================
 // Main process
@@ -1355,6 +1657,11 @@ int main(void)
 		return 0;
 	}
 
+	Init_FW_Info();
+	DEBUG_INFO("\n\n");
+	DEBUG_INFO("Latest Firmware Version : [ %s ]\n", Version_And_Date[0]);
+	DEBUG_INFO("Latest Upgrade Date : [ %s ]\n", Version_And_Date[1]);
+
 	// Enable server if rotary switch not slave mode
 	if(ShmSysConfigAndInfo->SysConfig.isEnableLocalPowerSharing == 1)
 	{