./git_commit_push.sh
This commit is contained in:
93
main.c
93
main.c
@@ -7,6 +7,38 @@
|
||||
// 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;
|
||||
|
||||
|
||||
/// @brief send a log message
|
||||
/// @param prio
|
||||
/// @param format
|
||||
/// @param
|
||||
void mylog(int prio, const char *format, ...)
|
||||
{
|
||||
va_list args;
|
||||
|
||||
// 1. Initialisiere die Argumentenliste mit dem letzten festen Parameter
|
||||
va_start(args, format);
|
||||
|
||||
// 2. Übergabe an vsyslog (statt syslog)
|
||||
// vsyslog nimmt eine va_list entgegen
|
||||
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");
|
||||
}
|
||||
|
||||
// 4. Aufräumen
|
||||
va_end(args);
|
||||
}
|
||||
|
||||
|
||||
/// @brief Initialize period_info with period_ms for cyclic task
|
||||
/// @param period_ms
|
||||
@@ -77,7 +109,6 @@ static void do_cyclic_1ms(struct period_info *pinfo)
|
||||
// called every 100ms
|
||||
nCalled |= 0x0010;
|
||||
Can_TransmitMotorGear(0);
|
||||
//printf("%.3f: 100ms-Cycle %ld...\n", clock_gettime_s() - pinfo->fStartTime, pinfo->cyclecounter);
|
||||
}
|
||||
|
||||
if (((pinfo->cyclecounter + 20) % 100) == 0)
|
||||
@@ -89,14 +120,9 @@ static void do_cyclic_1ms(struct period_info *pinfo)
|
||||
|
||||
if (((pinfo->cyclecounter + 30) % 500) == 0)
|
||||
{
|
||||
// called every 250ms
|
||||
// called every 500ms
|
||||
MqttClient_Publisher();
|
||||
}
|
||||
|
||||
if (nCalled > 0)
|
||||
{
|
||||
//printf("%.3f: Called 0x%.4X at cycle %ld...\n", clock_gettime_s() - pinfo->fStartTime, nCalled, pinfo->cyclecounter);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -105,31 +131,31 @@ static void do_cyclic_1ms(struct period_info *pinfo)
|
||||
/// @return
|
||||
void *thread_func(void *data)
|
||||
{
|
||||
// Initialize IO Ports
|
||||
if (IO_Init())
|
||||
{
|
||||
mylog(LOG_ERR, "IO_Init() failed!");
|
||||
return NULL;
|
||||
}
|
||||
|
||||
// Open CAN interface first motor
|
||||
if (Can_OpenInterface(0, "can0"))
|
||||
{
|
||||
printf("Can_OpenInterface() failed!\n");
|
||||
mylog(LOG_ERR, "Can_OpenInterface() failed!");
|
||||
return NULL;
|
||||
}
|
||||
|
||||
// Connect to mqtt broker
|
||||
while (MqttClient_Connect() && (iThreadControl == 0))
|
||||
{
|
||||
printf("MqttClient_Connect() failed!\n");
|
||||
mylog(LOG_ERR, "MqttClient_Connect() failed!");
|
||||
sleep(10);
|
||||
}
|
||||
|
||||
// Initialize IO Ports
|
||||
if (IO_Init())
|
||||
{
|
||||
printf("IO_Init() failed!\n");
|
||||
return NULL;
|
||||
}
|
||||
|
||||
// initialize cyclic task
|
||||
periodic_task_init(1, &pinfo);
|
||||
|
||||
// "Zündung" ein
|
||||
// Ignition on
|
||||
WriteOutputPin(GPIO_OUT_PWRON, HIGH);
|
||||
|
||||
// cyclic call of do_cyclic_1ms()
|
||||
@@ -145,7 +171,7 @@ void *thread_func(void *data)
|
||||
wait_rest_of_period(&pinfo);
|
||||
}
|
||||
|
||||
// "Zündung" aus
|
||||
// Ignition off
|
||||
WriteOutputPin(GPIO_OUT_PWRON, LOW);
|
||||
|
||||
// Disconnect from mqtt broker
|
||||
@@ -154,6 +180,7 @@ void *thread_func(void *data)
|
||||
// Close CAN interface
|
||||
Can_CloseInterface(0);
|
||||
|
||||
// signal thread has finnished
|
||||
iThreadControl = 1;
|
||||
return NULL;
|
||||
}
|
||||
@@ -165,13 +192,13 @@ void sig_handler(int signo)
|
||||
{
|
||||
if ((signo == SIGINT) || (signo == SIGTERM))
|
||||
{
|
||||
printf("Received signal %d\n", signo);
|
||||
mylog(LOG_INFO, "Received signal %d", signo);
|
||||
iThreadControl = -1; // signal realtime thread to exit
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/// @brief Hauptfunktion Echtzeit-Task erstellen und starten
|
||||
/// @brief Main function, create and start realtime task
|
||||
/// @param argc
|
||||
/// @param argv
|
||||
/// @return
|
||||
@@ -182,22 +209,25 @@ int main(int argc, char* argv[])
|
||||
pthread_t thread;
|
||||
int ret;
|
||||
|
||||
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)
|
||||
{
|
||||
printf("Can't catch SIGTERM\n");
|
||||
mylog(LOG_ERR, "Can't catch SIGTERM");
|
||||
exit(-1);
|
||||
}
|
||||
if (signal(SIGINT, sig_handler) == SIG_ERR)
|
||||
{
|
||||
printf("Can't catch SIGINT\n");
|
||||
mylog(LOG_ERR, "Can't catch SIGINT");
|
||||
exit(-2);
|
||||
}
|
||||
|
||||
/* Lock memory */
|
||||
if(mlockall(MCL_CURRENT | MCL_FUTURE) == -1)
|
||||
{
|
||||
printf("mlockall failed: %m\n");
|
||||
mylog(LOG_ERR, "mlockall failed: %m");
|
||||
exit(-3);
|
||||
}
|
||||
|
||||
@@ -205,7 +235,7 @@ int main(int argc, char* argv[])
|
||||
ret = pthread_attr_init(&attr);
|
||||
if (ret)
|
||||
{
|
||||
printf("init pthread attributes failed\n");
|
||||
mylog(LOG_ERR, "init pthread attributes failed");
|
||||
goto out;
|
||||
}
|
||||
|
||||
@@ -213,7 +243,7 @@ int main(int argc, char* argv[])
|
||||
ret = pthread_attr_setstacksize(&attr, PTHREAD_STACK_MIN);
|
||||
if (ret)
|
||||
{
|
||||
printf("pthread setstacksize failed\n");
|
||||
mylog(LOG_ERR, "pthread setstacksize failed");
|
||||
goto out;
|
||||
}
|
||||
|
||||
@@ -221,21 +251,21 @@ int main(int argc, char* argv[])
|
||||
ret = pthread_attr_setschedpolicy(&attr, SCHED_FIFO);
|
||||
if (ret)
|
||||
{
|
||||
printf("pthread setschedpolicy failed\n");
|
||||
mylog(LOG_ERR, "pthread setschedpolicy failed");
|
||||
goto out;
|
||||
}
|
||||
param.sched_priority = 99; // Priority between 1 (low) and 99() high)
|
||||
ret = pthread_attr_setschedparam(&attr, ¶m);
|
||||
if (ret)
|
||||
{
|
||||
printf("pthread setschedparam failed\n");
|
||||
mylog(LOG_ERR, "pthread setschedparam failed");
|
||||
goto out;
|
||||
}
|
||||
/* Use scheduling parameters of attr */
|
||||
ret = pthread_attr_setinheritsched(&attr, PTHREAD_EXPLICIT_SCHED);
|
||||
if (ret)
|
||||
{
|
||||
printf("pthread setinheritsched failed\n");
|
||||
mylog(LOG_ERR, "pthread setinheritsched failed");
|
||||
goto out;
|
||||
}
|
||||
|
||||
@@ -245,7 +275,7 @@ int main(int argc, char* argv[])
|
||||
ret = pthread_create(&thread, &attr, thread_func, NULL);
|
||||
if (ret)
|
||||
{
|
||||
printf("create pthread failed\n");
|
||||
mylog(LOG_ERR, "create pthread failed");
|
||||
goto out;
|
||||
}
|
||||
|
||||
@@ -253,10 +283,13 @@ int main(int argc, char* argv[])
|
||||
ret = pthread_join(thread, NULL);
|
||||
if (ret)
|
||||
{
|
||||
printf("faild to join thread!\n");
|
||||
mylog(LOG_ERR, "faild to join thread!");
|
||||
}
|
||||
|
||||
out:
|
||||
mylog(LOG_INFO, "Service quit.");
|
||||
closelog();
|
||||
|
||||
return ret;
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user