Compare commits
8 Commits
c16ac482bd
...
master
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
c4e21dca22 | ||
|
|
450e93982e | ||
|
|
4bde0cc888 | ||
|
|
281e70d91e | ||
|
|
874ebf51ba | ||
|
|
b5f7b5a45d | ||
|
|
f91aee06c7 | ||
|
|
1b265c71f0 |
277
can/can_client.c
277
can/can_client.c
@@ -1,12 +1,13 @@
|
||||
|
||||
#include "main.h"
|
||||
|
||||
#include <settings/settings.h>
|
||||
#include <can/can_client.h>
|
||||
#include <io/io.h>
|
||||
#include <mqtt/mqtt_client.h>
|
||||
|
||||
struct MOTOR_CONTROL_DATA motctrl[MOTOR_COUNT];
|
||||
struct CAN_INTERFACE_DATA intf_data[MOTOR_COUNT];
|
||||
struct MOTOR_CONTROL_DATA motctrl[MAX_MOTOR_COUNT];
|
||||
struct CAN_INTERFACE_DATA intf_data[MAX_MOTOR_COUNT];
|
||||
|
||||
|
||||
int iBusTimeoutCounter = 0;
|
||||
@@ -37,60 +38,72 @@ void IncBusTimeoutCounter(int iMotorIndex)
|
||||
int Can_OpenInterface(int iMotorIndex, const char * ifacename)
|
||||
{
|
||||
// Init control data
|
||||
motctrl[iMotorIndex].nDriveConnected = 0;
|
||||
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;
|
||||
motctrl[iMotorIndex].nDriveConnected = 0;
|
||||
motctrl[iMotorIndex].nDriveReady = 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_MIN_RAW=%d PWR_MAX_RAW=%d PWR_STEP_COUNT=%d",
|
||||
settings.iMotorPwrMinRaw, settings.iMotorPwrMaxRaw, settings.iMotorPwrStepCount);
|
||||
|
||||
strcpy(intf_data[iMotorIndex].iface_name, ifacename);
|
||||
Can_SetMotorGear(iMotorIndex, MOTOR_GEAR_NEUTRAL);
|
||||
Can_SetMotorPower(iMotorIndex, 0);
|
||||
|
||||
// first we have to create a socket
|
||||
if ((intf_data[iMotorIndex].socket = socket(PF_CAN, SOCK_RAW, CAN_RAW)) < 0)
|
||||
if (settings.iCanSimu)
|
||||
{
|
||||
mylog(LOG_ERR, "CAN: Could not create socket for motor %d!", iMotorIndex);
|
||||
return 1;
|
||||
mylog(LOG_WARNING, "CAN: Using simulation mode (motor %d).", iMotorIndex);
|
||||
intf_data[iMotorIndex].socket = -1;
|
||||
motctrl[iMotorIndex].nSwitchState = 0xF5;
|
||||
motctrl[iMotorIndex].nDriveConnected = 1;
|
||||
motctrl[iMotorIndex].nDriveReady = 1;
|
||||
}
|
||||
else
|
||||
{
|
||||
// first we have to create a socket
|
||||
if ((intf_data[iMotorIndex].socket = socket(PF_CAN, SOCK_RAW, CAN_RAW)) < 0)
|
||||
{
|
||||
mylog(LOG_ERR, "CAN: Could not create socket for motor %d!", iMotorIndex);
|
||||
return 1;
|
||||
}
|
||||
|
||||
// retrieve the interface index
|
||||
strcpy(intf_data[iMotorIndex].ifr.ifr_name, intf_data[iMotorIndex].iface_name);
|
||||
if (ioctl(intf_data[iMotorIndex].socket, SIOCGIFINDEX, &intf_data[iMotorIndex].ifr) < 0)
|
||||
{
|
||||
mylog(LOG_ERR, "CAN: Could not get interface index for motor %d!", iMotorIndex);
|
||||
return 2;
|
||||
}
|
||||
// retrieve the interface index
|
||||
strcpy(intf_data[iMotorIndex].ifr.ifr_name, intf_data[iMotorIndex].iface_name);
|
||||
if (ioctl(intf_data[iMotorIndex].socket, SIOCGIFINDEX, &intf_data[iMotorIndex].ifr) < 0)
|
||||
{
|
||||
mylog(LOG_ERR, "CAN: Could not get interface index for motor %d!", iMotorIndex);
|
||||
return 2;
|
||||
}
|
||||
|
||||
// bind the socket to the CAN interface
|
||||
memset(&intf_data[iMotorIndex].addr, 0, sizeof(intf_data[iMotorIndex].addr));
|
||||
intf_data[iMotorIndex].addr.can_family = AF_CAN;
|
||||
intf_data[iMotorIndex].addr.can_ifindex = intf_data[iMotorIndex].ifr.ifr_ifindex;
|
||||
if (bind(intf_data[iMotorIndex].socket, (struct sockaddr *)&intf_data[iMotorIndex].addr, sizeof(intf_data[iMotorIndex].addr)) < 0)
|
||||
{
|
||||
mylog(LOG_ERR, "CAN: Could not bind socket to inteface for motor %d!", iMotorIndex);
|
||||
return 3;
|
||||
}
|
||||
// bind the socket to the CAN interface
|
||||
memset(&intf_data[iMotorIndex].addr, 0, sizeof(intf_data[iMotorIndex].addr));
|
||||
intf_data[iMotorIndex].addr.can_family = AF_CAN;
|
||||
intf_data[iMotorIndex].addr.can_ifindex = intf_data[iMotorIndex].ifr.ifr_ifindex;
|
||||
if (bind(intf_data[iMotorIndex].socket, (struct sockaddr *)&intf_data[iMotorIndex].addr, sizeof(intf_data[iMotorIndex].addr)) < 0)
|
||||
{
|
||||
mylog(LOG_ERR, "CAN: Could not bind socket to inteface for motor %d!", iMotorIndex);
|
||||
return 3;
|
||||
}
|
||||
|
||||
// make socket to nonblocking
|
||||
int fcntl_flags = fcntl(intf_data[iMotorIndex].socket, F_GETFL, 0);
|
||||
if (fcntl_flags < 0)
|
||||
{
|
||||
mylog(LOG_ERR, "CAN: Could not get file descriptor flags!");
|
||||
return 4;
|
||||
}
|
||||
fcntl_flags |= O_NONBLOCK;
|
||||
if (fcntl(intf_data[iMotorIndex].socket, F_SETFL, fcntl_flags) < 0)
|
||||
{
|
||||
mylog(LOG_ERR, "CAN: Could not set file descriptor flags (set socket none-blocking)!");
|
||||
return 5;
|
||||
}
|
||||
// make socket to nonblocking
|
||||
int fcntl_flags = fcntl(intf_data[iMotorIndex].socket, F_GETFL, 0);
|
||||
if (fcntl_flags < 0)
|
||||
{
|
||||
mylog(LOG_ERR, "CAN: Could not get file descriptor flags!");
|
||||
return 4;
|
||||
}
|
||||
fcntl_flags |= O_NONBLOCK;
|
||||
if (fcntl(intf_data[iMotorIndex].socket, F_SETFL, fcntl_flags) < 0)
|
||||
{
|
||||
mylog(LOG_ERR, "CAN: Could not set file descriptor flags (set socket none-blocking)!");
|
||||
return 5;
|
||||
}
|
||||
|
||||
mylog(LOG_INFO, "CAN: Interface %s (motor %d) opened!", ifacename, iMotorIndex);
|
||||
mylog(LOG_INFO, "CAN: Interface %s (motor %d) opened!", ifacename, iMotorIndex);
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
@@ -100,13 +113,20 @@ int Can_OpenInterface(int iMotorIndex, const char * ifacename)
|
||||
/// @param iMotorIndex
|
||||
void Can_CloseInterface(int iMotorIndex)
|
||||
{
|
||||
if (close(intf_data[iMotorIndex].socket) < 0)
|
||||
if (intf_data[iMotorIndex].socket >= 0)
|
||||
{
|
||||
mylog(LOG_ERR, "CAN: Could not close socket of motor %d!", iMotorIndex);
|
||||
if (close(intf_data[iMotorIndex].socket) < 0)
|
||||
{
|
||||
mylog(LOG_ERR, "CAN: Could not close socket of motor %d!", iMotorIndex);
|
||||
}
|
||||
else
|
||||
{
|
||||
mylog(LOG_INFO, "CAN: Interface %s (motor %d) closed.", intf_data[iMotorIndex].iface_name, iMotorIndex);
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
mylog(LOG_INFO, "CAN: Interface %s (motor %d) closed.", intf_data[iMotorIndex].iface_name, iMotorIndex);
|
||||
mylog(LOG_INFO, "CAN: Close simulation mode.");
|
||||
}
|
||||
}
|
||||
|
||||
@@ -120,10 +140,9 @@ void Can_SetMotorGear(int iMotorIndex, int iGear)
|
||||
{
|
||||
if (motctrl[iMotorIndex].iMotorGear != MOTOR_GEAR_FORWARD)
|
||||
{
|
||||
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);
|
||||
@@ -132,10 +151,9 @@ void Can_SetMotorGear(int iMotorIndex, int iGear)
|
||||
{
|
||||
if (motctrl[iMotorIndex].iMotorGear != MOTOR_GEAR_REVERSE)
|
||||
{
|
||||
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);
|
||||
@@ -144,7 +162,6 @@ void Can_SetMotorGear(int iMotorIndex, int iGear)
|
||||
{
|
||||
if (motctrl[iMotorIndex].iMotorGear != MOTOR_GEAR_NEUTRAL)
|
||||
{
|
||||
MqttClient_Publish_MotorGear(iMotorIndex, iGear);
|
||||
motctrl[iMotorIndex].iMotorGear = MOTOR_GEAR_NEUTRAL;
|
||||
}
|
||||
// motor is switch to neutral -> set power to 0
|
||||
@@ -152,40 +169,57 @@ void Can_SetMotorGear(int iMotorIndex, int iGear)
|
||||
WriteOutputPin(GPIO_LED_MOTRUN, LOW);
|
||||
mylog(LOG_INFO, "CAN: Motor[%d]: Set gear neutral.", iMotorIndex);
|
||||
}
|
||||
MqttClient_Publish_MotorGear(iMotorIndex, iGear);
|
||||
}
|
||||
|
||||
|
||||
/// @brief Set power for the given motor
|
||||
/// @param iMotorIndex
|
||||
/// @param iPower (Range: 0..100)
|
||||
/// @param iPower (Range: 0..settings.iMotorPwrStepCount)
|
||||
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 >= settings.iMotorPwrStepCount)
|
||||
{
|
||||
// limit to max. power
|
||||
motctrl[iMotorIndex].iMotorPowerPct = MOTOR_PWR_MAX_PCT;
|
||||
motctrl[iMotorIndex].iMotorPowerSteps = settings.iMotorPwrStepCount;
|
||||
}
|
||||
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
|
||||
{
|
||||
// Scale motor power from steps to raw value
|
||||
motctrl[iMotorIndex].iMotorPowerRaw = (((settings.iMotorPwrMaxRaw - settings.iMotorPwrMinRaw) * (motctrl[iMotorIndex].iMotorPowerSteps - 1)) / (settings.iMotorPwrStepCount - 1)) + settings.iMotorPwrMinRaw;
|
||||
if (motctrl[iMotorIndex].iMotorPowerRaw < settings.iMotorPwrMinRaw)
|
||||
{
|
||||
motctrl[iMotorIndex].iMotorPowerRaw = settings.iMotorPwrMinRaw;
|
||||
}
|
||||
else if (motctrl[iMotorIndex].iMotorPowerRaw > settings.iMotorPwrMaxRaw)
|
||||
{
|
||||
motctrl[iMotorIndex].iMotorPowerRaw = settings.iMotorPwrMaxRaw;
|
||||
}
|
||||
}
|
||||
|
||||
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);
|
||||
}
|
||||
|
||||
|
||||
@@ -194,23 +228,26 @@ void Can_SetMotorPower(int iMotorIndex, int iPower)
|
||||
void Can_TransmitMotorGear(int iMotorIndex)
|
||||
{
|
||||
// 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))
|
||||
if (intf_data[iMotorIndex].socket >= 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))
|
||||
{
|
||||
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -220,23 +257,26 @@ void Can_TransmitMotorGear(int iMotorIndex)
|
||||
void Can_TransmitMotorPower(int iMotorIndex)
|
||||
{
|
||||
// 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))
|
||||
if (intf_data[iMotorIndex].socket >= 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].iMotorPowerRaw; // 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))
|
||||
{
|
||||
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -245,39 +285,42 @@ void Can_TransmitMotorPower(int iMotorIndex)
|
||||
/// @param iMotorIndex
|
||||
void Can_ReadData(int iMotorIndex)
|
||||
{
|
||||
ssize_t nbytes = 0;
|
||||
struct can_frame frame;
|
||||
|
||||
// increment cycle counter
|
||||
IncBusTimeoutCounter(iMotorIndex);
|
||||
|
||||
// read one frame
|
||||
if ((nbytes = read(intf_data[iMotorIndex].socket, &frame, sizeof(frame))) > 0)
|
||||
if (intf_data[iMotorIndex].socket >= 0)
|
||||
{
|
||||
canid_t pgn = frame.can_id & 0x00FFFF00;
|
||||
ssize_t nbytes = 0;
|
||||
struct can_frame frame;
|
||||
|
||||
switch(pgn)
|
||||
// increment cycle counter
|
||||
IncBusTimeoutCounter(iMotorIndex);
|
||||
|
||||
// read one frame
|
||||
if ((nbytes = read(intf_data[iMotorIndex].socket, &frame, sizeof(frame))) > 0)
|
||||
{
|
||||
case 0x00EF6400: // "repeat data"
|
||||
break;
|
||||
canid_t pgn = frame.can_id & 0x00FFFF00;
|
||||
|
||||
case 0x00F00300: // PGN 61443 "Electronic Engine Controller 2"
|
||||
// we have sent this -> ignore
|
||||
break;
|
||||
switch(pgn)
|
||||
{
|
||||
case 0x00EF6400: // "repeat data"
|
||||
break;
|
||||
|
||||
case 0x00F00500: // PGN 61445 "Electronic Transmission Controller 2"
|
||||
// we have sent this -> ignore
|
||||
break;
|
||||
case 0x00F00300: // PGN 61443 "Electronic Engine Controller 2"
|
||||
// we have sent this -> ignore
|
||||
break;
|
||||
|
||||
case 0x00FF1300: // PGN 65299 "Manufacturer PGN"
|
||||
// here we find the states of the switches
|
||||
Can_Read_Manu_PGN(iMotorIndex, &frame);
|
||||
break;
|
||||
case 0x00F00500: // PGN 61445 "Electronic Transmission Controller 2"
|
||||
// we have sent this -> ignore
|
||||
break;
|
||||
|
||||
case 0x00FF1400: // PGN 65300 "Manufacturer PGN 2"
|
||||
// here we find the actual power of the motor
|
||||
Can_Read_Manu_PGN2(iMotorIndex, &frame);
|
||||
break;
|
||||
case 0x00FF1300: // PGN 65299 "Manufacturer PGN"
|
||||
// here we find the states of the switches
|
||||
Can_Read_Manu_PGN(iMotorIndex, &frame);
|
||||
break;
|
||||
|
||||
case 0x00FF1400: // PGN 65300 "Manufacturer PGN 2"
|
||||
// here we find the actual power of the motor
|
||||
Can_Read_Manu_PGN2(iMotorIndex, &frame);
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -15,28 +15,24 @@
|
||||
#include <linux/can.h>
|
||||
#include <linux/can/raw.h>
|
||||
|
||||
#define MOTOR_COUNT 2
|
||||
#define MAX_MOTOR_COUNT 2
|
||||
|
||||
// motor gear: 0x7C=reverse, 0x7D=neutral, 0x7E=forward
|
||||
#define MOTOR_GEAR_REVERSE 0x7C
|
||||
#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)
|
||||
|
||||
struct MOTOR_CONTROL_DATA
|
||||
{
|
||||
char nDriveConnected;
|
||||
char nDriveReady;
|
||||
int iMotorGear;
|
||||
int iMotorPower;
|
||||
int iMotorPowerPct;
|
||||
int iMotorPowerRaw;
|
||||
int iMotorPowerSteps;
|
||||
unsigned char nSwitchState;
|
||||
int iActualMotorPowerW;
|
||||
};
|
||||
extern struct MOTOR_CONTROL_DATA motctrl[MOTOR_COUNT];
|
||||
extern struct MOTOR_CONTROL_DATA motctrl[MAX_MOTOR_COUNT];
|
||||
|
||||
struct CAN_INTERFACE_DATA
|
||||
{
|
||||
|
||||
62
io/io.c
62
io/io.c
@@ -2,17 +2,22 @@
|
||||
#include "main.h"
|
||||
#include "io.h"
|
||||
#include <can/can_client.h>
|
||||
#include <settings/settings.h>
|
||||
#include <mqtt/mqtt_client.h>
|
||||
|
||||
struct GPIO_KEY_DATA gpioKeyStop;
|
||||
struct GPIO_KEY_DATA gpioKeyPwrUp;
|
||||
struct GPIO_KEY_DATA gpioKeyPwrDown;
|
||||
|
||||
char nInitialized = 0;
|
||||
int iPowerSupplyOn = 0;
|
||||
|
||||
/// @brief Initialize the io pins
|
||||
/// @return
|
||||
int IO_Init()
|
||||
{
|
||||
iPowerSupplyOn = 0;
|
||||
|
||||
if (wiringPiSetupPinType(WPI_PIN_BCM))
|
||||
{
|
||||
mylog(LOG_ERR, "IO: Set up wiringPi failed!");
|
||||
@@ -51,7 +56,7 @@ void SetupKeyPin(struct GPIO_KEY_DATA *pdata, int iKeyPin)
|
||||
|
||||
if ((pdata->iKeyPin > 0) && nInitialized)
|
||||
{
|
||||
//mylog(LOG_INFO, "IO: Config Pin %d as input", iOutPin);
|
||||
mylog(LOG_DEBUG, "IO: Config Pin %d as input", pdata->iKeyPin);
|
||||
pinMode(pdata->iKeyPin, INPUT);
|
||||
pullUpDnControl(pdata->iKeyPin, PUD_UP);
|
||||
}
|
||||
@@ -154,7 +159,7 @@ void SetupOutputPin(int iOutPin)
|
||||
{
|
||||
if ((iOutPin > 0) && nInitialized)
|
||||
{
|
||||
//mylog(LOG_INFO, "IO: Config Pin %d as output", iOutPin);
|
||||
mylog(LOG_DEBUG, "IO: Config Pin %d as output", iOutPin);
|
||||
pinMode(iOutPin, OUTPUT);
|
||||
digitalWrite(iOutPin, LOW);
|
||||
}
|
||||
@@ -179,46 +184,57 @@ void IO_DoCyclic()
|
||||
ReadKey(&gpioKeyPwrUp);
|
||||
ReadKey(&gpioKeyPwrDown);
|
||||
|
||||
if (gpioKeyStop.iKeyValue)
|
||||
if (gpioKeyStop.iKeyValue || atomic_load(&abKeyStop))
|
||||
{
|
||||
// stop key is pressed
|
||||
if (gpioKeyStop.iKeyRisingEdge)
|
||||
if (gpioKeyStop.iKeyRisingEdge || atomic_load(&abKeyStop))
|
||||
{
|
||||
mylog(LOG_INFO, "IO: KEY-Stop: Stop motor.");
|
||||
Can_SetMotorGear(0, 0);
|
||||
}
|
||||
}
|
||||
else if (motctrl[0].nDriveReady) // plus and minus keys only when drive is ready
|
||||
|
||||
if (gpioKeyPwrUp.iKeyRisingEdge || atomic_load(&abKeyPlus))
|
||||
{
|
||||
if (gpioKeyPwrUp.iKeyRisingEdge)
|
||||
if (motctrl[0].nDriveReady)
|
||||
{
|
||||
// plus key is pressed -> increase power
|
||||
// when drive is ready to run: plus key is pressed -> increase power
|
||||
if (motctrl[0].iMotorGear == MOTOR_GEAR_NEUTRAL)
|
||||
{
|
||||
mylog(LOG_INFO, "IO: KEY-Plus: Start motor.");
|
||||
Can_SetMotorGear(0, 1);
|
||||
Can_SetMotorPower(0, MOTOR_PWR_MIN_PCT);
|
||||
Can_SetMotorPower(0, 1);
|
||||
}
|
||||
else
|
||||
else if (motctrl[0].iMotorPowerSteps < settings.iMotorPwrStepCount)
|
||||
{
|
||||
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)
|
||||
else if (settings.iShellySupplyCount > 0)
|
||||
{
|
||||
// minus key is pressed -> decrease power
|
||||
if (motctrl[0].iMotorPowerPct > MOTOR_PWR_MIN_PCT)
|
||||
{
|
||||
mylog(LOG_INFO, "IO: KEY-Minus: Decrease power.");
|
||||
Can_SetMotorPower(0, motctrl[0].iMotorPowerPct - MOTOR_PWR_STEP);
|
||||
}
|
||||
else
|
||||
{
|
||||
mylog(LOG_INFO, "IO: KEY-Minus: Stop motor.");
|
||||
Can_SetMotorGear(0, 0);
|
||||
}
|
||||
// when drive is not ready and we have to switch the supply
|
||||
MqttClient_SwitchPowerSupply(1);
|
||||
iPowerSupplyOn = 1;
|
||||
}
|
||||
}
|
||||
|
||||
if (gpioKeyPwrDown.iKeyRisingEdge || atomic_load(&abKeyMinus))
|
||||
{
|
||||
// minus key is pressed -> decrease power
|
||||
if (motctrl[0].iMotorPowerSteps > 1)
|
||||
{
|
||||
mylog(LOG_INFO, "IO: KEY-Minus: Decrease power.");
|
||||
Can_SetMotorPower(0, motctrl[0].iMotorPowerSteps - 1);
|
||||
}
|
||||
// else
|
||||
// {
|
||||
// mylog(LOG_INFO, "IO: KEY-Minus: Stop motor.");
|
||||
// Can_SetMotorGear(0, 0);
|
||||
// }
|
||||
}
|
||||
|
||||
atomic_store(&abKeyPlus, false);
|
||||
atomic_store(&abKeyMinus, false);
|
||||
atomic_store(&abKeyStop, false);
|
||||
}
|
||||
|
||||
2
io/io.h
2
io/io.h
@@ -30,6 +30,8 @@ struct GPIO_KEY_DATA
|
||||
int iKeyRepeatCycleCounter;
|
||||
};
|
||||
|
||||
extern int iPowerSupplyOn;
|
||||
|
||||
int IO_Init();
|
||||
void IO_DoCyclic();
|
||||
void SetupKeyPin(struct GPIO_KEY_DATA *pdata, int iKeyPin);
|
||||
|
||||
140
main.c
140
main.c
@@ -3,12 +3,18 @@
|
||||
#include <mqtt/mqtt_client.h>
|
||||
#include <can/can_client.h>
|
||||
#include <io/io.h>
|
||||
#include <settings/settings.h>
|
||||
#include <termios.h>
|
||||
|
||||
// Period info of the realtime task
|
||||
struct period_info pinfo;
|
||||
int iThreadControl = 0; // 0: thread is running, <0: thread shall exit, >0 thread has exited
|
||||
int iLogToConsole = 1;
|
||||
atomic_short asThreadControl = ATOMIC_VAR_INIT(0); // 0: thread is running, <0: thread shall exit, >0 thread has exited
|
||||
|
||||
// values for simulation
|
||||
atomic_bool abKeyPlus = ATOMIC_VAR_INIT(false);
|
||||
atomic_bool abKeyMinus = ATOMIC_VAR_INIT(false);
|
||||
atomic_bool abKeyStop = ATOMIC_VAR_INIT(false);
|
||||
|
||||
/// @brief send a log message
|
||||
/// @param prio
|
||||
@@ -16,27 +22,30 @@ int iLogToConsole = 1;
|
||||
/// @param
|
||||
void mylog(int prio, const char *format, ...)
|
||||
{
|
||||
va_list args;
|
||||
if (prio <= settings.iDebugLevel)
|
||||
{
|
||||
va_list args;
|
||||
|
||||
// 1. Initialisiere die Argumentenliste mit dem letzten festen Parameter
|
||||
va_start(args, format);
|
||||
// 1. Initialize the argument list with the last fixed argument
|
||||
va_start(args, format);
|
||||
|
||||
// 2. Übergabe an vsyslog (statt syslog)
|
||||
// vsyslog nimmt eine va_list entgegen
|
||||
vsyslog(prio, format, args);
|
||||
// 2. Transfer to vsyslog (instead of syslog)
|
||||
// vsyslog accepts a va_list
|
||||
vsyslog(prio, format, args);
|
||||
|
||||
// 3. Optional: Zusätzlich auf die Konsole ausgeben
|
||||
// Wir müssen die Liste neu initialisieren, da va_list "verbraucht" wird
|
||||
if (iLogToConsole)
|
||||
{
|
||||
va_end(args);
|
||||
va_start(args, format);
|
||||
vfprintf(stderr, format, args);
|
||||
fprintf(stderr, "\n");
|
||||
}
|
||||
// 3. Optional: Output additionally to the console
|
||||
// We have to reinitialize the list because va_list is "consumed."
|
||||
if (iLogToConsole)
|
||||
{
|
||||
va_end(args);
|
||||
va_start(args, format);
|
||||
vfprintf(stderr, format, args);
|
||||
fprintf(stderr, "\n");
|
||||
}
|
||||
|
||||
// 4. Aufräumen
|
||||
va_end(args);
|
||||
// 4. Cleanup
|
||||
va_end(args);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -146,7 +155,7 @@ void *thread_func(void *data)
|
||||
}
|
||||
|
||||
// Connect to mqtt broker
|
||||
while (MqttClient_Connect() && (iThreadControl == 0))
|
||||
while (MqttClient_Connect() && atomic_load(&asThreadControl) == 0)
|
||||
{
|
||||
mylog(LOG_ERR, "MqttClient_Connect() failed!");
|
||||
sleep(10);
|
||||
@@ -159,13 +168,13 @@ void *thread_func(void *data)
|
||||
WriteOutputPin(GPIO_OUT_PWRON, HIGH);
|
||||
|
||||
// cyclic call of do_cyclic_1ms()
|
||||
while (iThreadControl == 0)
|
||||
while (atomic_load(&asThreadControl) == 0)
|
||||
{
|
||||
pinfo.cyclecounter++;
|
||||
if (pinfo.cyclecounter >= 86400000)
|
||||
if (pinfo.cyclecounter > CYCLE_COUNTER_MAX)
|
||||
{
|
||||
// Reset cycle counter every 24h
|
||||
pinfo.cyclecounter = 0;
|
||||
pinfo.cyclecounter = 1;
|
||||
}
|
||||
do_cyclic_1ms(&pinfo);
|
||||
wait_rest_of_period(&pinfo);
|
||||
@@ -181,11 +190,22 @@ void *thread_func(void *data)
|
||||
Can_CloseInterface(0);
|
||||
|
||||
// signal thread has finnished
|
||||
iThreadControl = 1;
|
||||
atomic_store(&asThreadControl, 1);
|
||||
return NULL;
|
||||
}
|
||||
|
||||
|
||||
// Funktion, um das Terminal in den "Raw Mode" zu versetzen
|
||||
void set_conio_terminal_mode()
|
||||
{
|
||||
struct termios new_termios;
|
||||
tcgetattr(0, &new_termios);
|
||||
new_termios.c_lflag &= ~ICANON; // Deaktiviert den zeilenweisen Modus
|
||||
new_termios.c_lflag &= ~ECHO; // Verhindert, dass die Taste angezeigt wird
|
||||
tcsetattr(0, TCSANOW, &new_termios);
|
||||
}
|
||||
|
||||
|
||||
/// @brief catch signals and set flag to terminate for the realtime thread
|
||||
/// @param signo
|
||||
void sig_handler(int signo)
|
||||
@@ -193,7 +213,7 @@ void sig_handler(int signo)
|
||||
if ((signo == SIGINT) || (signo == SIGTERM))
|
||||
{
|
||||
mylog(LOG_INFO, "Received signal %d", signo);
|
||||
iThreadControl = -1; // signal realtime thread to exit
|
||||
atomic_store(&asThreadControl, -1); // signal realtime thread to exit
|
||||
}
|
||||
}
|
||||
|
||||
@@ -209,19 +229,33 @@ int main(int argc, char* argv[])
|
||||
pthread_t thread;
|
||||
int ret;
|
||||
|
||||
// First we have to get the default values of our settings
|
||||
Settings_InitDefaultValues();
|
||||
|
||||
openlog("CanRtDriver", LOG_PID | LOG_CONS, LOG_DAEMON);
|
||||
mylog(LOG_INFO, "Service started. PID: %d", getpid());
|
||||
|
||||
// catch signals
|
||||
if (signal(SIGTERM, sig_handler) == SIG_ERR)
|
||||
// Read the settings file after opening the log
|
||||
Settings_ReadConfFile();
|
||||
|
||||
if (settings.iCanSimu)
|
||||
{
|
||||
mylog(LOG_ERR, "Can't catch SIGTERM");
|
||||
exit(-1);
|
||||
// in simulation mode switch terminal to raw mode
|
||||
set_conio_terminal_mode();
|
||||
}
|
||||
if (signal(SIGINT, sig_handler) == SIG_ERR)
|
||||
else
|
||||
{
|
||||
mylog(LOG_ERR, "Can't catch SIGINT");
|
||||
exit(-2);
|
||||
// catch signals
|
||||
if (signal(SIGTERM, sig_handler) == SIG_ERR)
|
||||
{
|
||||
mylog(LOG_ERR, "Can't catch SIGTERM");
|
||||
exit(-1);
|
||||
}
|
||||
if (signal(SIGINT, sig_handler) == SIG_ERR)
|
||||
{
|
||||
mylog(LOG_ERR, "Can't catch SIGINT");
|
||||
exit(-2);
|
||||
}
|
||||
}
|
||||
|
||||
/* Lock memory */
|
||||
@@ -271,7 +305,7 @@ int main(int argc, char* argv[])
|
||||
|
||||
|
||||
/* Create a pthread with specified attributes */
|
||||
iThreadControl = 0;
|
||||
atomic_store(&asThreadControl, 0);
|
||||
ret = pthread_create(&thread, &attr, thread_func, NULL);
|
||||
if (ret)
|
||||
{
|
||||
@@ -279,6 +313,48 @@ int main(int argc, char* argv[])
|
||||
goto out;
|
||||
}
|
||||
|
||||
if (settings.iCanSimu)
|
||||
{
|
||||
// in simulation mode we use keys to control the motors
|
||||
mylog(LOG_INFO, "*** CanRtDriver in Simu-Mode ***");
|
||||
while (1)
|
||||
{
|
||||
char ch = getchar();
|
||||
mylog(LOG_INFO, "SIMU: Taste gedrückt: %c", ch);
|
||||
|
||||
if (ch == '+')
|
||||
{
|
||||
atomic_store(&abKeyPlus, true);
|
||||
}
|
||||
else if (ch == '-')
|
||||
{
|
||||
atomic_store(&abKeyMinus, true);
|
||||
}
|
||||
else if (ch == 's')
|
||||
{
|
||||
atomic_store(&abKeyStop, true);
|
||||
}
|
||||
else if (ch == 'p')
|
||||
{
|
||||
if (iPowerSupplyOn)
|
||||
{
|
||||
MqttClient_SwitchPowerSupply(0);
|
||||
iPowerSupplyOn = 0;
|
||||
}
|
||||
else
|
||||
{
|
||||
MqttClient_SwitchPowerSupply(1);
|
||||
iPowerSupplyOn = 1;
|
||||
}
|
||||
}
|
||||
else if ( ch == 'q')
|
||||
{
|
||||
atomic_store(&asThreadControl, -1); // signal realtime thread to exit
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// join the thread and wait for it to exit
|
||||
ret = pthread_join(thread, NULL);
|
||||
if (ret)
|
||||
|
||||
9
main.h
9
main.h
@@ -13,8 +13,13 @@
|
||||
#include <signal.h>
|
||||
#include <syslog.h>
|
||||
#include <stdarg.h>
|
||||
#include <stdatomic.h>
|
||||
#include <stdbool.h>
|
||||
|
||||
|
||||
//#define CYCLE_COUNTER_MAX 86400000
|
||||
#define CYCLE_COUNTER_MAX 600000 // for testing only
|
||||
|
||||
struct period_info
|
||||
{
|
||||
struct timespec next_period;
|
||||
@@ -23,6 +28,10 @@ struct period_info
|
||||
float fStartTime;
|
||||
};
|
||||
|
||||
extern atomic_bool abKeyPlus;
|
||||
extern atomic_bool abKeyMinus;
|
||||
extern atomic_bool abKeyStop;
|
||||
|
||||
extern struct period_info pinfo;
|
||||
extern void mylog(int prio, const char *format, ...);
|
||||
|
||||
|
||||
3
makefile
3
makefile
@@ -7,13 +7,14 @@ CFLAGS = -Wextra -Wall -std=gnu99 -I. -Wno-unused-parameter -Wno-unused-variable
|
||||
MQTT_SOURCES = mqtt/mqtt_client.c
|
||||
CAN_SOURCES = can/can_client.c
|
||||
IO_SOURCES = io/io.c
|
||||
SETTINGS_SOURCES = settings/settings.c
|
||||
PROG = bin/CanRtDriver
|
||||
BINDIR = bin
|
||||
|
||||
|
||||
all: $(BINDIR) $(PROG)
|
||||
|
||||
bin/CanRtDriver: main.c $(CAN_SOURCES) $(MQTT_SOURCES) $(IO_SOURCES)
|
||||
bin/CanRtDriver: main.c $(CAN_SOURCES) $(MQTT_SOURCES) $(IO_SOURCES) $(SETTINGS_SOURCES)
|
||||
$(CC) $(CFLAGS) $^ -lpthread -lmosquitto -lwiringPi -o $@
|
||||
|
||||
$(BINDIR):
|
||||
|
||||
@@ -8,30 +8,30 @@
|
||||
#include "main.h"
|
||||
#include <mqtt/mqtt_client.h>
|
||||
#include <can/can_client.h>
|
||||
#include <settings/settings.h>
|
||||
#include <string.h>
|
||||
|
||||
#include <mosquitto.h>
|
||||
|
||||
|
||||
// Topics to subscribe
|
||||
const char* mqtt_topic_motor_gear_request = "Pool/Motor_Gear_Request";
|
||||
const char* mqtt_topic_motor_power_request = "Pool/Motor_Power_Request";
|
||||
|
||||
// Topics to publish
|
||||
const char* mqtt_topic_status_cyclecounter = "Pool/Status/CycleCounter";
|
||||
|
||||
const char* mqtt_topic_motor1_gear = "Pool/Motor1/Gear";
|
||||
int iMqttMotor1Gear = 0;
|
||||
const char* mqtt_topic_motor1_power = "Pool/Motor1/Power";
|
||||
int iMqttMotor1Power = 0;
|
||||
const char* mqtt_topic_motor2_gear = "Pool/Motor2/Gear";
|
||||
int iMqttMotor2Gear = 0;
|
||||
const char* mqtt_topic_motor2_power = "Pool/Motor2/Power";
|
||||
int iMqttMotor2Power = 0;
|
||||
|
||||
const char* mqtt_topic_motor1_switchstate = "Pool/Motor1/SwitchState";
|
||||
unsigned char nMqttMotor1SwitchState = 0;
|
||||
unsigned char nMotor1SwitchState = 255;
|
||||
const char* mqtt_topic_motor2_switchstate = "Pool/Motor1/SwitchState";
|
||||
unsigned char nMqttMotor2SwitchState = 0;
|
||||
unsigned char nMotor2SwitchState = 255;
|
||||
const char* mqtt_topic_motor1_actualpowerw = "Pool/Motor1/ActualPowerW";
|
||||
int iMqttMotor1ActualPowerW = 0;
|
||||
int iMqttMotor1ActualPowerW = -1;
|
||||
const char* mqtt_topic_motor2_actualpowerw = "Pool/Motor1/ActualPowerW";
|
||||
int iMqttMotor2ActualPowerW = 0;
|
||||
int iMqttMotor2ActualPowerW = -1;
|
||||
|
||||
const char* mqtt_broker_addr = "127.0.0.1";
|
||||
const int mqtt_broker_port = 1883;
|
||||
@@ -50,60 +50,46 @@ void my_message_callback(struct mosquitto *mosq, void *userdata, const struct mo
|
||||
memcpy(topic_value, message->payload, message->payloadlen);
|
||||
topic_value[message->payloadlen] = '\0';
|
||||
|
||||
if (strcmp(message->topic, mqtt_topic_motor1_gear) == 0)
|
||||
if (strcmp(message->topic, mqtt_topic_motor_gear_request) == 0)
|
||||
{
|
||||
int val = 9999;
|
||||
int val = 123456789;
|
||||
if (sscanf(topic_value, "%d", &val))
|
||||
{
|
||||
mylog(LOG_INFO, "MQTT: Received value for mqtt_topic_motor1_gear: %d", val);
|
||||
iMqttMotor1Gear = val;
|
||||
Can_SetMotorGear(0, val);
|
||||
mylog(LOG_INFO, "MQTT: Received value for mqtt_topic_motor_gear_request: %d", val);
|
||||
if (val == 123456789)
|
||||
{
|
||||
val = 0;
|
||||
}
|
||||
|
||||
for (int i=0; i<settings.iMotorCount; i++)
|
||||
{
|
||||
Can_SetMotorGear(i, val);
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
mylog(LOG_WARNING, "MQTT: Received mqtt_topic_motor1_gear: %s", topic_value);
|
||||
mylog(LOG_WARNING, "MQTT: Received mqtt_topic_motor_gear_request: %s", topic_value);
|
||||
}
|
||||
}
|
||||
else if (strcmp(message->topic, mqtt_topic_motor1_power) == 0)
|
||||
else if (strcmp(message->topic, mqtt_topic_motor_power_request) == 0)
|
||||
{
|
||||
int val = 9999;
|
||||
int val = 123456789;
|
||||
if (sscanf(topic_value, "%d", &val))
|
||||
{
|
||||
mylog(LOG_INFO, "MQTT: Received value for mqtt_topic_motor1_power: %d", val);
|
||||
iMqttMotor1Power = val;
|
||||
Can_SetMotorPower(0, val);
|
||||
mylog(LOG_INFO, "MQTT: Received value for mqtt_topic_motor_power_request: %d", val);
|
||||
if (val == 123456789)
|
||||
{
|
||||
val = 0;
|
||||
}
|
||||
|
||||
for (int i=0; i<settings.iMotorCount; i++)
|
||||
{
|
||||
Can_SetMotorPower(i, val);
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
mylog(LOG_WARNING, "MQTT: Received mqtt_topic_motor1_power: %s", topic_value);
|
||||
}
|
||||
}
|
||||
else if (strcmp(message->topic, mqtt_topic_motor2_gear) == 0)
|
||||
{
|
||||
int val = 9999;
|
||||
if (sscanf(topic_value, "%d", &val))
|
||||
{
|
||||
mylog(LOG_INFO, "MQTT: Received value for mqtt_topic_motor2_gear: %d", val);
|
||||
iMqttMotor2Gear = val;
|
||||
Can_SetMotorGear(1, val);
|
||||
}
|
||||
else
|
||||
{
|
||||
mylog(LOG_WARNING, "MQTT: Received mqtt_topic_motor2_gear: %s", topic_value);
|
||||
}
|
||||
}
|
||||
else if (strcmp(message->topic, mqtt_topic_motor2_power) == 0)
|
||||
{
|
||||
int val = 9999;
|
||||
if (sscanf(topic_value, "%d", &val))
|
||||
{
|
||||
mylog(LOG_INFO, "MQTT: Received value for mqtt_topic_motor2_power: %d", val);
|
||||
iMqttMotor2Power = val;
|
||||
Can_SetMotorPower(1, val);
|
||||
}
|
||||
else
|
||||
{
|
||||
mylog(LOG_WARNING, "MQTT: Received mqtt_topic_motor2_power: %s", topic_value);
|
||||
mylog(LOG_WARNING, "MQTT: Received mqtt_topic_motor_gear_request: %s", topic_value);
|
||||
}
|
||||
}
|
||||
else
|
||||
@@ -114,6 +100,38 @@ void my_message_callback(struct mosquitto *mosq, void *userdata, const struct mo
|
||||
free(topic_value);
|
||||
}
|
||||
|
||||
/// @brief callback function after connection
|
||||
void my_connect_callback(struct mosquitto *mosq, void *obj, int rc)
|
||||
{
|
||||
if (rc == 0)
|
||||
{
|
||||
if (mosquitto_subscribe(mosq, NULL, mqtt_topic_motor_gear_request, 0) != MOSQ_ERR_SUCCESS)
|
||||
{
|
||||
mylog(LOG_ERR, "MQTT: mosquitto_subscribe(mqtt_topic_motor_gear_request) failed!");
|
||||
}
|
||||
if (mosquitto_subscribe(mosq, NULL, mqtt_topic_motor_power_request, 0) != MOSQ_ERR_SUCCESS)
|
||||
{
|
||||
mylog(LOG_ERR, "MQTT: mosquitto_subscribe(mqtt_topic_motor_power_request) failed!");
|
||||
}
|
||||
|
||||
char message[10];
|
||||
snprintf(message, sizeof(message), "0");
|
||||
if (mosquitto_publish(mosq, NULL, mqtt_topic_motor_gear_request, strlen(message), &message, 0, false) != MOSQ_ERR_SUCCESS)
|
||||
{
|
||||
mylog(LOG_ERR, "MQTT: mosquitto_publish(mqtt_topic_motor_gear_request) failed!");
|
||||
}
|
||||
if (mosquitto_publish(mosq, NULL, mqtt_topic_motor_power_request, strlen(message), &message, 0, false) != MOSQ_ERR_SUCCESS)
|
||||
{
|
||||
mylog(LOG_ERR, "MQTT: mosquitto_publish(mqtt_topic_motor_power_request) failed!");
|
||||
}
|
||||
|
||||
for (int i=0; i<settings.iMotorCount; i++)
|
||||
{
|
||||
MqttClient_Publish_MotorSwitchState(i, 0);
|
||||
MqttClient_Publish_MotorActualPowerW(i, 0);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// @brief connect to mqtt broker
|
||||
/// @return
|
||||
@@ -138,6 +156,7 @@ int MqttClient_Connect()
|
||||
|
||||
// Define a function which will be called by libmosquitto client every time when there is a new MQTT message
|
||||
mosquitto_message_callback_set(mosq, my_message_callback);
|
||||
mosquitto_connect_callback_set(mosq, my_connect_callback);
|
||||
|
||||
// Connect to MQTT broker
|
||||
if (mosquitto_connect(mosq, mqtt_broker_addr, mqtt_broker_port, 60) != MOSQ_ERR_SUCCESS)
|
||||
@@ -152,81 +171,11 @@ int MqttClient_Connect()
|
||||
{
|
||||
/* Wenn wir Verbindungsfehler hatten, dann befinden wir uns wohl in Boot-Prozess und der Mosquitto ist
|
||||
gerade erst gestartet. Wir müssen hier etwas warten, sonst funktioniert das Subscriben nicht */
|
||||
sleep(10);
|
||||
sleep(500);
|
||||
}
|
||||
|
||||
// publish all topics we want to subscribe
|
||||
char message[10];
|
||||
snprintf(message, sizeof(message), "0");
|
||||
if (mosquitto_publish(mosq, NULL, mqtt_topic_motor1_gear, strlen(message), &message, 0, false) != MOSQ_ERR_SUCCESS)
|
||||
{
|
||||
mylog(LOG_ERR, "MQTT: mosquitto_publish(mqtt_topic_motor1_gear) failed!");
|
||||
MqttClient_Close();
|
||||
iHadConnectError++;
|
||||
return 10;
|
||||
}
|
||||
if (mosquitto_publish(mosq, NULL, mqtt_topic_motor1_power, strlen(message), &message, 0, false) != MOSQ_ERR_SUCCESS)
|
||||
{
|
||||
mylog(LOG_ERR, "MQTT: mosquitto_publish(mqtt_topic_motor1_power) failed!");
|
||||
MqttClient_Close();
|
||||
iHadConnectError++;
|
||||
return 11;
|
||||
}
|
||||
if (mosquitto_publish(mosq, NULL, mqtt_topic_motor2_gear, strlen(message), &message, 0, false) != MOSQ_ERR_SUCCESS)
|
||||
{
|
||||
mylog(LOG_ERR, "MQTT: mosquitto_publish(mqtt_topic_motor2_gear) failed!");
|
||||
MqttClient_Close();
|
||||
iHadConnectError++;
|
||||
return 12;
|
||||
MqttClient_Close();
|
||||
}
|
||||
if (mosquitto_publish(mosq, NULL, mqtt_topic_motor2_power, strlen(message), &message, 0, false) != MOSQ_ERR_SUCCESS)
|
||||
{
|
||||
mylog(LOG_ERR, "MQTT: mosquitto_publish(mqtt_topic_motor2_power) failed!");
|
||||
MqttClient_Close();
|
||||
iHadConnectError++;
|
||||
return 13;
|
||||
}
|
||||
|
||||
// subscribe all needed topics
|
||||
if (mosquitto_subscribe(mosq, NULL, mqtt_topic_motor1_gear, 0) != MOSQ_ERR_SUCCESS)
|
||||
{
|
||||
mylog(LOG_ERR, "MQTT: mosquitto_subscribe(mqtt_topic_motor1_gear) failed!");
|
||||
iHadConnectError++;
|
||||
return 20;
|
||||
}
|
||||
if (mosquitto_subscribe(mosq, NULL, mqtt_topic_motor1_power, 0) != MOSQ_ERR_SUCCESS)
|
||||
{
|
||||
mylog(LOG_ERR, "MQTT: mosquitto_subscribe(mqtt_topic_motor1_power) failed!");
|
||||
MqttClient_Close();
|
||||
iHadConnectError++;
|
||||
return 21;
|
||||
}
|
||||
if (mosquitto_subscribe(mosq, NULL, mqtt_topic_motor2_gear, 0) != MOSQ_ERR_SUCCESS)
|
||||
{
|
||||
mylog(LOG_ERR, "MQTT: mosquitto_subscribe(mqtt_topic_motor2_gear) failed!");
|
||||
MqttClient_Close();
|
||||
iHadConnectError++;
|
||||
return 22;
|
||||
}
|
||||
if (mosquitto_subscribe(mosq, NULL, mqtt_topic_motor2_power, 0) != MOSQ_ERR_SUCCESS)
|
||||
{
|
||||
mylog(LOG_ERR, "MQTT: mosquitto_subscribe(mqtt_topic_motor2_power) failed!");
|
||||
MqttClient_Close();
|
||||
iHadConnectError++;
|
||||
return 23;
|
||||
}
|
||||
|
||||
nMqttMotor1SwitchState = 255;
|
||||
MqttClient_Publish_MotorSwitchState(0, 0);
|
||||
nMqttMotor2SwitchState = 255;
|
||||
MqttClient_Publish_MotorSwitchState(1, 0);
|
||||
iMqttMotor1ActualPowerW = -1;
|
||||
MqttClient_Publish_MotorActualPowerW(0, 0);
|
||||
iMqttMotor2ActualPowerW = -1;
|
||||
MqttClient_Publish_MotorActualPowerW(1, 0);
|
||||
|
||||
mylog(LOG_INFO, "MQTT: Connected successfull!");
|
||||
mylog(LOG_INFO, "MQTT: Connected successfull after %d errors!", iHadConnectError);
|
||||
return 0;
|
||||
}
|
||||
|
||||
@@ -266,23 +215,15 @@ void MqttClient_Publish_MotorGear(int iMotorIndex, int iGear)
|
||||
{
|
||||
if (iMotorIndex == 0)
|
||||
{
|
||||
if (iGear != iMqttMotor1Gear)
|
||||
{
|
||||
iMqttMotor1Gear = iGear;
|
||||
char message[100];
|
||||
snprintf(message, sizeof(message), "%d", iGear);
|
||||
mosquitto_publish(mosq, NULL, mqtt_topic_motor1_gear, strlen(message), message, 0, false);
|
||||
}
|
||||
char message[100];
|
||||
snprintf(message, sizeof(message), "%d", iGear);
|
||||
mosquitto_publish(mosq, NULL, mqtt_topic_motor1_gear, strlen(message), message, 0, false);
|
||||
}
|
||||
else if (iMotorIndex == 1)
|
||||
{
|
||||
if (iGear != iMqttMotor2Gear)
|
||||
{
|
||||
iMqttMotor2Gear = iGear;
|
||||
char message[100];
|
||||
snprintf(message, sizeof(message), "%d", iGear);
|
||||
mosquitto_publish(mosq, NULL, mqtt_topic_motor2_gear, strlen(message), message, 0, false);
|
||||
}
|
||||
char message[100];
|
||||
snprintf(message, sizeof(message), "%d", iGear);
|
||||
mosquitto_publish(mosq, NULL, mqtt_topic_motor2_gear, strlen(message), message, 0, false);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -294,23 +235,15 @@ void MqttClient_Publish_MotorPower(int iMotorIndex, int iPower)
|
||||
{
|
||||
if (iMotorIndex == 0)
|
||||
{
|
||||
if (iPower != iMqttMotor1Power)
|
||||
{
|
||||
iMqttMotor1Power = iPower;
|
||||
char message[100];
|
||||
snprintf(message, sizeof(message), "%d", iPower);
|
||||
mosquitto_publish(mosq, NULL, mqtt_topic_motor1_power, strlen(message), message, 0, false);
|
||||
}
|
||||
char message[100];
|
||||
snprintf(message, sizeof(message), "%d", iPower);
|
||||
mosquitto_publish(mosq, NULL, mqtt_topic_motor1_power, strlen(message), message, 0, false);
|
||||
}
|
||||
else if (iMotorIndex == 1)
|
||||
{
|
||||
if (iPower != iMqttMotor2Power)
|
||||
{
|
||||
iMqttMotor2Power = iPower;
|
||||
char message[100];
|
||||
snprintf(message, sizeof(message), "%d", iPower);
|
||||
mosquitto_publish(mosq, NULL, mqtt_topic_motor2_power, strlen(message), message, 0, false);
|
||||
}
|
||||
char message[100];
|
||||
snprintf(message, sizeof(message), "%d", iPower);
|
||||
mosquitto_publish(mosq, NULL, mqtt_topic_motor2_power, strlen(message), message, 0, false);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -322,9 +255,9 @@ void MqttClient_Publish_MotorSwitchState(int iMotorIndex, unsigned char nSwitchS
|
||||
{
|
||||
if (iMotorIndex == 0)
|
||||
{
|
||||
if (nSwitchState != nMqttMotor1SwitchState)
|
||||
if (nSwitchState != nMotor1SwitchState)
|
||||
{
|
||||
nMqttMotor1SwitchState = nSwitchState;
|
||||
nMotor1SwitchState = nSwitchState;
|
||||
char message[100];
|
||||
snprintf(message, sizeof(message), "%2X", nSwitchState);
|
||||
mosquitto_publish(mosq, NULL, mqtt_topic_motor1_switchstate, strlen(message), message, 0, false);
|
||||
@@ -332,9 +265,9 @@ void MqttClient_Publish_MotorSwitchState(int iMotorIndex, unsigned char nSwitchS
|
||||
}
|
||||
else if (iMotorIndex == 1)
|
||||
{
|
||||
if (nSwitchState != nMqttMotor2SwitchState)
|
||||
if (nSwitchState != nMotor2SwitchState)
|
||||
{
|
||||
nMqttMotor2SwitchState = nSwitchState;
|
||||
nMotor2SwitchState = nSwitchState;
|
||||
char message[100];
|
||||
snprintf(message, sizeof(message), "%2X", nSwitchState);
|
||||
mosquitto_publish(mosq, NULL, mqtt_topic_motor2_switchstate, strlen(message), message, 0, false);
|
||||
@@ -368,4 +301,17 @@ void MqttClient_Publish_MotorActualPowerW(int iMotorIndex, int iMotorPowerW)
|
||||
mosquitto_publish(mosq, NULL, mqtt_topic_motor2_actualpowerw, strlen(message), message, 0, false);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
void MqttClient_SwitchPowerSupply(int on)
|
||||
{
|
||||
char *cmd = on ? "on" : "off";
|
||||
|
||||
for (int i=0; i<settings.iShellySupplyCount; i++)
|
||||
{
|
||||
mosquitto_publish(mosq, NULL, settings.sShellySupplyTopic[i], strlen(cmd), cmd, 0, false);
|
||||
}
|
||||
|
||||
mylog(LOG_INFO, "MQTT: Switch supply-shellies %s", cmd);
|
||||
}
|
||||
@@ -1,7 +1,7 @@
|
||||
#if !defined(__MQTT_CLIENT_H__)
|
||||
#define __MQTT_CLIENT_H__
|
||||
|
||||
#define USE_MOSQUITTO_LIB
|
||||
//#define USE_MOSQUITTO_LIB
|
||||
|
||||
int MqttClient_Connect();
|
||||
void MqttClient_Close();
|
||||
@@ -11,5 +11,6 @@ void MqttClient_Publish_MotorGear(int iMotorIndex, int iGear);
|
||||
void MqttClient_Publish_MotorPower(int iMotorIndex, int iPower);
|
||||
void MqttClient_Publish_MotorSwitchState(int iMotorIndex, unsigned char nSwitchState);
|
||||
void MqttClient_Publish_MotorActualPowerW(int iMotorIndex, int iMotorPowerW);
|
||||
void MqttClient_SwitchPowerSupply(int on);
|
||||
|
||||
#endif
|
||||
|
||||
@@ -1,4 +1,5 @@
|
||||
#!/bin/bash
|
||||
|
||||
git fetch
|
||||
git pull
|
||||
|
||||
|
||||
168
settings/settings.c
Executable file
168
settings/settings.c
Executable file
@@ -0,0 +1,168 @@
|
||||
|
||||
#include <main.h>
|
||||
#include <settings/settings.h>
|
||||
#include <ctype.h>
|
||||
|
||||
struct APP_SETTINGS settings;
|
||||
|
||||
void Settings_InitDefaultValues()
|
||||
{
|
||||
// #define LOG_EMERG 0 /* system is unusable */
|
||||
// #define LOG_ALERT 1 /* action must be taken immediately */
|
||||
// #define LOG_CRIT 2 /* critical conditions */
|
||||
// #define LOG_ERR 3 /* error conditions */
|
||||
// #define LOG_WARNING 4 /* warning conditions */
|
||||
// #define LOG_NOTICE 5 /* normal but significant condition */
|
||||
// #define LOG_INFO 6 /* informational */
|
||||
// #define LOG_DEBUG 7 /* debug-level messages */
|
||||
settings.iDebugLevel = LOG_INFO;
|
||||
settings.iCanSimu = 1;
|
||||
|
||||
settings.iMotorCount = 1;
|
||||
settings.iMotorPwrMinRaw = 38;
|
||||
settings.iMotorPwrMaxRaw = 250;
|
||||
settings.iMotorPwrStepCount = 7;
|
||||
|
||||
settings.iShellySupplyCount = 0;
|
||||
for (int i=0; i<MAX_SHELLIES_COUNT; i++)
|
||||
{
|
||||
settings.sShellySupplyTopic[i][0] = '\0';
|
||||
}
|
||||
|
||||
// Get path of the executable itself
|
||||
ssize_t length = readlink("/proc/self/exe", settings.sExePath, sizeof(settings.sExePath) - 1);
|
||||
if (length >= 0)
|
||||
{
|
||||
settings.sExePath[length] = '\0';
|
||||
mylog(LOG_INFO, "SETTINGS: Executable path: %s", settings.sExePath);
|
||||
}
|
||||
else
|
||||
{
|
||||
settings.sExePath[0] = '\0';
|
||||
mylog(LOG_ERR, "SETTINGS: Executable path not found!");
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
char *trim_str(const char *s)
|
||||
{
|
||||
// Führende Leerzeichen finden
|
||||
while (isspace((unsigned char)*s))
|
||||
s++;
|
||||
|
||||
// Falls der String leer ist
|
||||
if (*s == 0)
|
||||
return strdup("");
|
||||
|
||||
// Letztes Zeichen finden
|
||||
const char *end = s + strlen(s) - 1;
|
||||
while (end > s && isspace((unsigned char)*end))
|
||||
end--;
|
||||
|
||||
// Länge des neuen Strings berechnen
|
||||
size_t len = (end - s) + 1;
|
||||
|
||||
// Speicher reservieren (+1 für das Null-Byte)
|
||||
char *new_str = malloc(len + 1);
|
||||
if (new_str)
|
||||
{
|
||||
memcpy(new_str, s, len);
|
||||
new_str[len] = '\0';
|
||||
}
|
||||
|
||||
return new_str;
|
||||
}
|
||||
|
||||
void Settings_ReadConfFile()
|
||||
{
|
||||
|
||||
//const char *filename = "/etc/CanRtDriver.conf";
|
||||
|
||||
char filename[MAX_PATH + 50];
|
||||
sprintf(filename, "/etc/CanRtDriver.conf");
|
||||
FILE *file = fopen(filename, "r");
|
||||
if (file == NULL)
|
||||
{
|
||||
mylog(LOG_INFO, "SETTINGS: File %s noch found", filename);
|
||||
sprintf(filename, "%s.conf", settings.sExePath);
|
||||
file = fopen(filename, "r");
|
||||
if (file == NULL)
|
||||
{
|
||||
mylog(LOG_ERR, "SETTINGS: No conf file found!");
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
mylog(LOG_INFO, "SETTINGS: Reading %s", filename);
|
||||
|
||||
char line[MAX_LINE_LENGTH];
|
||||
while (fgets(line, sizeof(line), file))
|
||||
{
|
||||
// 1. Ignore comments and empty lines
|
||||
if (line[0] == '#' || line[0] == '\n' || line[0] == '\r')
|
||||
{
|
||||
continue;
|
||||
}
|
||||
|
||||
// 2. Remove CRLF at end of line
|
||||
line[strcspn(line, "\r\n")] = 0;
|
||||
|
||||
// 3. Split line to key and value
|
||||
char *key = trim_str(strtok(line, "="));
|
||||
char *value = trim_str(strtok(NULL, "="));
|
||||
|
||||
if ((key != NULL) && (value != NULL))
|
||||
{
|
||||
if (strcmp(key, "DebugLevel") == 0)
|
||||
{
|
||||
// how many log messages we want to see
|
||||
settings.iDebugLevel = atoi(value);
|
||||
mylog(LOG_DEBUG, "SETTINGS: %s = %d", key, settings.iDebugLevel);
|
||||
}
|
||||
else if (strcmp(key, "CanSimu") == 0)
|
||||
{
|
||||
// shall we do simulation
|
||||
settings.iCanSimu = atoi(value);
|
||||
mylog(LOG_DEBUG, "SETTINGS: %s = %d", key, settings.iCanSimu);
|
||||
}
|
||||
else if (strcmp(key, "MotorPowerMinRaw") == 0)
|
||||
{
|
||||
// Minimum power value for the motors
|
||||
settings.iMotorPwrMinRaw = atoi(value);
|
||||
mylog(LOG_DEBUG, "SETTINGS: %s = %d", key, settings.iMotorPwrMinRaw);
|
||||
}
|
||||
else if (strcmp(key, "MotorPowerMaxRaw") == 0)
|
||||
{
|
||||
// Maximum power value for the motors
|
||||
settings.iMotorPwrMaxRaw = atoi(value);
|
||||
mylog(LOG_DEBUG, "SETTINGS: %s = %d", key, settings.iMotorPwrMaxRaw);
|
||||
}
|
||||
else if (strcmp(key, "MotorPowerStepCount") == 0)
|
||||
{
|
||||
// How many steps do we want to have switching the power
|
||||
settings.iMotorPwrStepCount = atoi(value);
|
||||
mylog(LOG_DEBUG, "SETTINGS: %s = %d", key, settings.iMotorPwrStepCount);
|
||||
}
|
||||
else if (strcmp(key, "SupplyShellyMqttTopic") == 0)
|
||||
{
|
||||
// MQTT topic for switching power supply
|
||||
if (settings.iShellySupplyCount < MAX_SHELLIES_COUNT)
|
||||
{
|
||||
strcpy(settings.sShellySupplyTopic[settings.iShellySupplyCount], value);
|
||||
settings.iShellySupplyCount++;
|
||||
}
|
||||
else
|
||||
{
|
||||
mylog(LOG_WARNING, "SETTINGS: Too many SupplyShellyMqttTopic!");
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
mylog(LOG_WARNING, "SETTING: Unknown key: %s", key);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fclose(file);
|
||||
}
|
||||
33
settings/settings.h
Executable file
33
settings/settings.h
Executable file
@@ -0,0 +1,33 @@
|
||||
#if !defined(__SETTINGS_H__)
|
||||
#define __SETTINGS_H__
|
||||
|
||||
#include <stdio.h>
|
||||
#include <unistd.h>
|
||||
#include <limits.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
|
||||
#define MAX_LINE_LENGTH 256
|
||||
#define MAX_PATH 256
|
||||
#define MAX_SHELLIES_COUNT 5
|
||||
|
||||
struct APP_SETTINGS
|
||||
{
|
||||
int iDebugLevel; // Level of debug messages
|
||||
char sExePath[MAX_PATH]; // Path of the executable
|
||||
int iCanSimu; // Simulate CAN if 1
|
||||
|
||||
int iMotorCount; // Number of used motors (1 or 2)
|
||||
int iMotorPwrMinRaw; // Minimum power value for motor (raw value)
|
||||
int iMotorPwrMaxRaw; // Maximum power value for motor (raw value)
|
||||
int iMotorPwrStepCount; // Number of power steps
|
||||
|
||||
int iShellySupplyCount; // How many Shellies we have to switch the power supply
|
||||
char sShellySupplyTopic[MAX_SHELLIES_COUNT][MAX_PATH];
|
||||
};
|
||||
extern struct APP_SETTINGS settings;
|
||||
|
||||
void Settings_InitDefaultValues();
|
||||
void Settings_ReadConfFile();
|
||||
|
||||
#endif
|
||||
Reference in New Issue
Block a user