#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);
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);
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;
}
int rc_adc_init(void)
initializes the analog to digital converter for reading
int rc_adc_cleanup(void)
Cleans up the ADC subsystem.
double rc_adc_batt(void)
reads the voltage of the 2-cell Lithium battery
double rc_dsm_ch_normalized(int ch)
Returns a scaled value from -1 to 1 corresponding to the min and max values recorded during calibrati...
int rc_dsm_cleanup(void)
stops the DSM background service
int rc_dsm_channels(void)
fetches number of DSM channels currently being received.
#define RC_MAX_DSM_CHANNELS
Definition: dsm.h:35
int rc_dsm_resolution(void)
Used to determine if DSM packets are arriving with 10 or 11-bit resolution.
int rc_dsm_init(void)
Starts the DSM background service.
int64_t rc_dsm_nanos_since_last_packet(void)
Measures time since the last DSM packet was received.
int rc_dsm_is_new_data(void)
This is a check to see if new data is available.
#define RC_SERVO_CH_MIN
servo channels range from 1-8
Definition: servo.h:92
void rc_servo_cleanup(void)
Cleans up servo functionality and turns off the power rail.
int rc_servo_power_rail_en(int en)
enables or disables the 6V power rail to drive servos.
#define RC_SERVO_CH_MAX
servo channels range from 1-8
Definition: servo.h:93
int rc_servo_send_pulse_normalized(int ch, double input)
Like rc_send_pulse_us but translates a desired servo position from -1.5 to 1.5 to a corresponding pul...
int rc_servo_init(void)
Configures the PRU to send servo pulses.
int rc_servo_send_pulse_us(int ch, int us)
Sends a single pulse of desired width in microseconds to one or all channels.
void rc_usleep(unsigned int us)
Sleep in microseconds.