#include <stdio.h>
#include <getopt.h>
#include <stdlib.h>
#include <signal.h>
#include <rc/time.h>
#include <rc/adc.h>
#include <rc/dsm.h>
#include <rc/servo.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);
if(ch<RC_SERVO_CH_MIN || ch>RC_SERVO_CH_MAX){
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);
if(radio_ch<1 || radio_ch>RC_MAX_DSM_CHANNELS){
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;
if(rc_adc_init()){
fprintf(stderr,"ERROR: failed to run rc_adc_init()\n");
return -1;
}
if(rc_adc_batt()<6.0){
fprintf(stderr,"ERROR: battery disconnected or insufficiently charged to drive servos\n");
return -1;
}
rc_adc_cleanup();
if(rc_servo_init()) return -1;
if(mode==RADIO){
if(rc_dsm_init()==-1) return -1;
printf("Waiting for first DSM packet\n");
fflush(stdout);
while(rc_dsm_is_new_data()==0){
if(running==0) return 0;
rc_usleep(50000);
}
}
printf("Turning On 6V Servo Power Rail\n");
rc_servo_power_rail_en(1);
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:
if(rc_servo_send_pulse_normalized(ch,servo_pos)==-1) return -1;
break;
case MICROSECONDS:
if(rc_servo_send_pulse_us(ch, width_us)==-1) return -1;
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;
}
if(rc_servo_send_pulse_normalized(ch,servo_pos)==-1) return -1;
break;
case RADIO:
dsm_nanos = rc_dsm_nanos_since_last_packet();
if(dsm_nanos > 200000000){
printf("\rSeconds since last DSM packet: %.2f ", dsm_nanos/1000000000.0);
}
else{
servo_pos = rc_dsm_ch_normalized(radio_ch);
if(servo_pos<-1.5) servo_pos=-1.5;
if(servo_pos>1.5) servo_pos=1.5;
if(rc_servo_send_pulse_normalized(ch,servo_pos)==-1) return -1;
printf("\r"); printf("%d/", rc_dsm_resolution());
printf("%d-ch ", rc_dsm_channels());
for(i=0; i<rc_dsm_channels(); i++){
printf("%d:% 0.2f ", i+1, rc_dsm_ch_normalized(i+1));
}
}
break;
default:
fprintf(stderr,"ERROR unhandled mode\n");
return -1;
}
rc_usleep(1000000/frequency_hz);
}
rc_usleep(50000);
rc_servo_power_rail_en(0);
rc_servo_cleanup();
rc_dsm_cleanup();
return 0;
}