#include <stdio.h>
#include <signal.h>
#include <stdlib.h>
#include <getopt.h>
static int running = 0;
typedef enum m_mode_t{
DISABLED,
NORMAL,
BRAKE,
FREE,
SWEEP
} m_mode_t;
static void __print_usage(void)
{
printf("\n");
printf("-d {duty} define a duty cycle from -1.0 to 1.0\n");
printf("-b enable motor brake function\n");
printf("-F {freq} set a custom pwm frequency in HZ, otherwise default 25000 is used\n");
printf("-f enable free spin function\n");
printf("-s {duty} sweep motors back and forward at duty cycle\n");
printf("-m {motor} specify a single motor from 1-4, otherwise all will be driven\n");
printf(" motors will be driven equally.\n");
printf("-h print this help message\n");
printf("\n");
}
static void __signal_handler(__attribute__ ((unused)) int dummy)
{
running=0;
return;
}
int main(int argc, char *argv[])
{
double duty = 0.0;
int ch = 0;
int c, in;
m_mode_t m_mode = DISABLED;
opterr = 0;
while ((c = getopt(argc, argv, "m:d:F:fbs:h")) != -1){
switch (c){
case 'm':
in = atoi(optarg);
if(in<=4 && in>=0){
ch = in;
}
else{
fprintf(stderr,"-m motor option must be from 0-4\n");
return -1;
}
break;
case 'd':
if(m_mode!=DISABLED) __print_usage();
duty = atof(optarg);
if(duty<=1 && duty >=-1){
m_mode = NORMAL;
}
else{
fprintf(stderr,"duty cycle must be from -1 to 1\n");
return -1;
}
break;
case 'F':
freq_hz = atoi(optarg);
if(freq_hz<1){
fprintf(stderr,"PWM frequency must be >=1\n");
return -1;
}
break;
case 'f':
if(m_mode!=DISABLED) __print_usage();
m_mode = FREE;
break;
case 'b':
if(m_mode!=DISABLED) __print_usage();
m_mode = BRAKE;
break;
case 's':
if(m_mode!=DISABLED) __print_usage();
duty = atof(optarg);
if(duty<=1 && duty >=-1){
m_mode = SWEEP;
}
else{
fprintf(stderr,"duty cycle must be from -1 to 1\n");
return -1;
}
break;
case 'h':
__print_usage();
return -1;
break;
default:
__print_usage();
return -1;
break;
}
}
if(m_mode==DISABLED){
__print_usage();
return -1;
}
signal(SIGINT, __signal_handler);
running =1;
switch(m_mode){
case NORMAL:
printf("sending duty cycle %0.4f\n", duty);
break;
case FREE:
printf("Free Spin Mode\n");
break;
case BRAKE:
printf("Braking Mode\n");
break;
default:
break;
}
while(running){
if(m_mode==SWEEP){
duty = -duty;
printf("sending duty cycle %0.4f\n", duty);
fflush(stdout);
}
}
printf("\ncalling rc_motor_cleanup()\n");
return 0;
}