00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037
00038
00039
00040
00041
00042
00043
00044
00045
00046
00047
00048
00049
00050
00051
00052
00053
00054
00056
00057 #ifndef _HEK_TELEOP_H
00058 #define _HEK_TELEOP_H
00059
00060
00061
00062
00063 #include <sys/types.h>
00064 #include <stdlib.h>
00065 #include <unistd.h>
00066 #include <stdio.h>
00067 #include <string.h>
00068 #include <string>
00069 #include <map>
00070
00071
00072
00073
00074 #include <boost/bind.hpp>
00075 #include "boost/assign.hpp"
00076
00077
00078
00079
00080 #include "ros/ros.h"
00081 #include "actionlib/server/simple_action_server.h"
00082
00083
00084
00085
00086 #include "trajectory_msgs/JointTrajectory.h"
00087 #include "sensor_msgs/JointState.h"
00088 #include "industrial_msgs/RobotStatus.h"
00089 #include "hekateros_control/HekJointStateExtended.h"
00090 #include "hekateros_control/HekRobotStatusExtended.h"
00091
00092
00093
00094
00095 #include "hekateros_control/Calibrate.h"
00096 #include "hekateros_control/ClearAlarms.h"
00097 #include "hekateros_control/CloseGripper.h"
00098 #include "hekateros_control/EStop.h"
00099 #include "hekateros_control/Freeze.h"
00100 #include "hekateros_control/GetProductInfo.h"
00101 #include "hekateros_control/GotoBalancedPos.h"
00102 #include "hekateros_control/GotoParkedPos.h"
00103 #include "hekateros_control/GotoZeroPt.h"
00104 #include "hekateros_control/IsAlarmed.h"
00105 #include "hekateros_control/IsCalibrated.h"
00106 #include "hekateros_control/IsDescLoaded.h"
00107 #include "hekateros_control/OpenGripper.h"
00108 #include "hekateros_control/Release.h"
00109 #include "hekateros_control/ResetEStop.h"
00110 #include "hekateros_control/SetRobotMode.h"
00111 #include "hekateros_control/Stop.h"
00112
00113
00114
00115
00116
00117
00118
00119
00120 #include "hid/ConnStatus.h"
00121 #include "hid/Controller360State.h"
00122 #include "hid/LEDPattern.h"
00123 #include "hid/RumbleCmd.h"
00124
00125
00126
00127
00128 #include "hid/SetLED.h"
00129 #include "hid/SetRumble.h"
00130
00131
00132
00133
00134 #include "rnr/rnrconfig.h"
00135 #include "rnr/log.h"
00136 #include "rnr/hid/HIDXbox360.h"
00137
00138
00139
00140
00141 #include "Hekateros/hekateros.h"
00142 #include "Hekateros/hekXmlCfg.h"
00143 #include "Hekateros/hekRobot.h"
00144 #include "Hekateros/hekUtils.h"
00145
00146
00147 namespace hekateros_control
00148 {
00152 class HekTeleop
00153 {
00154 public:
00156 typedef std::map<std::string, ros::ServiceServer> MapServices;
00157
00159 typedef std::map<std::string, ros::ServiceClient> MapClientServices;
00160
00162 typedef std::map<std::string, ros::Publisher> MapPublishers;
00163
00165 typedef std::map<std::string, ros::Subscriber> MapSubscriptions;
00166
00168 typedef std::map<std::string, double> MapDouble;
00169
00171 typedef std::map<std::string, bool> MapBool;
00172
00174 typedef std::map<std::string, int> MapJointTraj;
00175
00179 enum TeleopState
00180 {
00181 TeleopStateUninit,
00182 TeleopStatePaused,
00183 TeleopStateReady
00184 };
00185
00189 enum TeleopMode
00190 {
00191 TeleopModeFirstPerson,
00192 TeleopModeShoulder,
00193 TeleopModeElbow
00194 };
00195
00199 enum ButtonId
00200 {
00201 ButtonIdGotoBalPos = rnr::Xbox360FeatIdAButton,
00202 ButtonIdEStop = rnr::Xbox360FeatIdBButton,
00203 ButtonIdGotoParkedPos = rnr::Xbox360FeatIdXButton,
00204 ButtonIdGotoZeroPt = rnr::Xbox360FeatIdYButton,
00205
00206 ButtonIdPause = rnr::Xbox360FeatIdBack,
00207 ButtonIdToggleMode = rnr::Xbox360FeatIdCenterX,
00208 ButtonIdStart = rnr::Xbox360FeatIdStart,
00209
00210 ButtonIdPrevJoint = rnr::Xbox360FeatIdPadDown,
00211 ButtonIdNextJoint = rnr::Xbox360FeatIdPadUp,
00212
00213 ButtonIdFineTune1 = rnr::Xbox360FeatIdLeftStickClick,
00214 ButtonIdFineTune2 = rnr::Xbox360FeatIdRightStickClick,
00215
00216 ButtonIdMoveJoints = rnr::Xbox360FeatIdLeftJoyY,
00217 ButtonIdRotBase = rnr::Xbox360FeatIdLeftJoyX,
00218 ButtonIdPitchWrist = rnr::Xbox360FeatIdRightJoyY,
00219
00220 ButtonIdRotWristCw = rnr::Xbox360FeatIdLeftBump,
00221 ButtonIdRotWristCcw = rnr::Xbox360FeatIdRightBump,
00222
00223 ButtonIdOpenGripper = rnr::Xbox360FeatIdRightTrigger,
00224 ButtonIdCloseGripper = rnr::Xbox360FeatIdLeftTrigger
00225 };
00226
00228 typedef std::map<int, int> ButtonState;
00229
00233 enum LEDPat
00234 {
00235 LEDPatOff = XBOX360_LED_PAT_ALL_OFF,
00236 LEDPatOn = XBOX360_LED_PAT_ALL_BLINK,
00237 LEDPatPaused = XBOX360_LED_PAT_4_ON,
00238 LEDPatReady = XBOX360_LED_PAT_ALL_SPIN,
00239 LEDPatShoulder = XBOX360_LED_PAT_3_ON,
00240 LEDPatElbow = XBOX360_LED_PAT_1_ON
00241 };
00242
00246 struct FirstPersonState
00247 {
00248 bool m_bNewGoal;
00249 double m_goalSign;
00250 struct
00251 {
00252 double alpha;
00253 double beta;
00254 double gamma;
00255 } m_goalJoint;
00256 struct
00257 {
00258 double x;
00259 double y;
00260 } m_goalCart;
00261 };
00262
00269 HekTeleop(ros::NodeHandle &nh, double hz);
00270
00274 virtual ~HekTeleop();
00275
00279 virtual void advertiseServices()
00280 {
00281
00282 }
00283
00287 virtual void clientServices();
00288
00294 virtual void advertisePublishers(int nQueueDepth=2);
00295
00301 virtual void subscribeToTopics(int nQueueDepth=2);
00302
00308 virtual void publish()
00309 {
00310
00311 }
00312
00318 virtual void commCheck();
00319
00326 void putRobotInSafeMode(bool bHard);
00327
00333 bool canMove()
00334 {
00335 if((m_eState == TeleopStateReady) &&
00336 (m_msgRobotStatus.is_calibrated.val ==
00337 industrial_msgs::TriState::TRUE) &&
00338 (m_msgRobotStatus.e_stopped.val == industrial_msgs::TriState::FALSE) &&
00339 (m_msgRobotStatus.in_error.val == industrial_msgs::TriState::FALSE))
00340 {
00341 return true;
00342 }
00343 else
00344 {
00345 return false;
00346 }
00347 }
00348
00354 ros::NodeHandle &getNodeHandle()
00355 {
00356 return m_nh;
00357 }
00358
00366 int countsPerSecond(double seconds)
00367 {
00368 return (int)(seconds * m_hz);
00369 }
00370
00371 protected:
00372 ros::NodeHandle &m_nh;
00373 double m_hz;
00374
00375
00376 MapServices m_services;
00377 MapClientServices m_clientServices;
00378 MapPublishers m_publishers;
00379 MapSubscriptions m_subscriptions;
00380
00381
00382 TeleopState m_eState;
00383 TeleopMode m_eMode;
00384 bool m_bHasXboxComm;
00385 int m_nWdXboxCounter;
00386 int m_nWdXboxTimeout;
00387 bool m_bHasRobotComm;
00388 int m_nWdRobotCounter;
00389 int m_nWdRobotTimeout;
00390 bool m_bRcvdRobotStatus;
00391 bool m_bRcvdJointState;
00392 bool m_bHasFullComm;
00393 ButtonState m_buttonState;
00394 int m_rumbleLeft;
00395 int m_rumbleRight;
00396 FirstPersonState m_fpState;
00397 bool m_bPreemptMove;
00398 double m_fMoveTuning;
00399 MapDouble m_mapCurPos;
00400 MapDouble m_mapCurVel;
00401 MapDouble m_mapGoalPos;
00402 MapDouble m_mapGoalVel;
00403 MapBool m_mapTeleop;
00404 MapJointTraj m_mapTraj;
00405
00406
00407 hekateros_control::HekRobotStatusExtended m_msgRobotStatus;
00409 hekateros_control::HekJointStateExtended m_msgJointState;
00411 trajectory_msgs::JointTrajectoryPoint m_msgActiveJointTrajPoint;
00413 trajectory_msgs::JointTrajectoryPoint m_msgJointTrajPoint;
00415 trajectory_msgs::JointTrajectory m_msgJointTraj;
00417 hid::ConnStatus m_msgConnStatus;
00419
00420
00421
00422
00423
00424
00425
00426
00427
00428
00429
00430
00431
00437 void setLED(int pattern);
00438
00445 void setRumble(int motorLeft, int motorRight);
00446
00450 void closeGripper();
00451
00455 void estop();
00456
00460 void freeze();
00461
00465 void gotoBalancedPos();
00466
00470 void gotoParkedPos();
00471
00475 void gotoZeroPt();
00476
00480 void resetEStop();
00481
00487 void setRobotMode(hekateros::HekRobotMode mode);
00488
00494 void stop(const std::vector<std::string> &vecJointNames);
00495
00496
00497
00498
00499
00500
00504 void publishJointCmd();
00505
00509 void publishRumbleCmd(int motorLeft, int motorRight);
00510
00511
00512
00513
00514
00515
00521 void cbRobotStatus(const hekateros_control::HekRobotStatusExtended &msg);
00522
00528 void cbJointState(const hekateros_control::HekJointStateExtended &msg);
00529
00535 void cbXboxConnStatus(const hid::ConnStatus &msg);
00536
00542 void cbXboxBttnState(const hid::Controller360State &msg);
00543
00544
00545
00546
00547
00548
00555 void msgToState(const hid::Controller360State &msg,
00556 ButtonState &buttonState);
00557
00566 bool buttonOffToOn(int id, ButtonState &buttonState)
00567 {
00568 return (m_buttonState[id] == 0) && (buttonState[id] == 1);
00569 }
00570
00579 bool buttonDiff(int id, ButtonState &buttonState)
00580 {
00581 return m_buttonState[id] != buttonState[id];
00582 }
00583
00589 void execAllButtonActions(ButtonState &buttonState);
00590
00596 void buttonStart(ButtonState &buttonState);
00597
00603 void buttonPause(ButtonState &buttonState);
00604
00610 void buttonToggleMode(ButtonState &buttonState);
00611
00617 void buttonPrevJoint(ButtonState &buttonState);
00618
00624 void buttonNextJoint(ButtonState &buttonState);
00625
00631 void buttonFineTune(ButtonState &buttonState);
00632
00638 void buttonEStop(ButtonState &buttonState);
00639
00645 void buttonGotoBalancedPos(ButtonState &buttonState)
00646 {
00647 if( buttonOffToOn(ButtonIdGotoBalPos, buttonState) )
00648 {
00649 gotoBalancedPos();
00650 m_fpState.m_bNewGoal = true;
00651 m_bPreemptMove = true;
00652 }
00653 }
00654
00660 void buttonGotoParkedPos(ButtonState &buttonState)
00661 {
00662 if( buttonOffToOn(ButtonIdGotoParkedPos, buttonState) )
00663 {
00664 gotoParkedPos();
00665 m_fpState.m_bNewGoal = true;
00666 m_bPreemptMove = true;
00667 }
00668 }
00669
00675 void buttonGotoZeroPt(ButtonState &buttonState)
00676 {
00677 if( buttonOffToOn(ButtonIdGotoZeroPt, buttonState) )
00678 {
00679 gotoZeroPt();
00680 m_fpState.m_bNewGoal = true;
00681 m_bPreemptMove = true;
00682 }
00683 }
00684
00690 void buttonCloseGripper(ButtonState &buttonState);
00691
00697 void buttonOpenGripper(ButtonState &buttonState);
00698
00704 void buttonMoveJoints(ButtonState &buttonState);
00705
00711 void buttonMoveFirstPerson(int joy);
00712
00718 void buttonMoveShoulder(int joy);
00719
00725 void buttonMoveElbow(int joy);
00726
00732 void buttonRotateBase(ButtonState &buttonState);
00733
00739 void buttonPitchWrist(ButtonState &buttonState);
00740
00746 void buttonRotateWristCw(ButtonState &buttonState);
00747
00753 void buttonRotateWristCcw(ButtonState &buttonState);
00754
00755
00756
00757
00758
00759
00763 void pause();
00764
00768 void ready();
00769
00773 void driveLEDsFigure8Pattern();
00774
00782 bool hasActiveJoint(const std::string strJointName)
00783 {
00784 return m_mapGoalPos.find(strJointName) != m_mapGoalPos.end()? true: false;
00785 }
00786
00792 bool hasActiveTrajectory()
00793 {
00794 return m_mapGoalPos.size() > 0? true: false;
00795 }
00796
00802 bool hasWorkingTrajectory()
00803 {
00804 return m_msgJointTraj.joint_names.size() > 0? true: false;
00805 }
00806
00807
00808
00809
00810
00811
00812 void stopUnteleopJoints();
00813
00814
00815
00816
00817
00818
00819
00820
00821
00822
00823
00824
00825
00826
00827
00828
00829 ssize_t setJoint(const std::string &strJointName, double pos, double vel);
00830
00845 ssize_t addJointToTrajectoryPoint(const std::string &strJointName);
00846
00850 void clearWorkingTrajectory();
00851
00855 void clearActiveTrajectory();
00856
00860 void resetActiveTeleop();
00861 };
00862
00863 }
00864
00865
00866 #endif // _HEK_TELEOP_H