(ALLEGROHAND1.lua)
(myAHConroller.cpp)
 
(28 intermediate revisions by 2 users not shown)
Line 1: Line 1:
In the last few tutorials, we verified created a control algorithm project and verified our setup by performing a simple user-defined motion on both the virtual and actual Allegro Hands. We did all this ignoring the features of Allegro Hand Application Studio (AHAS) as a robust GUI interface for controlling the hand and visualizing certain data.
+
In the last few tutorials, we created a control algorithm project and verified our setup by performing a simple user-defined motion on both the virtual and actual Allegro Hands. We did all this ignoring the features of Allegro Hand Application Studio (AHAS) as a robust GUI interface for controlling the hand and visualizing certain data.
  
 
In this tutorial, we will take advantage of '''buttons''' in AHAS and use them to invoke our own algorithms and those supplied along with AHAS in the BHand Library.
 
In this tutorial, we will take advantage of '''buttons''' in AHAS and use them to invoke our own algorithms and those supplied along with AHAS in the BHand Library.
Line 5: Line 5:
 
==AHAS Buttons==
 
==AHAS Buttons==
  
In your AHAS ''bin/controls'' directory, locate the file ''ALLEGROHAND1.lua''. This is the file that we made in Tutorial 1 which links to out new controller. Let's open up ''ALLEGROHAND1.lua'' and take a look.
+
Let's open up ''myAHController.lua'' and take a look.<br>
 
+
This is the file that we made in Tutorial 1 which links to out new controller.<br>
The function ''OnGUI()'' is the area of interest. This function contains the buttons and joint angle sliders we seen on the AHAS GUI. To create a button and link its press to a command is as simple as follows:
+
The function ''OnGUI()'' is the area of interest. This function contains the buttons we seen on the AHAS GUI.<br>
 +
To create a button and link its press to a command is as simple as follows:
  
 
<syntaxhighlight lang="lua">
 
<syntaxhighlight lang="lua">
 
...
 
...
 
+
-- ROW 1
    -- if button is pressed
+
row = 1
    -- GUIButton(x-pos, y-pos, width, height, "name")
+
col = 1
     if (GUIButton(5, 35, 120, 25, "Home")) then
+
button_image = Image.new("buttonImages/home_off.png")
 
+
if (isHome) then
      -- command to be send to controller:
+
button_image = Image.new("buttonImages/home_on.png")
      -- RCMD_GO_HOME, RCMD_GO_READY or RCMD_USER + #
+
end
      sendCommand("HandController", RCMD_GO_HOME, VK_H)
+
     if (GUI.Button(Rect.new(button(row,col)[1], button(row,col)[2], button(row,col)[3], button(row,col)[4]),  button_image)) then
 
+
controller:command(RCMD_GO_HOME, VK_H)
      -- command prompt output
+
print("Home button pressed")
      print("Home button pressed")
+
allMotionsFalse()
    end
+
if (isConnected) then isHome = true end
 
+
end
 
...
 
...
 
</syntaxhighlight >
 
</syntaxhighlight >
  
This button, when pressed, will cause the hand to assume its ''Home'' position. As mentioned in the comments, the arguments of ''GUIButtons()'' specify the button's location and the string displayed on the button.
+
This button, when pressed, will cause the hand to assume its ''Home'' position. As mentioned in the comments, the arguments of ''GUI.Button()'' specify the button's Rect and the image displayed on the button.
  
'''x-pos:''' button's distance from the left side of the AHAS window (pixels)<br>
+
The function ''controller:command()'' is used to forward a command to the hand controller, the DLL plug-in, ''myAHController.dll''.
'''y-pos:''' button's distance from the top of the AHAS window (pixels)<br>
+
'''width:''' button width (pixels)<br>
+
'''height:''' button height (pixels)<br>
+
  
The function ''sendCommand'' is used to forward a command to the hand controller, the DLL plug-in, ''control_AllegroHand.dll''.
+
Notice that all of the button code to command the Allegro Hand library motion is already available within the original LUA file, yet, if clicked, these buttons do nothing. We must develop the ''command()'' function in ''myAHController.cpp'' to handle the incoming commands.
 
+
<syntaxhighlight lang="lua">
+
...
+
 
+
-- "HandController", name assigned to controller plugin control_AllegroHand.dll (linked through control_AllegroHand.xdl)
+
-- code from function Awake()
+
  -- createController("HandController", "hand") --, 1, "controls/control_AllegroHand.xdl", "")
+
  -- setControlAlgorithm("HandController", "controls/control_AllegroHand.xdl")
+
 
+
-- RCMD_GO_HOME, command value recognized by switch statement within control_AllegroHand.cpp's command() function.
+
-- RCMD_GO_HOME defined in control_AllegroHandCmd.h
+
 
+
sendCommand("HandController", RCMD_GO_HOME)
+
 
+
...
+
</syntaxhighlight >
+
 
+
Notice that all of the button code to command the Allegro Hand library motion is already available within the original LUA file, yet, if clicked, these buttons do nothing. We must develop the ''command()'' function in ''control_AllegroHand.cpp'' to handle the incoming commands.
+
  
 
<br>
 
<br>
Line 60: Line 40:
 
Since the buttons are already set up in AHAS, it is very easy to set up the function that will handle the commands in the control plug-in. But first, we must define the commands as integers for easy recognition.
 
Since the buttons are already set up in AHAS, it is very easy to set up the function that will handle the commands in the control plug-in. But first, we must define the commands as integers for easy recognition.
  
===control_AllegroHandCmd.h===
+
===myAHControllerCmd.h===
  
In the following header file, the command AH_HOME, used to call the home position for the Allegro Hand, is defined as RCMD_GO_HOME. This represents an integer value predefined in ''rCmdManipulator.h''.  
+
In the following header file, the command BH_HOME, used to call the home position for the Allegro Hand, is defined as RCMD_GO_HOME. This represents an integer value predefined in ''rCmdManipulator.h''.  
  
The command AH_NONE is defined as RCMD_USER, another integer value predefined in ''rCmdDefine.h''. We will use RCMD_USER plus some integer to define all of our further commands from both the BHand library and our user-written algorithms (see AH_ONE).
+
The command BH_NONE is defined as RCMD_USER, another integer value predefined in ''rCmdDefine.h''. We will use RCMD_USER plus some integer to define all of our further commands from both the BHand library and our user-written algorithms (see BH_ONE).
  
 
<syntaxhighlight lang="cpp">
 
<syntaxhighlight lang="cpp">
  
#ifndef __CONTROL_ALLEGROHAND_CMD_H__
+
#ifndef __MY_AH_CONTROLLER_CMD_H__
#define __CONTROL_ALLEGROHAND_CMD_H__
+
#define __MY_AH_CONTROLLER_CMD_H__
 
+
 
#include "rCommand/rCmdManipulator.h"
 
#include "rCommand/rCmdManipulator.h"
 
+
// These command values will be fed into command()
+
// These commands will be fed into command()
 
// and can be used to envoke certain actions
 
// and can be used to envoke certain actions
 
// by the robot. Allegro Application Studio
 
// by the robot. Allegro Application Studio
 
// will use these to interface with the
 
// will use these to interface with the
 
// cotroller plug-in.
 
// cotroller plug-in.
 
+
#define BH_NONE (RCMD_USER + 0)
// RCMD_GO_HOME and RCMD_USER are predefines
+
#define BH_HOME (RCMD_GO_HOME)
// Every user command besides the 'home' command
+
// #define BH_ONE (RCMD_USER + 1)
// will be RCMD_USER plus an integer
+
 
+
#define AH_NONE (RCMD_USER + 0)
+
#define AH_HOME (RCMD_GO_HOME)
+
// #define AH_ONE      (RCMD_USER + 1)
+
 
+
 
#endif
 
#endif
  
 
</syntaxhighlight>
 
</syntaxhighlight>
  
===control_AllegroHand.cpp===
+
===myAHController.cpp===
  
Now that the 'home' command, AH_HOME has been defined, we can write the command handler into our controller. In the function ''command()'', we will create a switch statement with the integer valued command as input. This method will allow us to easily expand out list off executable commands in the future.
+
Now that the 'home' command, BH_HOME has been defined, we can write the command handler into our controller. In the function ''command()'', we will create a switch statement with the integer valued command as input. This method will allow us to easily expand out list off executable commands in the future.
  
 
<syntaxhighlight lang="cpp">
 
<syntaxhighlight lang="cpp">
int control_AllegroHand::command(const short& cmd, const int& arg)
+
int myAHController::command(const short& cmd, const int& arg)
 
{
 
{
 
// Handles user-defined commands according to cmd.
 
// Handles user-defined commands according to cmd.
Line 128: Line 103:
  
 
<syntaxhighlight lang="cpp">
 
<syntaxhighlight lang="cpp">
void control_AllegroHand::_compute(const double& t)
+
void myAHController::_compute(const double& t)
 
{
 
{
 
if (_hand)
 
if (_hand)
Line 146: Line 121:
 
'''''Note: '''Make sure you are opening the shortcut that will run the virtual hand, not the actual hand.''
 
'''''Note: '''Make sure you are opening the shortcut that will run the virtual hand, not the actual hand.''
  
The hand should be fully flat when AHAS starts. Click ''"Home"'' to send the home position command. The should assume the home position. Unfortunately, we now have no way to access our demo mode. We will fix that, but first, lets get all of the other buttons working.
+
The hand should be fully flat when AHAS starts. Click ''"Home"'' to send the home position command. That should assume the home position. Unfortunately, we now have no way to access our demo mode. We will fix that, but first, lets get all of the other buttons working.
  
 
==More Buttons, More Motions==
 
==More Buttons, More Motions==
  
As you can see in the ''ALLEGROHAND1.lua'' code, all of the motion buttons already have a command in the form ''RCMD_USER + #''. We will reference these commands to update our header file, ''control_AllegroHandCmd.h''.
+
As you can see in the ''myAHController.lua'' code, all of the motion buttons already have a command in the form ''RCMD_USER + #''. We will reference these commands to update our header file, ''myAHControllerCmd.h''.
  
  
===ALLEGROHAND1.lua===
+
===myAHController.lua===
 
<syntaxhighlight lang="lua">
 
<syntaxhighlight lang="lua">
 
...
 
...
  
    -- RCMD_GO_HOME and RCMD_USER are predefines
+
  BH_READY = RCMD_USER + 2
    -- Every user command besides the 'home' command
+
  BH_GRASP_3 = RCMD_USER + 3
    -- will be RCMD_USER plus an integer see the
+
  BH_GRASP_4 = RCMD_USER + 4
    -- control_AllegroHandCmd.h header file for
+
  BH_PINCH_IT = RCMD_USER + 5
    -- command definitions
+
  BH_PINCH_MT = RCMD_USER + 6
    AH_READY = RCMD_USER + 2
+
  BH_ENVELOP = RCMD_USER + 8
 +
  BH_SHOWOFF = RCMD_USER + 50
 +
  BH_GRAVITY_COMP = RCMD_USER + 9
 +
  BH_TEST = RCMD_USER + 100
 +
 +
  CAN_CMD_RESET_ENC = 4
  
...
+
  function OnGUI()
  
    if (GUIButton(5, 35, 120, 25, "Home")) then
+
...
      sendCommand("HandController", RCMD_GO_HOME)
+
    end
+
    if (GUIButton(130, 35, 120, 25, "Ready")) then
+
      sendCommand("HandController", AH_READY)
+
    end
+
    if (GUIButton(5, 65, 120, 25, "GRASP 3")) then
+
      sendCommand("HandController", RCMD_USER + 3)
+
    end
+
    if (GUIButton(130, 65, 120, 25, "GRASP 4")) then
+
      sendCommand("HandController", RCMD_USER + 4)
+
    end
+
    if (GUIButton(5, 95, 120, 25, "PINCHING(I)")) then
+
      sendCommand("HandController", RCMD_USER + 5)
+
    end
+
    if (GUIButton(130, 95, 120, 25, "PINCHING(M)")) then
+
      sendCommand("HandController", RCMD_USER + 6)
+
    end
+
    if (GUIButton(5, 125, 245, 25, "Envelop")) then
+
      sendCommand("HandController", RCMD_USER + 8)
+
    end
+
 
+
...
+
 
</syntaxhighlight>
 
</syntaxhighlight>
  
  
Please note that the command, AH_READY, is defined above as RCMD_USER + 2. This is the same as feeding RCMD_USER + 2 directly into ''sendCommand()''. Feel free to define these wherever you want.
+
Please note that the command, BH_READY, is defined above as RCMD_USER + 2.<br>
 +
This is the same as feeding RCMD_USER + 2 directly into ''controller:command()''. Feel free to define these wherever you want.
  
According to the definitions in the AHAS LUA file, we will add these six (6) new commands to the ''control_AllegroHandCmd.h'' header file definitions. We will then add the code to be executed with each button to ''control_AllegroHand.cpp'''s ''command()'' function.
+
According to the definitions in the AHAS LUA file, we will add these six(6) new commands to the ''myAHControllerCmd.h'' header file definitions. We will then add the code to be executed with each button to ''myAHController.cpp'''s ''command()'' function.
  
 
+
===myAHControllerCmd.h:===
===control_AllegroHandCmd.h:===
+
 
<syntaxhighlight lang="cpp">
 
<syntaxhighlight lang="cpp">
 
...
 
...
  
#define AH_NONE (RCMD_USER + 0)
+
#define BH_NONE (RCMD_USER + 0)
#define AH_HOME (RCMD_GO_HOME)
+
#define BH_HOME (RCMD_GO_HOME)
#define AH_READY (RCMD_USER + 2)
+
#define BH_READY (RCMD_USER + 2)
#define AH_GRASP_3 (RCMD_USER + 3)
+
#define BH_GRASP_3 (RCMD_USER + 3)
#define AH_GRASP_4 (RCMD_USER + 4)
+
#define BH_GRASP_4 (RCMD_USER + 4)
#define AH_PINCH_IT (RCMD_USER + 5)
+
#define BH_PINCH_IT (RCMD_USER + 5)
#define AH_PINCH_MT (RCMD_USER + 6)
+
#define BH_PINCH_MT (RCMD_USER + 6)
#define AH_ENVELOP (RCMD_USER + 8)
+
#define BH_ENVELOP (RCMD_USER + 8)
  
//#define AH_MORE (RCMD_USER + 99)
+
//#define BH_MORE (RCMD_USER + 99)
  
 
...
 
...
 
</syntaxhighlight>
 
</syntaxhighlight>
  
 +
===myAHConroller.cpp===
 +
<syntaxhighlight lang="cpp">
  
===control_AllegroHand.cpp===
+
myAHController::myAHController(rDC rdc)
<syntaxhighlight lang="cpp">
+
:rControlAlgorithmEx(rdc)
int control_AllegroHand::command(const short& cmd, const int& arg)
+
, _jdof(0)                              // everything is NULL to start
 +
, _hand(NULL)
 +
, _is_left_hand(false)
 +
, _demo_mode(false)   // will be used when
 +
, _demo_start_time(0)           // we make our own motion
 +
{
 +
}
 +
 +
...
 +
int myAHController::command(const short& cmd, const int& arg)
 
{
 
{
 
// Handles user-defined commands according to cmd.
 
// Handles user-defined commands according to cmd.
Line 228: Line 197:
 
switch (cmd)
 
switch (cmd)
 
{
 
{
case AH_HOME:    // The position ensures that all joints are oriented properly for executing a grasp.
+
case BH_HOME:    // The position ensures that all joints are oriented properly for executing a grasp.
 
{
 
{
 
_demo_mode = 0;
 
_demo_mode = 0;
Line 235: Line 204:
 
}
 
}
 
break;
 
break;
case AH_READY:  // Click the buttons entitled Ready to prepare for each type of grasping motion.
+
case BH_READY:  // Click the buttons entitled Ready to prepare for each type of grasping motion.
 
{
 
{
 
_demo_mode = 0;
 
_demo_mode = 0;
Line 242: Line 211:
 
}
 
}
 
break;
 
break;
case AH_GRASP_3:  // This grasping algorithm is a torque-controlled, three-fingered grip.  
+
case BH_GRASP_3:  // This grasping algorithm is a torque-controlled, three-fingered grip.  
 
{
 
{
 
_demo_mode = 0;
 
_demo_mode = 0;
Line 249: Line 218:
 
}
 
}
 
break;
 
break;
case AH_GRASP_4:  // This grasping algorithm is a torque-controlled, four-fingered grip.
+
case BH_GRASP_4:  // This grasping algorithm is a torque-controlled, four-fingered grip.  
 
{
 
{
 
_demo_mode = 0;
 
_demo_mode = 0;
Line 256: Line 225:
 
}
 
}
 
break;
 
break;
case AH_PINCH_IT: // This grasping algorithm is a torque-controlled, two-fingered pinch.
+
case BH_PINCH_IT: // This grasping algorithm is a torque-controlled, two-fingered pinch.
 
{
 
{
 
_demo_mode = 0;
 
_demo_mode = 0;
Line 263: Line 232:
 
}
 
}
 
break;
 
break;
case AH_PINCH_MT: // This grasping algorithm is a torque-controlled, two-fingered pinch.
+
case BH_PINCH_MT: // This grasping algorithm is a torque-controlled, two-fingered pinch.
 
{
 
{
 
_demo_mode = 0;
 
_demo_mode = 0;
Line 270: Line 239:
 
}
 
}
 
break;
 
break;
case AH_ENVELOP: // This grasping algorithm can be used to fully envelop an object within the hand's four fingers.
+
case BH_ENVELOP: // This grasping algorithm is a torque-controlled, two-fingered pinch.
 
{
 
{
 
_demo_mode = 0;
 
_demo_mode = 0;
Line 289: Line 258:
  
 
Now compile, reload AHAS and play with your buttons!!
 
Now compile, reload AHAS and play with your buttons!!
 
  
 
==A Button Just For Us==
 
==A Button Just For Us==
  
In your AHAS ''bin/controls'' directory, locate the file ''ALLEGROHAND1.lua''. Copy this file to your ''Desktop''.
+
In your AHAS ''bin/controls'' directory, locate the file ''myAHController.lua''. Copy this file to your ''Desktop''.
  
 
'''''Note: '''Once again, we must copy this file to the desktop for editing then move it back to the controls folder when we are finished to avoid opening with admin privileges.''
 
'''''Note: '''Once again, we must copy this file to the desktop for editing then move it back to the controls folder when we are finished to avoid opening with admin privileges.''
  
From the ''Desktop'', let's open up ''ALLEGROHAND1.lua'' and take a look.
+
From the ''Desktop'', let's open up ''myAHController.lua'' and take a look.
  
  
 
To make our own button we need only to copy the code from a stock button and change three things: the location, the name and the command sent to the controller.
 
To make our own button we need only to copy the code from a stock button and change three things: the location, the name and the command sent to the controller.
  
First, select any of the stock button and copy the ''if statement'' and anything inside. Paste it below the last stock button (''Envelop'').
+
First, select any of the stock button and copy the ''if statement'' and anything inside. Paste it below the last stock button (''ENVELOP'').
  
 
Now we will change a few things to make this button our own.
 
Now we will change a few things to make this button our own.
  
 +
The following button code produces a button called ''"Demo Mode 1"'' just below the ''"ENVELOP"'' button. The command sent to the controller is ''RCMD_USER + 50''. Remember, we will have to define this in the controller header for use in the controller command function.
  
'''Name:''' The name of the button is the text displayed on the button within the AHAS GUI. I will name the new button ''"Demo Mode 1"'' but feel free to name this button whatever you'd like.<br>
 
'''Position and Size:''' Defines the window position in pixels relative to the top left and the button size in pixels.<br>
 
:'''x-pos:''' button's distance from the left side of the AHAS window (pixels)<br>
 
:'''y-pos:''' button's distance from the top of the AHAS window (pixels)<br>
 
:'''width:''' button width (pixels)<br>
 
:'''height:''' button height (pixels)<br>
 
'''Command:''' Command value to be send to the controller.
 
  
 +
===myAHController.lua===
 +
<syntaxhighlight lang="lua">
 +
...
 +
  function Update()
 +
  -- Can be used do draw graphic elements in AHAS
 +
  end
  
The following button code produces a button called ''"Demo Mode 1"'' just below the ''"Envelop"'' button. The command sent to the controller is ''RCMD_USER + 50''. Remember, we will have to define this in the controller header for use in the controller command function.
+
  BH_READY = RCMD_USER + 2
 +
  BH_GRASP_3 = RCMD_USER + 3
 +
  BH_GRASP_4 = RCMD_USER + 4
 +
  BH_PINCH_IT = RCMD_USER + 5
 +
  BH_PINCH_MT = RCMD_USER + 6
 +
  BH_ENVELOP = RCMD_USER + 8
 +
--  BH_SHOWOFF = RCMD_USER + 50
 +
  BH_DEMO1 = RCMD_USER + 50
 +
  BH_GRAVITY_COMP = RCMD_USER + 9
 +
  BH_TEST = RCMD_USER + 100
  
 +
...
 +
 +
  function allMotionsFalse()
 +
    isHome = false
 +
    isReady = false
 +
    isGrasp3 = false
 +
    isGrasp4 = false
 +
    isPinchIT = false
 +
    isPinchMT = false
 +
    isEnvelop = false
 +
--    isShowOff = false
 +
isOn = false
 +
    isGravity = false
 +
  end
  
===ALLEGROHAND1.lua===
 
<syntaxhighlight lang="lua">
 
 
...
 
...
  
    if (GUIButton(5, 125, 245, 25, "Envelop")) then
+
row = 4
      sendCommand("HandController", RCMD_USER + 8)
+
col = 2
    end
+
button_image = Image.new("buttonImages/off.png")
+
if (isOn) then
    if (GUIButton(5, 155, 245, 25, "Demo Mode 1")) then
+
button_image = Image.new("buttonImages/on.png")
      sendCommand("HandController", RCMD_USER + 50)
+
end
    end
+
if (GUI.Button(Rect.new(button(row,col)[1], button(row,col)[2], button(row,col)[3], button(row,col)[4]),  button_image)) then
 +
controller:command(BH_DEMO1)
 +
print("Demo1 button pressed")
 +
allMotionsFalse()
 +
if (isConnected) then isOn = true end
 +
end
  
 
...
 
...
 
</syntaxhighlight>
 
</syntaxhighlight>
  
===controlAllegroHandCmd.h===
+
===myAHControllerCmd.h===
 
<syntaxhighlight lang="cpp">
 
<syntaxhighlight lang="cpp">
 
...
 
...
  
#define AH_ENVELOP (RCMD_USER + 8)
+
#define BH_ENVELOP (RCMD_USER + 8)
 
+
#define BH_DEMO1 (RCMD_USER + 50)
#define AH_DEMO1 (RCMD_USER + 50)
+
  
 
#endif
 
#endif
 
</syntaxhighlight>
 
</syntaxhighlight>
  
 
+
===myAHController.cpp===
===controlAllegroHand.cpp===
+
 
<syntaxhighlight lang="cpp">
 
<syntaxhighlight lang="cpp">
int control_AllegroHand::command(const short& cmd, const int& arg)
+
int myAHController::command(const short& cmd, const int& arg)
 
...
 
...
  
case AH_ENVELOP:
+
case BH_ENVELOP:
 
{
 
{
 
_demo_mode = 0;
 
_demo_mode = 0;
Line 358: Line 350:
 
}
 
}
 
break;
 
break;
case AH_DEMO1:
+
case BH_DEMO1:
 
{
 
{
 
_demo_mode = 1;
 
_demo_mode = 1;
Line 375: Line 367:
  
 
With what you learned in this tutorial, you can add as many buttons and respective control algorithms as you want. Please feel free to share your results with us!
 
With what you learned in this tutorial, you can add as many buttons and respective control algorithms as you want. Please feel free to share your results with us!
 
+
<br>
 
+
Please move on to the next tutorial.<br><br>
==Actual Allegro Hand==
+
[[5. Joint PD Control Without Hand Library]]<br>
 
+
[[Category:Programming Guides]]
The controller's ''command()'' function calls the BHand library functions and the command values used are based on the original LUA file. This means that the BHand library function like Grasp and Pinch should automatically work with the buttons in AHAS for the actual hand. These buttons send the same commands and will therefore execute the same motions without any changes to the LUA file.
+
 
+
If you would like to demonstrate your demo mode controller on the actual hand, you need only add your new button to ''ALLEGROHAND1_ACTUAL.lua''.
+
 
+
 
+
'''''Note: '''The first argument of sendCommand is "RTController", not "HandController" as in the virtual hand LUA file.''
+
 
+
 
+
===ALLEGROHAND1_ACTUAL.lua===
+
<syntaxhighlight lang="lua">
+
...
+
 
+
    if (GUIButton(5, 125, 245, 25, "Envelop")) then
+
      sendCommand("RTController", RCMD_USER + 8)
+
      print("Envelop")
+
    end
+
+
    if (GUIButton(5, 155, 245, 25, "Demo Mode 1")) then
+
      sendCommand("RTController", RCMD_USER + 50)
+
      print("Show Off")
+
    end
+
 
+
...
+
</syntaxhighlight>
+

Latest revision as of 15:53, 30 April 2015

In the last few tutorials, we created a control algorithm project and verified our setup by performing a simple user-defined motion on both the virtual and actual Allegro Hands. We did all this ignoring the features of Allegro Hand Application Studio (AHAS) as a robust GUI interface for controlling the hand and visualizing certain data.

In this tutorial, we will take advantage of buttons in AHAS and use them to invoke our own algorithms and those supplied along with AHAS in the BHand Library.

Contents

[edit] AHAS Buttons

Let's open up myAHController.lua and take a look.
This is the file that we made in Tutorial 1 which links to out new controller.
The function OnGUI() is the area of interest. This function contains the buttons we seen on the AHAS GUI.
To create a button and link its press to a command is as simple as follows:

...
	-- ROW 1
	row = 1
	col = 1
	button_image = Image.new("buttonImages/home_off.png")
	if (isHome) then
		button_image = Image.new("buttonImages/home_on.png")
	end
    if (GUI.Button(Rect.new(button(row,col)[1], button(row,col)[2], button(row,col)[3], button(row,col)[4]),  button_image)) then	
		controller:command(RCMD_GO_HOME, VK_H)
		print("Home button pressed")
		allMotionsFalse()
		if (isConnected) then isHome = true end
	end
...

This button, when pressed, will cause the hand to assume its Home position. As mentioned in the comments, the arguments of GUI.Button() specify the button's Rect and the image displayed on the button.

The function controller:command() is used to forward a command to the hand controller, the DLL plug-in, myAHController.dll.

Notice that all of the button code to command the Allegro Hand library motion is already available within the original LUA file, yet, if clicked, these buttons do nothing. We must develop the command() function in myAHController.cpp to handle the incoming commands.


[edit] Plug-in Command Handling

Since the buttons are already set up in AHAS, it is very easy to set up the function that will handle the commands in the control plug-in. But first, we must define the commands as integers for easy recognition.

[edit] myAHControllerCmd.h

In the following header file, the command BH_HOME, used to call the home position for the Allegro Hand, is defined as RCMD_GO_HOME. This represents an integer value predefined in rCmdManipulator.h.

The command BH_NONE is defined as RCMD_USER, another integer value predefined in rCmdDefine.h. We will use RCMD_USER plus some integer to define all of our further commands from both the BHand library and our user-written algorithms (see BH_ONE).

#ifndef __MY_AH_CONTROLLER_CMD_H__
#define __MY_AH_CONTROLLER_CMD_H__
 
#include "rCommand/rCmdManipulator.h"
 
// These commands will be fed into command()
// and can be used to envoke certain actions
// by the robot. Allegro Application Studio
// will use these to interface with the
// cotroller plug-in.
#define BH_NONE		(RCMD_USER + 0)
#define BH_HOME		(RCMD_GO_HOME)
// #define BH_ONE		(RCMD_USER + 1)
 
#endif

[edit] myAHController.cpp

Now that the 'home' command, BH_HOME has been defined, we can write the command handler into our controller. In the function command(), we will create a switch statement with the integer valued command as input. This method will allow us to easily expand out list off executable commands in the future.

int myAHController::command(const short& cmd, const int& arg)
{
	// Handles user-defined commands according to cmd.
	// Further information can be retrieved from the second argument.
 
	// The variable cmd will be received from Allegro Application Studio
	// and will be used to envoke hand actions
 
	switch (cmd)
	{
	case BH_HOME:
		{
			_demo_mode = 0;
 
			if (_hand)
				_hand->SetMotionType(eMotionType_HOME);
		}
		break;
 
	default:
		break;
	}
 
	return 0;
}

As you can see, when it is time to the home position, we must turn off demo mode, which, as you remember, is the sinusoidal joint position controller we implementer in the last two tutorials. But setting _demo_mode to zero will not do the trick. We must revisit the demo mode algorithm and edit the if statement around it.

Currently, the if statement controlling the execution of the demo mode algorithm is evaluated as always true. Replace the true with the variable _demo_mode so that we can once again control it. This will also prevent the algorithm from running immediately when AHAS starts up.

void myAHController::_compute(const double& t)
{
	if (_hand)
	{
		_hand->SetJointPosition(_q.array);		    // joint positions set via torque PID
 
                // DEMO MODE NOW ONLY RUNS IF WE SET _demo_mode TO 1
		if (_demo_mode) 
		{
			_hand->SetMotionType(eMotionType_JOINT_PD); // PID gains and motion control set
 
...

Compile then open up AHAS from the shortcut on your desktop.

Note: Make sure you are opening the shortcut that will run the virtual hand, not the actual hand.

The hand should be fully flat when AHAS starts. Click "Home" to send the home position command. That should assume the home position. Unfortunately, we now have no way to access our demo mode. We will fix that, but first, lets get all of the other buttons working.

[edit] More Buttons, More Motions

As you can see in the myAHController.lua code, all of the motion buttons already have a command in the form RCMD_USER + #. We will reference these commands to update our header file, myAHControllerCmd.h.


[edit] myAHController.lua

...
 
  BH_READY 			= RCMD_USER + 2
  BH_GRASP_3 		= RCMD_USER + 3
  BH_GRASP_4 		= RCMD_USER + 4
  BH_PINCH_IT 		= RCMD_USER + 5
  BH_PINCH_MT 		= RCMD_USER + 6
  BH_ENVELOP 		= RCMD_USER + 8
  BH_SHOWOFF 		= RCMD_USER + 50
  BH_GRAVITY_COMP 	= RCMD_USER + 9
  BH_TEST 			= RCMD_USER + 100
 
  CAN_CMD_RESET_ENC = 4
 
  function OnGUI()
 
...


Please note that the command, BH_READY, is defined above as RCMD_USER + 2.
This is the same as feeding RCMD_USER + 2 directly into controller:command(). Feel free to define these wherever you want.

According to the definitions in the AHAS LUA file, we will add these six(6) new commands to the myAHControllerCmd.h header file definitions. We will then add the code to be executed with each button to myAHController.cpp's command() function.

[edit] myAHControllerCmd.h:

...
 
#define BH_NONE		(RCMD_USER + 0)
#define BH_HOME		(RCMD_GO_HOME)
#define BH_READY		(RCMD_USER + 2)
#define BH_GRASP_3	(RCMD_USER + 3)
#define BH_GRASP_4	(RCMD_USER + 4)
#define BH_PINCH_IT	(RCMD_USER + 5)
#define BH_PINCH_MT	(RCMD_USER + 6)
#define BH_ENVELOP	(RCMD_USER + 8)
 
//#define BH_MORE	(RCMD_USER + 99)
 
...

[edit] myAHConroller.cpp

myAHController::myAHController(rDC rdc) 
:rControlAlgorithmEx(rdc)
, _jdof(0)                              // everything is NULL to start
, _hand(NULL)
, _is_left_hand(false)
, _demo_mode(false)		  // will be used when
, _demo_start_time(0)	          // we make our own motion
{
}
 
...
int myAHController::command(const short& cmd, const int& arg)
{
	// Handles user-defined commands according to cmd.
	// Further information can be retrieved from the second argument.
 
	// The variable cmd will be received from Allegro Application Studio
	// and will be used to envoke hand actions
 
	switch (cmd)
	{
	case BH_HOME:    // The position ensures that all joints are oriented properly for executing a grasp.
		{
			_demo_mode = 0;
			if (_hand)
				_hand->SetMotionType(eMotionType_HOME);
		}
		break;
	case BH_READY:   // Click the buttons entitled Ready to prepare for each type of grasping motion.
		{
			_demo_mode = 0;
			if (_hand)
				_hand->SetMotionType(eMotionType_READY);
		}
		break;
	case BH_GRASP_3:  // This grasping algorithm is a torque-controlled, three-fingered grip. 
		{
			_demo_mode = 0;
			if (_hand)
				_hand->SetMotionType(eMotionType_GRASP_3);
		}
		break;
	case BH_GRASP_4:  // This grasping algorithm is a torque-controlled, four-fingered grip. 
		{
			_demo_mode = 0;
			if (_hand)
				_hand->SetMotionType(eMotionType_GRASP_4);
		}
		break;
	case BH_PINCH_IT: // This grasping algorithm is a torque-controlled, two-fingered pinch.
		{
			_demo_mode = 0;
			if (_hand)
				_hand->SetMotionType(eMotionType_PINCH_IT);
		}
		break;
	case BH_PINCH_MT: // This grasping algorithm is a torque-controlled, two-fingered pinch.
		{
			_demo_mode = 0;
			if (_hand)
				_hand->SetMotionType(eMotionType_PINCH_MT);
		}
		break;
	case BH_ENVELOP: // This grasping algorithm is a torque-controlled, two-fingered pinch.
		{
			_demo_mode = 0;
			if (_hand)
				_hand->SetMotionType(eMotionType_ENVELOP);
		}
		break;
	default:
		break;
	}
 
	return 0;
}


As you can see in every case, we are ensuring that _demo_mode is set to zero. The value of the _demo_mode flag is checked every iteration. If _demo_mode == 1, then the demo code we wrote earlier will override the latest command.

Now compile, reload AHAS and play with your buttons!!

[edit] A Button Just For Us

In your AHAS bin/controls directory, locate the file myAHController.lua. Copy this file to your Desktop.

Note: Once again, we must copy this file to the desktop for editing then move it back to the controls folder when we are finished to avoid opening with admin privileges.

From the Desktop, let's open up myAHController.lua and take a look.


To make our own button we need only to copy the code from a stock button and change three things: the location, the name and the command sent to the controller.

First, select any of the stock button and copy the if statement and anything inside. Paste it below the last stock button (ENVELOP).

Now we will change a few things to make this button our own.

The following button code produces a button called "Demo Mode 1" just below the "ENVELOP" button. The command sent to the controller is RCMD_USER + 50. Remember, we will have to define this in the controller header for use in the controller command function.


[edit] myAHController.lua

...
  function Update()
  	-- Can be used do draw graphic elements in AHAS
  end
 
  BH_READY 			= RCMD_USER + 2
  BH_GRASP_3 		= RCMD_USER + 3
  BH_GRASP_4 		= RCMD_USER + 4
  BH_PINCH_IT 		= RCMD_USER + 5
  BH_PINCH_MT 		= RCMD_USER + 6
  BH_ENVELOP 		= RCMD_USER + 8
--  BH_SHOWOFF 		= RCMD_USER + 50
  BH_DEMO1			= RCMD_USER + 50
  BH_GRAVITY_COMP 	= RCMD_USER + 9
  BH_TEST 			= RCMD_USER + 100
 
...
 
  function allMotionsFalse()
    isHome = false
    isReady = false
    isGrasp3 = false
    isGrasp4 = false
    isPinchIT = false
    isPinchMT = false
    isEnvelop = false
--    isShowOff = false
	isOn = false
    isGravity = false
  end
 
...
 
	row = 4
	col = 2	
	button_image = Image.new("buttonImages/off.png")
	if (isOn) then
		button_image = Image.new("buttonImages/on.png")
	end  
	if (GUI.Button(Rect.new(button(row,col)[1], button(row,col)[2], button(row,col)[3], button(row,col)[4]),  button_image)) then	
		controller:command(BH_DEMO1)
		print("Demo1 button pressed")
		allMotionsFalse()
		if (isConnected) then isOn = true end			
	end	
 
...

[edit] myAHControllerCmd.h

...
 
#define BH_ENVELOP	(RCMD_USER + 8)
#define BH_DEMO1		(RCMD_USER + 50)
 
#endif

[edit] myAHController.cpp

int myAHController::command(const short& cmd, const int& arg)
...
 
	case BH_ENVELOP:
		{
			_demo_mode = 0;
			if (_hand)
				_hand->SetMotionType(eMotionType_ENVELOP);
		}
		break;
	case BH_DEMO1:
		{
			_demo_mode = 1;
		}
		break;
	default:
		break;
	}
 
	return 0;
}


As simple as that!

With what you learned in this tutorial, you can add as many buttons and respective control algorithms as you want. Please feel free to share your results with us!
Please move on to the next tutorial.

5. Joint PD Control Without Hand Library





Whos here now:   Members 0   Guests 0   Bots & Crawlers 1