varch/test/test_pid.c
2024-11-16 13:36:54 +08:00

92 lines
1.7 KiB
C

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#if defined(TEST_TARGET_pid)
#include <varch/command.h>
#include <varch/pid.h>
#else
#include "init.h"
#include "command.h"
#include "pid.h"
#endif
static void test_sim(void)
{
PIDC pid;
pid_init(&pid);
pid.kp = 2;
pid.ki = 0.1;
pid.kd = 0.05;
pid.point = 100.0;
/* Simulate the process, assuming that the value of each cycle process increases by 5 */
for (int i = 0; i < 20; i++)
{
pid.process += 5;
pid_compute(&pid);
printf("Process Value: %.2f, PID Output: %.2f\n", pid.process, pid.output);
}
}
static void usage(void)
{
printf(
"Usage: pid [opt] [arg]\n"
"\n"
"options:\n"
" -h Print help\n"
" -v Print version\n"
"\n"
"argument:\n"
" <read> Test default read function\n"
" <write> Test default write function\n"
);
}
static int test(int argc, char *argv[])
{
/* reset getopt */
command_opt_init();
while (1)
{
int opt = command_getopt(argc, argv, "hv");
if (opt == -1) break;
switch (opt)
{
case 'v' :
printf("pid version %d.%d.%d\r\n", PID_V_MAJOR, PID_V_MINOR, PID_V_PATCH);
return 0;
case '?':
printf("Unknown option `%c`\r\n", command_optopt);
return -1;
case 'h' :
default:
usage();
return 0;
}
}
test_sim();
return 0;
}
#if defined(TEST_TARGET_pid)
int main(int argc, char *argv[])
{
return test(argc, argv);
}
#else
void test_pid(void)
{
command_export("pid", test);
}
init_export_app(test_pid);
#endif