#include <stdio.h>
#include <getopt.h>
#include <signal.h>
#include <rc/dsm.h>
#include <rc/servo.h>
#include <rc/time.h>
#include <rc/adc.h>
static int running;
typedef enum p_mode_t{
NONE,
POWERON,
POWEROFF
} p_mode_t;
static void __send_pulses(void)
{
int i, ch, val;
for(i=1; i<=8; i++){
val=rc_dsm_ch_raw(i);
if(val>0) rc_servo_send_pulse_us(i,val);
}
printf("\r");
ch = rc_dsm_channels();
for(i=1;i<=ch;i++){
printf("% 4d ", rc_dsm_ch_raw(i));
}
fflush(stdout);
return;
}
static void __signal_handler(__attribute__ ((unused)) int dummy)
{
running=0;
return;
}
static void __print_usage(void)
{
printf("\n");
printf(" Options\n");
printf(" -s Enable 6V power rail for servos.\n");
printf(" -e Disable 6V power rail for ESCs.\n");
printf(" -h Print this messege.\n\n");
return;
}
int main(int argc, char *argv[])
{
int c;
p_mode_t mode = NONE;
opterr = 0;
while ((c = getopt(argc, argv, "seh")) != -1){
switch (c){
case 's': mode = POWERON;
break;
case 'e': mode = POWEROFF;
break;
case 'h': __print_usage();
return -1;
break;
default:
fprintf(stderr,"Invalid Argument \n");
__print_usage();
return -1;
}
}
if(mode == NONE){
fprintf(stderr,"You must select a power mode -s or -e\n");
__print_usage();
return -1;
}
if(rc_dsm_init()==-1) return -1;
if(mode == POWERON){
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 motors\n");
return -1;
}
rc_adc_cleanup();
if(rc_servo_power_rail_en(1)){
fprintf(stderr,"failed to enable power rail\n");
return -1;
}
}
printf("1:Thr ");
printf("2:Roll ");
printf("3:Pitch ");
printf("4:Yaw ");
printf("5:Kill ");
printf("6:Mode ");
printf("7:Aux1 ");
printf("8:Aux2 ");
printf("\n");
printf("Waiting for DSM Connection");
fflush(stdout);
signal(SIGINT, __signal_handler);
running=1;
rc_dsm_set_callback(&__send_pulses);
while(running){
if(rc_dsm_is_connection_active()==0){
printf("\rSeconds since last DSM packet: ");
printf("%0.1f ", rc_dsm_nanos_since_last_packet()/1000000000.0);
printf(" ");
fflush(stdout);
}
rc_usleep(25000);
}
printf("\n");
rc_dsm_cleanup();
return 0;
}