Optimierung Systemhochlauf

This commit is contained in:
Bernhard
2025-12-12 18:50:18 +01:00
parent fbff41de71
commit bd6e3bbfe9
6 changed files with 69 additions and 42 deletions

View File

@@ -6,6 +6,19 @@
struct MOTOR_CONTROL_DATA motctrl[MOTOR_COUNT]; struct MOTOR_CONTROL_DATA motctrl[MOTOR_COUNT];
struct CAN_INTERFACE_DATA intf_data[MOTOR_COUNT]; struct CAN_INTERFACE_DATA intf_data[MOTOR_COUNT];
int iBusTimeoutCounter = 0;
void IncBusTimeoutCounter(int iMotorIndex)
{
if (iBusTimeoutCounter < 2000)
{
iBusTimeoutCounter++;
if (iBusTimeoutCounter >= 2000)
{
motctrl[iMotorIndex].nSwitchState = 0;
MqttClient_Publish_MotorSwitchState(iMotorIndex, motctrl[iMotorIndex].nSwitchState);
}
}
}
/// @brief Open socket of CAN interface for the given motor /// @brief Open socket of CAN interface for the given motor
/// @param iMotorIndex /// @param iMotorIndex
/// @param ifacename /// @param ifacename
@@ -152,24 +165,27 @@ void Can_SetMotorPower(int iMotorIndex, int iPower)
/// @param iMotorIndex /// @param iMotorIndex
void Can_TransmitMotorGear(int iMotorIndex) void Can_TransmitMotorGear(int iMotorIndex)
{ {
// Transmission rate: 100ms //if (motctrl[iMotorIndex].nSwitchState > 0)
struct can_frame frame;
frame.can_id = 0x18F005D0;
frame.can_id |= CAN_EFF_FLAG;
frame.can_dlc = 8;
frame.data[0] = motctrl[iMotorIndex].iMotorGear;
frame.data[1] = 0xFF;
frame.data[2] = 0xFF;
frame.data[3] = 0xFF;
frame.data[4] = 0xFF;
frame.data[5] = 0xFF;
frame.data[6] = 0xFF;
frame.data[7] = 0xFF;
if (write(intf_data[iMotorIndex].socket, &frame, sizeof(frame)) != sizeof(frame))
{ {
// Transmission rate: 100ms
struct can_frame frame;
frame.can_id = 0x18F005D0;
frame.can_id |= CAN_EFF_FLAG;
frame.can_dlc = 8;
frame.data[0] = motctrl[iMotorIndex].iMotorGear;
frame.data[1] = 0xFF;
frame.data[2] = 0xFF;
frame.data[3] = 0xFF;
frame.data[4] = 0xFF;
frame.data[5] = 0xFF;
frame.data[6] = 0xFF;
frame.data[7] = 0xFF;
if (write(intf_data[iMotorIndex].socket, &frame, sizeof(frame)) != sizeof(frame))
{
}
} }
} }
@@ -178,24 +194,27 @@ void Can_TransmitMotorGear(int iMotorIndex)
/// @param iMotorIndex /// @param iMotorIndex
void Can_TransmitMotorPower(int iMotorIndex) void Can_TransmitMotorPower(int iMotorIndex)
{ {
// Transmission rate: 50ms //if (motctrl[iMotorIndex].nSwitchState > 0)
struct can_frame frame;
frame.can_id = 0x0CF003D0;
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[2] = 0xFF;
frame.data[3] = 0xFF;
frame.data[4] = 0xFF;
frame.data[5] = 0xFF;
frame.data[6] = 0xFF;
frame.data[7] = 0xFF;
if (write(intf_data[iMotorIndex].socket, &frame, sizeof(frame)) != sizeof(frame))
{ {
// Transmission rate: 50ms
struct can_frame frame;
frame.can_id = 0x0CF003D0;
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[2] = 0xFF;
frame.data[3] = 0xFF;
frame.data[4] = 0xFF;
frame.data[5] = 0xFF;
frame.data[6] = 0xFF;
frame.data[7] = 0xFF;
if (write(intf_data[iMotorIndex].socket, &frame, sizeof(frame)) != sizeof(frame))
{
}
} }
} }
@@ -207,6 +226,8 @@ void Can_ReadData(int iMotorIndex)
ssize_t nbytes = 0; ssize_t nbytes = 0;
struct can_frame frame; struct can_frame frame;
IncBusTimeoutCounter(iMotorIndex);
// wir wollen immer alle verfügbaren Frames lesen // wir wollen immer alle verfügbaren Frames lesen
while ((nbytes = read(intf_data[iMotorIndex].socket, &frame, sizeof(frame))) > 0) while ((nbytes = read(intf_data[iMotorIndex].socket, &frame, sizeof(frame))) > 0)
{ {
@@ -239,7 +260,8 @@ void Can_ReadData(int iMotorIndex)
/// @param frame /// @param frame
void Can_Read_Manu_PGN(int iMotorIndex, struct can_frame *frame) void Can_Read_Manu_PGN(int iMotorIndex, struct can_frame *frame)
{ {
motctrl[iMotorIndex].nSwitchState = frame->data[2]; iBusTimeoutCounter = 0;
motctrl[iMotorIndex].nSwitchState = frame->data[4];
MqttClient_Publish_MotorSwitchState(iMotorIndex, motctrl[iMotorIndex].nSwitchState); MqttClient_Publish_MotorSwitchState(iMotorIndex, motctrl[iMotorIndex].nSwitchState);
} }
@@ -247,6 +269,6 @@ void Can_Read_Manu_PGN(int iMotorIndex, struct can_frame *frame)
/// @param frame /// @param frame
void Can_Read_Manu_PGN2(int iMotorIndex, struct can_frame *frame) void Can_Read_Manu_PGN2(int iMotorIndex, struct can_frame *frame)
{ {
motctrl[iMotorIndex].iActualMotorPowerW = (frame->data[3] << 16) | (frame->data[4] << 8) | frame->data[5]; motctrl[iMotorIndex].iActualMotorPowerW = (frame->data[6] << 16) | (frame->data[5] << 8) | frame->data[4];
MqttClient_Publish_MotorActualPowerW(iMotorIndex, motctrl[iMotorIndex].iActualMotorPowerW); MqttClient_Publish_MotorActualPowerW(iMotorIndex, motctrl[iMotorIndex].iActualMotorPowerW);
} }

View File

@@ -21,9 +21,9 @@
#define MOTOR_GEAR_NEUTRAL 0x7D #define MOTOR_GEAR_NEUTRAL 0x7D
#define MOTOR_GEAR_FORWARD 0x7E #define MOTOR_GEAR_FORWARD 0x7E
#define MOTOR_PWR_MIN_PCT 5 #define MOTOR_PWR_MIN_PCT 15
#define MOTOR_PWR_MAX_PCT 100 #define MOTOR_PWR_MAX_PCT 100
#define MOTOR_PWR_STEP 14 #define MOTOR_PWR_STEP 12
struct MOTOR_CONTROL_DATA struct MOTOR_CONTROL_DATA
{ {

4
main.c
View File

@@ -130,7 +130,7 @@ void *thread_func(void *data)
periodic_task_init(1, &pinfo); periodic_task_init(1, &pinfo);
// "Zündung" ein // "Zündung" ein
WriteOutputPin(GPIO_OUT_PWRON, 1); WriteOutputPin(GPIO_OUT_PWRON, HIGH);
// cyclic call of do_cyclic_1ms() // cyclic call of do_cyclic_1ms()
while (iThreadControl == 0) while (iThreadControl == 0)
@@ -146,7 +146,7 @@ void *thread_func(void *data)
} }
// "Zündung" aus // "Zündung" aus
WriteOutputPin(GPIO_OUT_PWRON, 0); WriteOutputPin(GPIO_OUT_PWRON, LOW);
// Disconnect from mqtt broker // Disconnect from mqtt broker
MqttClient_Close(); MqttClient_Close();

View File

@@ -297,7 +297,7 @@ void MqttClient_Publish_MotorSwitchState(int iMotorIndex, unsigned char nSwitchS
{ {
nMqttMotor1SwitchState = nSwitchState; nMqttMotor1SwitchState = nSwitchState;
char message[100]; char message[100];
snprintf(message, sizeof(message), "%d", nSwitchState); snprintf(message, sizeof(message), "%2X", nSwitchState);
mosquitto_publish(mosq, NULL, mqtt_topic_motor1_switchstate, strlen(message), message, 0, false); mosquitto_publish(mosq, NULL, mqtt_topic_motor1_switchstate, strlen(message), message, 0, false);
} }
} }
@@ -307,7 +307,7 @@ void MqttClient_Publish_MotorSwitchState(int iMotorIndex, unsigned char nSwitchS
{ {
nMqttMotor2SwitchState = nSwitchState; nMqttMotor2SwitchState = nSwitchState;
char message[100]; char message[100];
snprintf(message, sizeof(message), "%d", nSwitchState); snprintf(message, sizeof(message), "%2X", nSwitchState);
mosquitto_publish(mosq, NULL, mqtt_topic_motor2_switchstate, strlen(message), message, 0, false); mosquitto_publish(mosq, NULL, mqtt_topic_motor2_switchstate, strlen(message), message, 0, false);
} }
} }

View File

@@ -2,8 +2,9 @@
## Datei gehört in /etc/systemd/system ## Datei gehört in /etc/systemd/system
## aktivieren dann mit: ## aktivieren dann mit:
## - sudo systemctl daemon-reload ## - sudo systemctl daemon-reload
## - sudo systemctl enable CanRtDriver.service ## - sudo systemctl enable CanRtDriver.service NICHT AUSFÜHREN!!!
## - sudo systemctl start CanRtDriver.service ## - sudo systemctl start CanRtDriver.service NICHT AUSFÜHREN!!!
## => Service wird über ./scripts/can_link_up.sh gestartet
## ##
## Device Unit ermitteln mit: systemctl --type=device | grep can0 ## Device Unit ermitteln mit: systemctl --type=device | grep can0
## Ggf. unter 'Requires', 'After' und 'BindsTo' anpassen! ## Ggf. unter 'Requires', 'After' und 'BindsTo' anpassen!

View File

@@ -2,6 +2,10 @@
ip link set can0 up type can bitrate 250000 ip link set can0 up type can bitrate 250000
ip link set can1 up type can bitrate 250000 ip link set can1 up type can bitrate 250000
sleep 5
ifconfig can0 txqueuelen 65536 ifconfig can0 txqueuelen 65536
ifconfig can1 txqueuelen 65536 ifconfig can1 txqueuelen 65536
sleep 10
systemctl start CanRtDriver