Compare commits

...

2 Commits

Author SHA1 Message Date
Bernhard
c4e21dca22 Änderung CYCLE_COUNTER_MAX für Testzwecke 2026-01-24 19:38:15 +01:00
Bernhard
450e93982e Im Simu-Mode Bedienung über Tastatur 2026-01-24 18:56:30 +01:00
8 changed files with 169 additions and 43 deletions

54
io/io.c
View File

@@ -2,17 +2,22 @@
#include "main.h" #include "main.h"
#include "io.h" #include "io.h"
#include <can/can_client.h> #include <can/can_client.h>
#include <settings/settings.h>
#include <mqtt/mqtt_client.h>
struct GPIO_KEY_DATA gpioKeyStop; struct GPIO_KEY_DATA gpioKeyStop;
struct GPIO_KEY_DATA gpioKeyPwrUp; struct GPIO_KEY_DATA gpioKeyPwrUp;
struct GPIO_KEY_DATA gpioKeyPwrDown; struct GPIO_KEY_DATA gpioKeyPwrDown;
char nInitialized = 0; char nInitialized = 0;
int iPowerSupplyOn = 0;
/// @brief Initialize the io pins /// @brief Initialize the io pins
/// @return /// @return
int IO_Init() int IO_Init()
{ {
iPowerSupplyOn = 0;
if (wiringPiSetupPinType(WPI_PIN_BCM)) if (wiringPiSetupPinType(WPI_PIN_BCM))
{ {
mylog(LOG_ERR, "IO: Set up wiringPi failed!"); mylog(LOG_ERR, "IO: Set up wiringPi failed!");
@@ -179,46 +184,57 @@ void IO_DoCyclic()
ReadKey(&gpioKeyPwrUp); ReadKey(&gpioKeyPwrUp);
ReadKey(&gpioKeyPwrDown); ReadKey(&gpioKeyPwrDown);
if (gpioKeyStop.iKeyValue) if (gpioKeyStop.iKeyValue || atomic_load(&abKeyStop))
{ {
// stop key is pressed // stop key is pressed
if (gpioKeyStop.iKeyRisingEdge) if (gpioKeyStop.iKeyRisingEdge || atomic_load(&abKeyStop))
{ {
mylog(LOG_INFO, "IO: KEY-Stop: Stop motor."); mylog(LOG_INFO, "IO: KEY-Stop: Stop motor.");
Can_SetMotorGear(0, 0); 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) if (motctrl[0].iMotorGear == MOTOR_GEAR_NEUTRAL)
{ {
mylog(LOG_INFO, "IO: KEY-Plus: Start motor."); mylog(LOG_INFO, "IO: KEY-Plus: Start motor.");
Can_SetMotorGear(0, 1); Can_SetMotorGear(0, 1);
Can_SetMotorPower(0, 1); Can_SetMotorPower(0, 1);
} }
else else if (motctrl[0].iMotorPowerSteps < settings.iMotorPwrStepCount)
{ {
mylog(LOG_INFO, "IO: KEY-Plus: Increase power."); mylog(LOG_INFO, "IO: KEY-Plus: Increase power.");
Can_SetMotorPower(0, motctrl[0].iMotorPowerSteps + 1); Can_SetMotorPower(0, motctrl[0].iMotorPowerSteps + 1);
} }
} }
else if (settings.iShellySupplyCount > 0)
if (gpioKeyPwrDown.iKeyRisingEdge)
{ {
// minus key is pressed -> decrease power // when drive is not ready and we have to switch the supply
if (motctrl[0].iMotorPowerSteps > 1) MqttClient_SwitchPowerSupply(1);
{ iPowerSupplyOn = 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);
}
} }
} }
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);
} }

View File

@@ -30,6 +30,8 @@ struct GPIO_KEY_DATA
int iKeyRepeatCycleCounter; int iKeyRepeatCycleCounter;
}; };
extern int iPowerSupplyOn;
int IO_Init(); int IO_Init();
void IO_DoCyclic(); void IO_DoCyclic();
void SetupKeyPin(struct GPIO_KEY_DATA *pdata, int iKeyPin); void SetupKeyPin(struct GPIO_KEY_DATA *pdata, int iKeyPin);

92
main.c
View File

@@ -4,12 +4,17 @@
#include <can/can_client.h> #include <can/can_client.h>
#include <io/io.h> #include <io/io.h>
#include <settings/settings.h> #include <settings/settings.h>
#include <termios.h>
// Period info of the realtime task // Period info of the realtime task
struct period_info pinfo; struct period_info pinfo;
int iThreadControl = 0; // 0: thread is running, <0: thread shall exit, >0 thread has exited
int iLogToConsole = 1; 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 /// @brief send a log message
/// @param prio /// @param prio
@@ -150,7 +155,7 @@ void *thread_func(void *data)
} }
// Connect to mqtt broker // Connect to mqtt broker
while (MqttClient_Connect() && (iThreadControl == 0)) while (MqttClient_Connect() && atomic_load(&asThreadControl) == 0)
{ {
mylog(LOG_ERR, "MqttClient_Connect() failed!"); mylog(LOG_ERR, "MqttClient_Connect() failed!");
sleep(10); sleep(10);
@@ -163,7 +168,7 @@ void *thread_func(void *data)
WriteOutputPin(GPIO_OUT_PWRON, HIGH); WriteOutputPin(GPIO_OUT_PWRON, HIGH);
// cyclic call of do_cyclic_1ms() // cyclic call of do_cyclic_1ms()
while (iThreadControl == 0) while (atomic_load(&asThreadControl) == 0)
{ {
pinfo.cyclecounter++; pinfo.cyclecounter++;
if (pinfo.cyclecounter > CYCLE_COUNTER_MAX) if (pinfo.cyclecounter > CYCLE_COUNTER_MAX)
@@ -185,11 +190,22 @@ void *thread_func(void *data)
Can_CloseInterface(0); Can_CloseInterface(0);
// signal thread has finnished // signal thread has finnished
iThreadControl = 1; atomic_store(&asThreadControl, 1);
return NULL; 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 /// @brief catch signals and set flag to terminate for the realtime thread
/// @param signo /// @param signo
void sig_handler(int signo) void sig_handler(int signo)
@@ -197,7 +213,7 @@ void sig_handler(int signo)
if ((signo == SIGINT) || (signo == SIGTERM)) if ((signo == SIGINT) || (signo == SIGTERM))
{ {
mylog(LOG_INFO, "Received signal %d", signo); mylog(LOG_INFO, "Received signal %d", signo);
iThreadControl = -1; // signal realtime thread to exit atomic_store(&asThreadControl, -1); // signal realtime thread to exit
} }
} }
@@ -222,16 +238,24 @@ int main(int argc, char* argv[])
// Read the settings file after opening the log // Read the settings file after opening the log
Settings_ReadConfFile(); Settings_ReadConfFile();
// catch signals if (settings.iCanSimu)
if (signal(SIGTERM, sig_handler) == SIG_ERR)
{ {
mylog(LOG_ERR, "Can't catch SIGTERM"); // in simulation mode switch terminal to raw mode
exit(-1); set_conio_terminal_mode();
} }
if (signal(SIGINT, sig_handler) == SIG_ERR) else
{ {
mylog(LOG_ERR, "Can't catch SIGINT"); // catch signals
exit(-2); 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 */ /* Lock memory */
@@ -281,7 +305,7 @@ int main(int argc, char* argv[])
/* Create a pthread with specified attributes */ /* Create a pthread with specified attributes */
iThreadControl = 0; atomic_store(&asThreadControl, 0);
ret = pthread_create(&thread, &attr, thread_func, NULL); ret = pthread_create(&thread, &attr, thread_func, NULL);
if (ret) if (ret)
{ {
@@ -289,6 +313,48 @@ int main(int argc, char* argv[])
goto out; 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 // join the thread and wait for it to exit
ret = pthread_join(thread, NULL); ret = pthread_join(thread, NULL);
if (ret) if (ret)

10
main.h
View File

@@ -13,8 +13,12 @@
#include <signal.h> #include <signal.h>
#include <syslog.h> #include <syslog.h>
#include <stdarg.h> #include <stdarg.h>
#include <stdatomic.h>
#include <stdbool.h>
#define CYCLE_COUNTER_MAX 86400000
//#define CYCLE_COUNTER_MAX 86400000
#define CYCLE_COUNTER_MAX 600000 // for testing only
struct period_info struct period_info
{ {
@@ -24,6 +28,10 @@ struct period_info
float fStartTime; float fStartTime;
}; };
extern atomic_bool abKeyPlus;
extern atomic_bool abKeyMinus;
extern atomic_bool abKeyStop;
extern struct period_info pinfo; extern struct period_info pinfo;
extern void mylog(int prio, const char *format, ...); extern void mylog(int prio, const char *format, ...);

View File

@@ -301,4 +301,17 @@ void MqttClient_Publish_MotorActualPowerW(int iMotorIndex, int iMotorPowerW)
mosquitto_publish(mosq, NULL, mqtt_topic_motor2_actualpowerw, strlen(message), message, 0, false); 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);
} }

View File

@@ -11,5 +11,6 @@ void MqttClient_Publish_MotorGear(int iMotorIndex, int iGear);
void MqttClient_Publish_MotorPower(int iMotorIndex, int iPower); void MqttClient_Publish_MotorPower(int iMotorIndex, int iPower);
void MqttClient_Publish_MotorSwitchState(int iMotorIndex, unsigned char nSwitchState); void MqttClient_Publish_MotorSwitchState(int iMotorIndex, unsigned char nSwitchState);
void MqttClient_Publish_MotorActualPowerW(int iMotorIndex, int iMotorPowerW); void MqttClient_Publish_MotorActualPowerW(int iMotorIndex, int iMotorPowerW);
void MqttClient_SwitchPowerSupply(int on);
#endif #endif

View File

@@ -23,6 +23,12 @@ void Settings_InitDefaultValues()
settings.iMotorPwrMaxRaw = 250; settings.iMotorPwrMaxRaw = 250;
settings.iMotorPwrStepCount = 7; 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 // Get path of the executable itself
ssize_t length = readlink("/proc/self/exe", settings.sExePath, sizeof(settings.sExePath) - 1); ssize_t length = readlink("/proc/self/exe", settings.sExePath, sizeof(settings.sExePath) - 1);
if (length >= 0) if (length >= 0)
@@ -74,12 +80,12 @@ void Settings_ReadConfFile()
//const char *filename = "/etc/CanRtDriver.conf"; //const char *filename = "/etc/CanRtDriver.conf";
char filename[MAX_PATH + 50]; char filename[MAX_PATH + 50];
sprintf(filename, "%s.conf", settings.sExePath); sprintf(filename, "/etc/CanRtDriver.conf");
FILE *file = fopen(filename, "r"); FILE *file = fopen(filename, "r");
if (file == NULL) if (file == NULL)
{ {
mylog(LOG_INFO, "SETTINGS: File %s noch found", filename); mylog(LOG_INFO, "SETTINGS: File %s noch found", filename);
sprintf(filename, "/etc/CanRtDriver.conf"); sprintf(filename, "%s.conf", settings.sExePath);
file = fopen(filename, "r"); file = fopen(filename, "r");
if (file == NULL) if (file == NULL)
{ {
@@ -108,43 +114,53 @@ void Settings_ReadConfFile()
if ((key != NULL) && (value != NULL)) if ((key != NULL) && (value != NULL))
{ {
//mylog(LOG_INFO, "SETTINGS: Found key: '%s' | value: '%s'", key, value);
if (strcmp(key, "DebugLevel") == 0) if (strcmp(key, "DebugLevel") == 0)
{ {
// how many log messages we want to see
settings.iDebugLevel = atoi(value); settings.iDebugLevel = atoi(value);
mylog(LOG_DEBUG, "SETTINGS: %s = %d", key, settings.iDebugLevel); mylog(LOG_DEBUG, "SETTINGS: %s = %d", key, settings.iDebugLevel);
} }
else if (strcmp(key, "CanSimu") == 0) else if (strcmp(key, "CanSimu") == 0)
{ {
// shall we do simulation
settings.iCanSimu = atoi(value); settings.iCanSimu = atoi(value);
mylog(LOG_DEBUG, "SETTINGS: %s = %d", key, settings.iCanSimu); mylog(LOG_DEBUG, "SETTINGS: %s = %d", key, settings.iCanSimu);
} }
else if (strcmp(key, "MotorPowerMinRaw") == 0) else if (strcmp(key, "MotorPowerMinRaw") == 0)
{ {
// Minimum power value for the motors
settings.iMotorPwrMinRaw = atoi(value); settings.iMotorPwrMinRaw = atoi(value);
mylog(LOG_DEBUG, "SETTINGS: %s = %d", key, settings.iMotorPwrMinRaw); mylog(LOG_DEBUG, "SETTINGS: %s = %d", key, settings.iMotorPwrMinRaw);
} }
else if (strcmp(key, "MotorPowerMaxRaw") == 0) else if (strcmp(key, "MotorPowerMaxRaw") == 0)
{ {
// Maximum power value for the motors
settings.iMotorPwrMaxRaw = atoi(value); settings.iMotorPwrMaxRaw = atoi(value);
mylog(LOG_DEBUG, "SETTINGS: %s = %d", key, settings.iMotorPwrMaxRaw); mylog(LOG_DEBUG, "SETTINGS: %s = %d", key, settings.iMotorPwrMaxRaw);
} }
else if (strcmp(key, "MotorPowerStepCount") == 0) else if (strcmp(key, "MotorPowerStepCount") == 0)
{ {
// How many steps do we want to have switching the power
settings.iMotorPwrStepCount = atoi(value); settings.iMotorPwrStepCount = atoi(value);
mylog(LOG_DEBUG, "SETTINGS: %s = %d", key, settings.iMotorPwrStepCount); 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 else
{ {
mylog(LOG_WARNING, "SETTING: Unknown key: %s", key); mylog(LOG_WARNING, "SETTING: Unknown key: %s", key);
} }
// // Beispiel: Wert verarbeiten
// if (strcmp(key, "port") == 0)
// {
// int port = atoi(value);
// printf(" [System] Port auf %d gesetzt.\n", port);
// }
} }
} }

View File

@@ -9,6 +9,7 @@
#define MAX_LINE_LENGTH 256 #define MAX_LINE_LENGTH 256
#define MAX_PATH 256 #define MAX_PATH 256
#define MAX_SHELLIES_COUNT 5
struct APP_SETTINGS struct APP_SETTINGS
{ {
@@ -20,6 +21,9 @@ struct APP_SETTINGS
int iMotorPwrMinRaw; // Minimum power value for motor (raw value) int iMotorPwrMinRaw; // Minimum power value for motor (raw value)
int iMotorPwrMaxRaw; // Maximum power value for motor (raw value) int iMotorPwrMaxRaw; // Maximum power value for motor (raw value)
int iMotorPwrStepCount; // Number of power steps 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; extern struct APP_SETTINGS settings;