#include <stdio.h>
#include <getopt.h>
#include <stdlib.h>
#include <signal.h>
static int running;
typedef enum test_mode_t{
DISABLED,
NORM,
MICROSECONDS,
SWEEP,
RADIO
}test_mode_t;
static void __print_usage(void)
{
printf("\n");
printf(" Options\n");
printf(" -c {channel} Specify one channel from 1-8.\n");
printf(" Otherwise all channels will be driven equally\n");
printf(" -f {hz} Specify pulse frequency, otherwise 50hz is used\n");
printf(" -p {position} Drive servo to a position between -1.5 & 1.5\n");
printf(" -w {width_us} Send pulse width in microseconds (us)\n");
printf(" -s {limit} Sweep servo back/forth between +- limit\n");
printf(" Limit can be between 0 & 1.5\n");
printf(" -r {ch} Use DSM radio channel {ch} to control servo\n");
printf(" -h Print this help messege \n\n");
printf("sample use to center servo channel 1:\n");
printf(" rc_test_servo -c 1 -p 0.0\n\n");
}
static void __signal_handler(__attribute__ ((unused)) int dummy)
{
running=0;
return;
}
int main(int argc, char *argv[])
{
double servo_pos=0;
double sweep_limit=0;
uint64_t dsm_nanos=0;
int width_us=0;
int ch=0;
double direction = 1;
test_mode_t mode = DISABLED;
int frequency_hz = 50;
int i, c, radio_ch;
opterr = 0;
while ((c = getopt(argc, argv, "c:f:p:w:s:r:h")) != -1){
switch (c){
case 'c':
ch = atoi(optarg);
fprintf(stderr,
"ERROR channel option must be between %d and %d\n",
RC_SERVO_CH_MIN, RC_SERVO_CH_MAX);
return -1;
}
break;
case 'f':
frequency_hz = atoi(optarg);
if(frequency_hz<1){
fprintf(stderr,"Frequency option must be >=1\n");
return -1;
}
break;
case 'p':
if(mode!=DISABLED){
__print_usage();
return -1;
}
servo_pos = atof(optarg);
if(servo_pos>1.5 || servo_pos<-1.5){
fprintf(stderr,"Servo position must be from -1.5 to 1.5\n");
return -1;
}
mode = NORM;
break;
case 'w':
if(mode!=DISABLED){
__print_usage();
return -1;
}
width_us = atof(optarg);
if(width_us<10){
fprintf(stderr,"ERROR: Width in microseconds must be >10\n");
return -1;
}
mode = MICROSECONDS;
break;
case 's':
if(mode!=DISABLED){
__print_usage();
return -1;
}
sweep_limit = atof(optarg);
if(sweep_limit>1.5 || sweep_limit<-1.5){
fprintf(stderr,"ERROR: Sweep limit must be from -1.5 to 1.5\n");
return -1;
}
mode = SWEEP;
servo_pos = 0.0;
break;
case 'r':
if(mode!=DISABLED){
__print_usage();
return -1;
}
radio_ch = atoi(optarg);
fprintf(stderr,"ERROR radio channel option must be between 1 and %d\n", RC_MAX_DSM_CHANNELS);
return -1;
}
mode = RADIO;
break;
case 'h':
__print_usage();
return 0;
default:
printf("\nInvalid Argument \n");
__print_usage();
return -1;
}
}
if(mode==DISABLED){
fprintf(stderr,"\nNot enough input arguments\n");
__print_usage();
return -1;
}
signal(SIGINT, __signal_handler);
running=1;
fprintf(stderr,"ERROR: failed to run rc_adc_init()\n");
return -1;
}
fprintf(stderr,"ERROR: battery disconnected or insufficiently charged to drive servos\n");
return -1;
}
if(mode==RADIO){
printf("Waiting for first DSM packet\n");
fflush(stdout);
if(running==0) return 0;
}
}
printf("Turning On 6V Servo Power Rail\n");
printf("\n");
if(ch==0) printf("Sending on all channels.\n");
else printf("Sending only to channel %d.\n", ch);
switch(mode){
case NORM:
printf("Using rc_servo_send_pulse_normalized\n");
printf("Normalized Signal: %f Pulse Frequency: %d\n", servo_pos, frequency_hz);
break;
case MICROSECONDS:
printf("Using rc_servo_send_pulse_us\n");
printf("Pulse_width: %d Pulse Frequency: %d\n", width_us, frequency_hz);
break;
case SWEEP:
printf("Sweeping servos back/forth between +-%f\n", sweep_limit);
printf("Pulse Frequency: %d\n", frequency_hz);
break;
case RADIO:
printf("Listening for DSM radio signal\n");
printf("Pulse Frequency: %d\n", frequency_hz);
break;
default:
fprintf(stderr,"ERROR invalid mode enum\n");
return -1;
}
while(running){
switch(mode){
case NORM:
break;
case MICROSECONDS:
break;
case SWEEP:
servo_pos += direction * sweep_limit / frequency_hz;
if(servo_pos>sweep_limit){
servo_pos = sweep_limit;
direction = -1;
}
else if(servo_pos < (-sweep_limit)){
servo_pos = -sweep_limit;
direction = 1;
}
break;
case RADIO:
if(dsm_nanos > 200000000){
printf("\rSeconds since last DSM packet: %.2f ", dsm_nanos/1000000000.0);
}
else{
if(servo_pos<-1.5) servo_pos=-1.5;
if(servo_pos>1.5) servo_pos=1.5;
printf("\r");
}
}
break;
default:
fprintf(stderr,"ERROR unhandled mode\n");
return -1;
}
}
return 0;
}