From 1b265c71f00dfdb7f9e42b824a4b9712536743a9 Mon Sep 17 00:00:00 2001 From: Bernhard Date: Sat, 17 Jan 2026 19:14:44 +0100 Subject: [PATCH] Leistungsvorgabe jetzt in Schritten und nicht mehr in Prozent --- can/can_client.c | 43 +++++++++++++++++++++++++------------------ can/can_client.h | 10 +++++----- io/io.c | 8 ++++---- scripts/git_update.sh | 1 + 4 files changed, 35 insertions(+), 27 deletions(-) diff --git a/can/can_client.c b/can/can_client.c index 2e86f4a..f9697f7 100755 --- a/can/can_client.c +++ b/can/can_client.c @@ -41,11 +41,11 @@ int Can_OpenInterface(int iMotorIndex, const char * ifacename) motctrl[iMotorIndex].nDriveReady = 0; motctrl[iMotorIndex].iActualMotorPowerW = 0; motctrl[iMotorIndex].iMotorGear = MOTOR_GEAR_NEUTRAL; - motctrl[iMotorIndex].iMotorPower = 0; - motctrl[iMotorIndex].iMotorPowerPct = 0; + motctrl[iMotorIndex].iMotorPowerRaw = 0; + motctrl[iMotorIndex].iMotorPowerSteps = 0; motctrl[iMotorIndex].nSwitchState = 0; - mylog(LOG_INFO, "CAN: PWR_MIN_PCT=%d PWR_MAX_PCT=%d PWR_STEP=%d", MOTOR_PWR_MIN_PCT, MOTOR_PWR_MAX_PCT, MOTOR_PWR_STEP); + mylog(LOG_INFO, "CAN: PWR_MAX_RAW=%d PWR_STEP_COUNT=%d", MOTOR_PWR_MAX_RAW, MOTOR_PWR_STEP_COUNT); strcpy(intf_data[iMotorIndex].iface_name, ifacename); Can_SetMotorGear(iMotorIndex, MOTOR_GEAR_NEUTRAL); @@ -123,7 +123,7 @@ void Can_SetMotorGear(int iMotorIndex, int iGear) MqttClient_Publish_MotorGear(iMotorIndex, iGear); motctrl[iMotorIndex].iMotorGear = MOTOR_GEAR_FORWARD; // motor is switched to forward -> set min. power - Can_SetMotorPower(iMotorIndex, MOTOR_PWR_MIN_PCT); + Can_SetMotorPower(iMotorIndex, 1); } WriteOutputPin(GPIO_LED_MOTRUN, HIGH); mylog(LOG_INFO, "CAN: Motor[%d]: Set gear forward.", iMotorIndex); @@ -135,7 +135,7 @@ void Can_SetMotorGear(int iMotorIndex, int iGear) MqttClient_Publish_MotorGear(iMotorIndex, iGear); motctrl[iMotorIndex].iMotorGear = MOTOR_GEAR_REVERSE; // motor is switched to reverse -> set min. power - Can_SetMotorPower(iMotorIndex, MOTOR_PWR_MIN_PCT); + Can_SetMotorPower(iMotorIndex, 1); } WriteOutputPin(GPIO_LED_MOTRUN, HIGH); mylog(LOG_INFO, "CAN: Motor[%d]: Set gear reverse.", iMotorIndex); @@ -157,35 +157,42 @@ void Can_SetMotorGear(int iMotorIndex, int iGear) /// @brief Set power for the given motor /// @param iMotorIndex -/// @param iPower (Range: 0..100) +/// @param iPower (Range: 0..MOTOR_PWR_STEP_COUNT) void Can_SetMotorPower(int iMotorIndex, int iPower) { if ((motctrl[iMotorIndex].iMotorGear == MOTOR_GEAR_NEUTRAL) || (motctrl[iMotorIndex].nDriveReady == 0)) { // when motor is neutral or not ready set power to 0 - motctrl[iMotorIndex].iMotorPowerPct = 0; + motctrl[iMotorIndex].iMotorPowerSteps = 0; } - else if (iPower <= MOTOR_PWR_MIN_PCT) + else if (iPower <= 1) { // limit to min. power - motctrl[iMotorIndex].iMotorPowerPct = MOTOR_PWR_MIN_PCT; + motctrl[iMotorIndex].iMotorPowerSteps = 1; } - else if (iPower >= MOTOR_PWR_MAX_PCT) + else if (iPower >= MOTOR_PWR_STEP_COUNT) { // limit to max. power - motctrl[iMotorIndex].iMotorPowerPct = MOTOR_PWR_MAX_PCT; + motctrl[iMotorIndex].iMotorPowerSteps = MOTOR_PWR_STEP_COUNT; } else { - motctrl[iMotorIndex].iMotorPowerPct = iPower; - } - MqttClient_Publish_MotorPower(iMotorIndex, motctrl[iMotorIndex].iMotorPowerPct); + motctrl[iMotorIndex].iMotorPowerSteps = iPower; + } + MqttClient_Publish_MotorPower(iMotorIndex, motctrl[iMotorIndex].iMotorPowerSteps); // calc value for telegram - motctrl[iMotorIndex].iMotorPower = 250 * motctrl[iMotorIndex].iMotorPowerPct / 100; + if (motctrl[iMotorIndex].iMotorPowerSteps <= 0) + { + motctrl[iMotorIndex].iMotorPowerRaw = 0; + } + else + { + motctrl[iMotorIndex].iMotorPowerRaw = (((MOTOR_PWR_MAX_RAW - MOTOR_PWR_MIN_RAW) * motctrl[iMotorIndex].iMotorPowerSteps) / MOTOR_PWR_STEP_COUNT) + MOTOR_PWR_MIN_RAW; + } - mylog(LOG_INFO, "CAN: Motor[%d]: Set power to %d%% -> %d", - iMotorIndex, motctrl[iMotorIndex].iMotorPowerPct, motctrl[iMotorIndex].iMotorPower); + mylog(LOG_INFO, "CAN: Motor[%d]: Set power to %d -> %d", + iMotorIndex, motctrl[iMotorIndex].iMotorPowerSteps, motctrl[iMotorIndex].iMotorPowerRaw); } @@ -226,7 +233,7 @@ void Can_TransmitMotorPower(int iMotorIndex) frame.can_id |= CAN_EFF_FLAG; frame.can_dlc = 8; frame.data[0] = 0xFF; - frame.data[1] = motctrl[iMotorIndex].iMotorPower; // motor power 0 = 0%, 250 = 100% + frame.data[1] = motctrl[iMotorIndex].iMotorPowerRaw; // motor power 0 = 0%, 250 = 100% frame.data[2] = 0xFF; frame.data[3] = 0xFF; frame.data[4] = 0xFF; diff --git a/can/can_client.h b/can/can_client.h index 7a36a7c..65cefab 100755 --- a/can/can_client.h +++ b/can/can_client.h @@ -22,17 +22,17 @@ #define MOTOR_GEAR_NEUTRAL 0x7D #define MOTOR_GEAR_FORWARD 0x7E -#define MOTOR_PWR_MIN_PCT 15 -#define MOTOR_PWR_MAX_PCT 100 -#define MOTOR_PWR_STEP ((MOTOR_PWR_MAX_PCT - MOTOR_PWR_MIN_PCT) / 7) +#define MOTOR_PWR_STEP_COUNT 7 // how many steps to set power +#define MOTOR_PWR_MIN_RAW 38 +#define MOTOR_PWR_MAX_RAW 250 // max. raw power value for motor struct MOTOR_CONTROL_DATA { char nDriveConnected; char nDriveReady; int iMotorGear; - int iMotorPower; - int iMotorPowerPct; + int iMotorPowerRaw; + int iMotorPowerSteps; unsigned char nSwitchState; int iActualMotorPowerW; }; diff --git a/io/io.c b/io/io.c index 5e754dd..24b3df2 100755 --- a/io/io.c +++ b/io/io.c @@ -197,22 +197,22 @@ void IO_DoCyclic() { mylog(LOG_INFO, "IO: KEY-Plus: Start motor."); Can_SetMotorGear(0, 1); - Can_SetMotorPower(0, MOTOR_PWR_MIN_PCT); + Can_SetMotorPower(0, 1); } else { mylog(LOG_INFO, "IO: KEY-Plus: Increase power."); - Can_SetMotorPower(0, motctrl[0].iMotorPowerPct + MOTOR_PWR_STEP); + Can_SetMotorPower(0, motctrl[0].iMotorPowerSteps + 1); } } if (gpioKeyPwrDown.iKeyRisingEdge) { // minus key is pressed -> decrease power - if (motctrl[0].iMotorPowerPct > MOTOR_PWR_MIN_PCT) + if (motctrl[0].iMotorPowerSteps > 1) { mylog(LOG_INFO, "IO: KEY-Minus: Decrease power."); - Can_SetMotorPower(0, motctrl[0].iMotorPowerPct - MOTOR_PWR_STEP); + Can_SetMotorPower(0, motctrl[0].iMotorPowerSteps - 1); } else { diff --git a/scripts/git_update.sh b/scripts/git_update.sh index 7415001..473b781 100755 --- a/scripts/git_update.sh +++ b/scripts/git_update.sh @@ -1,4 +1,5 @@ #!/bin/bash +git fetch git pull