1- /*
1+ /*
22 * Copyright (C) 2010 RobotCub Consortium, European Commission FP6 Project IST-004370
33 * Author: Marco Randazzo
44 * email: marco.randazzo@iit.it
@@ -104,13 +104,13 @@ using namespace yarp::math;
104104#define PRINT_STATUS_PER 0.1
105105#define MAX_AXES 32
106106
107- enum { JTYPE_UNDEF=-1 , JTYPE_POLAR=0 , JTYPE_CARTESIAN=1 , JTYPE_CONSTANT=2 , JTYPE_STRING=3 };
107+ enum { JTYPE_UNDEF=-1 , JTYPE_POLAR=0 , JTYPE_CARTESIAN=1 , JTYPE_CONSTANT=2 , JTYPE_STRING=3 , JTYPE_BUTTON= 4 };
108108
109109class CtrlThread : public PeriodicThread
110110{
111111 struct struct_jointProperties
112112 {
113- int type;
113+ int type;
114114 int param[3 ];
115115 string param_s;
116116 };
@@ -131,7 +131,7 @@ class CtrlThread: public PeriodicThread
131131 int numButtons;
132132 int joy_id;
133133 SDL_Joystick* joy1;
134-
134+
135135 // button actions;
136136 string button_actions [20 ];
137137
@@ -215,7 +215,7 @@ class CtrlThread: public PeriodicThread
215215 }
216216
217217 Bottle b;
218-
218+
219219
220220 b = rf.findGroup (" INPUTS" ).findGroup (" Reverse" );
221221 if (b.size ()-1 == num_inputs)
@@ -292,6 +292,19 @@ class CtrlThread: public PeriodicThread
292292 jointProperties[i].param [2 ]=0 ;
293293 }
294294 else
295+ if (b.get (1 ).asString ()==" button_as_axis" )
296+ {
297+ if (b.size () < 5 )
298+ {
299+ yError (" Configuration error: 'button_as_axis' for Ax%d requires 3 parameters (button_num, low_val, high_val)" , i);
300+ return false ;
301+ }
302+ jointProperties[i].type =JTYPE_BUTTON;
303+ jointProperties[i].param [0 ]=b.get (2 ).asInt32 (); // button num
304+ jointProperties[i].param [1 ]=b.get (3 ).asInt32 (); // low value
305+ jointProperties[i].param [2 ]=b.get (4 ).asInt32 (); // high value
306+ }
307+ else
295308 if (b.get (1 ).asString ()==" constant" )
296309 {
297310 jointProperties[i].type =JTYPE_CONSTANT;
@@ -345,7 +358,7 @@ class CtrlThread: public PeriodicThread
345358 yInfo ( " associating the following actions to the buttons: \n " );
346359 for (int iii = 0 ; iii < 20 ; iii++){
347360 char tmp[80 ];
348- sprintf (tmp, " button%d" , iii);
361+ sprintf (tmp, " button%d" , iii);
349362 if (exec_comm_bottle.check (tmp))
350363 {
351364 button_actions[iii] = exec_comm_bottle.find (tmp).toString ();
@@ -448,7 +461,7 @@ class CtrlThread: public PeriodicThread
448461 yInfo ( " \n " );
449462
450463 // check: selected joint MUST have at least one axis
451- if (numAxes<=0 )
464+ if (numAxes<=0 )
452465 {
453466 yError ( " Error: selected joystick has %d Axes?!\n " ,numAxes );
454467 return false ;
@@ -463,7 +476,7 @@ class CtrlThread: public PeriodicThread
463476 yWarning ( " Do you want to continue anyway (y/n)?\n " );
464477 char input[255 ];
465478 cin >> input;
466- if (input[0 ]!=' y' && input[0 ]!=' Y' )
479+ if (input[0 ]!=' y' && input[0 ]!=' Y' )
467480 {
468481 yInfo ( " Quitting...\n " );
469482 return false ;
@@ -583,7 +596,7 @@ class CtrlThread: public PeriodicThread
583596 Bottle axis_data;
584597 Bottle buttons_data;
585598 for (int i=0 ;i<num_outputs;i++)
586- {
599+ {
587600 if (jointProperties[i].type == JTYPE_POLAR)
588601 {
589602 if (jointProperties[i].param [2 ] == 0 )
@@ -610,6 +623,26 @@ class CtrlThread: public PeriodicThread
610623 {
611624 outAxes[i]=(jointProperties[i].param [0 ]);
612625 }
626+ else if (jointProperties[i].type == JTYPE_BUTTON)
627+ {
628+ const int button_idx = jointProperties[i].param [0 ];
629+ if (button_idx>=0 && button_idx<numButtons)
630+ {
631+ if (rawButtons[button_idx] == 0 )
632+ {
633+ outAxes[i] = jointProperties[i].param [1 ];
634+ }
635+ else
636+ {
637+ outAxes[i] = jointProperties[i].param [2 ];
638+ }
639+ }
640+ else
641+ {
642+ yWarning ( " Button id out of bounds (%d<%d)" ,button_idx,numButtons);
643+ outAxes[i] = 0.0 ;
644+ }
645+ }
613646 else
614647 {
615648 outAxes[i]=0.0 ;
@@ -647,14 +680,14 @@ class CtrlThread: public PeriodicThread
647680 action += " " ;
648681 // if true, the behavior is like a button, one esecution per press
649682 // if false, execution is continuous
650- bool trigger_mode=true ;
683+ bool trigger_mode=true ;
651684 switch (rawHats[i])
652685 {
653686 case SDL_HAT_UP:
654687 action += " up" ;
655688 break ;
656689 case SDL_HAT_RIGHT:
657- action += " right" ;
690+ action += " right" ;
658691 break ;
659692 case SDL_HAT_DOWN:
660693 action += " down" ;
@@ -686,7 +719,7 @@ class CtrlThread: public PeriodicThread
686719 }
687720 }
688721 else
689- {
722+ {
690723 yInfo (" executing script %d: %s\n " , i, action.c_str ());
691724 int ret = system (action.c_str ());
692725 }
@@ -700,7 +733,7 @@ class CtrlThread: public PeriodicThread
700733
701734 // Preparing data to be sent on the yarp ports
702735 for (int i=0 ;i<num_outputs;i++)
703- {
736+ {
704737 if ( jointProperties[i].type == JTYPE_STRING)
705738 data.addString (jointProperties[i].param_s .c_str ());
706739 else
@@ -737,7 +770,7 @@ class CtrlThread: public PeriodicThread
737770 }
738771
739772 virtual void threadRelease ()
740- {
773+ {
741774 if (rawAxes) delete [] rawAxes;
742775 if (rawHats) delete [] rawHats;
743776 if (rawHatsOld) delete [] rawHatsOld;
0 commit comments