Skip to content

Commit 0ac4bc4

Browse files
authored
Merge pull request #88 from BCLab-UNM/hotfix-manual-gripper-control
Manual gripper control fix
2 parents b8a2cf5 + b9e3aa2 commit 0ac4bc4

File tree

3 files changed

+34
-100
lines changed

3 files changed

+34
-100
lines changed

src/abridge/src/abridge.cpp

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,11 @@ const float deltaTime = 0.1; //abridge's update interval
4545
int currentMode = 0;
4646
string publishedName;
4747

48+
// Allowing messages to be sent to the arduino too fast causes a disconnect
49+
// This is the minimum time between messages to the arduino in microseconds.
50+
// Only used with the gripper commands to fix a manual control bug.
51+
unsigned int min_usb_send_delay = 100;
52+
4853
float heartbeat_publish_interval = 2;
4954

5055

@@ -197,6 +202,10 @@ void driveCommandHandler(const geometry_msgs::Twist::ConstPtr& message) {
197202
// radians, write them to a string and send that to the arduino
198203
// for processing.
199204
void fingerAngleHandler(const std_msgs::Float32::ConstPtr& angle) {
205+
206+
// To throttle the message rate so we don't lose connection to the arduino
207+
usleep(min_usb_send_delay);
208+
200209
char cmd[16]={'\0'};
201210

202211
// Avoid dealing with negative exponents which confuse the conversion to string by checking if the angle is small
@@ -211,6 +220,9 @@ void fingerAngleHandler(const std_msgs::Float32::ConstPtr& angle) {
211220
}
212221

213222
void wristAngleHandler(const std_msgs::Float32::ConstPtr& angle) {
223+
// To throttle the message rate so we don't lose connection to the arduino
224+
usleep(min_usb_send_delay);
225+
214226
char cmd[16]={'\0'};
215227

216228
// Avoid dealing with negative exponents which confuse the conversion to string by checking if the angle is small

src/rqt_rover_gui/src/JoystickGripperInterface.cpp

Lines changed: 22 additions & 88 deletions
Original file line numberDiff line numberDiff line change
@@ -17,10 +17,6 @@ JoystickGripperInterface::JoystickGripperInterface(const JoystickGripperInterfac
1717
this->gripperWristAnglePublisher = source.gripperWristAnglePublisher;
1818
this->gripperFingerAnglePublisher = source.gripperFingerAnglePublisher;
1919

20-
// QTimer state
21-
this->joystickGripperWristControlTimer = source.joystickGripperWristControlTimer;
22-
this->joystickGripperFingerControlTimer = source.joystickGripperFingerControlTimer;
23-
2420
// Wrist state
2521
this->wristAngle = source.wristAngle;
2622
this->wristAngleChangeRate = source.wristAngleChangeRate;
@@ -35,10 +31,6 @@ JoystickGripperInterface::JoystickGripperInterface(const JoystickGripperInterfac
3531
this->fingerAngleMin = source.fingerAngleMin;
3632
this->fingerJoystickVector = source.fingerJoystickVector;
3733

38-
// Other
39-
this->commandReapplyRate = source.commandReapplyRate;
40-
this->stickCenterTolerance = source.stickCenterTolerance;
41-
4234
this->nh = source.nh;
4335
}
4436

@@ -49,19 +41,6 @@ JoystickGripperInterface::JoystickGripperInterface(ros::NodeHandle nh, string ro
4941

5042
this->nh = nh;
5143

52-
// Joystick auto repeat for gripper control
53-
joystickGripperWristControlTimer = new QTimer(this);
54-
joystickGripperFingerControlTimer = new QTimer(this);
55-
56-
// Connect the command timers to their handlers
57-
connect(joystickGripperWristControlTimer, SIGNAL(timeout()), this, SLOT(joystickGripperWristControlTimerEventHandler()));
58-
connect(joystickGripperFingerControlTimer, SIGNAL(timeout()), this, SLOT(joystickGripperFingerControlTimerEventHandler()));
59-
60-
connect(this, SIGNAL(sendJoystickGripperWristControlTimerStart(int)), joystickGripperWristControlTimer, SLOT(start(int)));
61-
connect(this, SIGNAL(sendJoystickGripperWristControlTimerStop()), joystickGripperWristControlTimer, SLOT(stop()));
62-
connect(this, SIGNAL(sendJoystickGripperFingerControlTimerStart(int)), joystickGripperFingerControlTimer, SLOT(start(int)));
63-
connect(this, SIGNAL(sendJoystickGripperFingerControlTimerStop()), joystickGripperFingerControlTimer, SLOT(stop()));
64-
6544
// Initialize gripper angles in radians
6645
wristAngle = 0;
6746
fingerAngle = 0;
@@ -83,12 +62,7 @@ JoystickGripperInterface::JoystickGripperInterface(ros::NodeHandle nh, string ro
8362
// Representation of the desired movement speed and direction generated by the joystick
8463
fingerJoystickVector = 0.0;
8564
wristJoystickVector = 0.0;
86-
87-
commandReapplyRate = 100; // in milliseconds
88-
89-
stickCenterTolerance = 0.05; // How close to zero does output from the joystick have to be for
90-
// us to consider the user to have centered the stick
91-
65+
9266
// Setup the gripper angle command publishers
9367
gripperWristAnglePublisher = nh.advertise<std_msgs::Float32>("/"+roverName+"/wristAngle/cmd", 10, this);
9468
gripperFingerAnglePublisher = nh.advertise<std_msgs::Float32>("/"+roverName+"/fingerAngle/cmd", 10, this);
@@ -106,15 +80,20 @@ void JoystickGripperInterface::moveWrist(float vec) {
10680

10781
wristJoystickVector = -vec; // negate to make down the positive angle
10882

109-
// Check whether the stick is near the center deadzone - if so stop issuing movement commands
110-
// if not apply the movement indicated by vec
111-
if (fabs(wristJoystickVector) < stickCenterTolerance) {
112-
emit sendJoystickGripperWristControlTimerStop();
113-
} else {
114-
// The event handler calculates the new angle for the wrist and sends it to the gripper wrist control topic
115-
emit sendJoystickGripperWristControlTimerStart(commandReapplyRate);
116-
}
83+
// Calculate the new wrist angle to request
84+
wristAngle += wristJoystickVector*wristAngleChangeRate;
11785

86+
// Don't exceed the min and max angles
87+
if (wristAngle > wristAngleMax) wristAngle = wristAngleMax;
88+
else if (wristAngle < wristAngleMin) wristAngle = wristAngleMin;
89+
90+
// If the wrist angle is small enough to use negative exponents set to zero
91+
// negative exponents confuse the downstream conversion to string
92+
if (fabs(wristAngle) < 0.001) wristAngle = 0.0f;
93+
94+
std_msgs::Float32 angleMsg;
95+
angleMsg.data = wristAngle;
96+
gripperWristAnglePublisher.publish(angleMsg);
11897

11998
}
12099

@@ -127,45 +106,26 @@ void JoystickGripperInterface::moveFingers(float vec){
127106

128107
fingerJoystickVector = vec;
129108

130-
// Check whether the stick is near the center deadzone - if so stop issuing movement commands
131-
// if not apply the movement indicated by vec
132-
if (fabs(fingerJoystickVector) < stickCenterTolerance) {
133-
emit sendJoystickGripperFingerControlTimerStop();
134-
} else {
135-
// The event handler calculates the new angle for the wrist and sends it to the gripper wrist control topic
136-
emit sendJoystickGripperFingerControlTimerStart(commandReapplyRate);
137-
}
138-
139-
140-
}
141-
142-
// Update and broadcast gripper wrist commands
143-
// Called by event timer so the commands continue to be generated even when the joystick
144-
// is not moving
145-
void JoystickGripperInterface::joystickGripperWristControlTimerEventHandler(){
146-
147109
// Calculate the new wrist angle to request
148-
wristAngle += wristJoystickVector*wristAngleChangeRate;
110+
fingerAngle += fingerJoystickVector*fingerAngleChangeRate;
149111

150112
// Don't exceed the min and max angles
151-
if (wristAngle > wristAngleMax) wristAngle = wristAngleMax;
152-
else if (wristAngle < wristAngleMin) wristAngle = wristAngleMin;
113+
if (fingerAngle > fingerAngleMax) fingerAngle = fingerAngleMax;
114+
else if (fingerAngle < fingerAngleMin) fingerAngle = fingerAngleMin;
153115

154-
// If the wrist angle is small enough to use negative exponents set to zero
116+
// If the finger angle is small enough to use negative exponents set to zero
155117
// negative exponents confuse the downstream conversion to string
156-
if (fabs(wristAngle) < 0.001) wristAngle = 0.0f;
118+
if (fabs(fingerAngle) < 0.001) fingerAngle = 0.0f;
157119

120+
// Publish the finger angle commands
158121
std_msgs::Float32 angleMsg;
159-
angleMsg.data = wristAngle;
160-
gripperWristAnglePublisher.publish(angleMsg);
161-
122+
angleMsg.data = fingerAngle;
123+
gripperFingerAnglePublisher.publish(angleMsg);
162124
}
163125

164126
void JoystickGripperInterface::changeRovers(string roverName)
165127
{
166128
ready = false;
167-
joystickGripperWristControlTimer->stop();
168-
joystickGripperFingerControlTimer->stop();
169129

170130
gripperWristAnglePublisher.shutdown();
171131
gripperFingerAnglePublisher.shutdown();
@@ -187,36 +147,10 @@ void JoystickGripperInterface::changeRovers(string roverName)
187147
ready = true;
188148
}
189149

190-
// Update and broadcast gripper commands for the fingers
191-
// Called by event timer so the commands continue to be generated even when the joystick
192-
// is not moving
193-
void JoystickGripperInterface::joystickGripperFingerControlTimerEventHandler(){
194-
// Calculate the new wrist angle to request
195-
fingerAngle += fingerJoystickVector*fingerAngleChangeRate;
196-
197-
// Don't exceed the min and max angles
198-
if (fingerAngle > fingerAngleMax) fingerAngle = fingerAngleMax;
199-
else if (fingerAngle < fingerAngleMin) fingerAngle = fingerAngleMin;
200-
201-
202-
// If the finger angle is small enough to use negative exponents set to zero
203-
// negative exponents confuse the downstream conversion to string
204-
if (fabs(fingerAngle) < 0.001) fingerAngle = 0.0f;
205-
206-
// Publish the finger angle commands
207-
std_msgs::Float32 angleMsg;
208-
angleMsg.data = fingerAngle;
209-
gripperFingerAnglePublisher.publish(angleMsg);
210-
211-
}
212-
213150
// Clean up the publishers when this object is destroyed
214151
JoystickGripperInterface::~JoystickGripperInterface(){
215152
ready = false;
216153

217-
if(joystickGripperWristControlTimer) delete joystickGripperWristControlTimer;
218-
if(joystickGripperFingerControlTimer) delete joystickGripperFingerControlTimer;
219-
220154
gripperWristAnglePublisher.shutdown();
221155
gripperFingerAnglePublisher.shutdown();
222156
}

src/rqt_rover_gui/src/JoystickGripperInterface.h

Lines changed: 0 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -67,30 +67,18 @@ class JoystickGripperInterface : public QObject // inherits from QObject for QT
6767

6868
private slots:
6969

70-
// These timed events calculate and broadcast the new gripper angles on the gripper control topics
71-
void joystickGripperWristControlTimerEventHandler();
72-
void joystickGripperFingerControlTimerEventHandler();
73-
74-
7570
private:
7671

7772
// Publishers for sending gripper commands to the ROS topics
7873
ros::Publisher gripperWristAnglePublisher;
7974
ros::Publisher gripperFingerAnglePublisher;
8075

81-
QTimer* joystickGripperWristControlTimer; // Timer to trigger the wrist angle command
82-
QTimer* joystickGripperFingerControlTimer; // Timer to trigger the finger angle command
83-
8476
// Joystick gripper controller state
8577
float wristAngle, wristAngleChangeRate, wristAngleMax, wristAngleMin, wristJoystickVector;
8678

8779
// The finger movements are symmetric so we just refer to one finger angle etc
8880
float fingerAngle, fingerAngleChangeRate, fingerAngleMax, fingerAngleMin, fingerJoystickVector;
8981

90-
int commandReapplyRate; // Rate at which to apply the joystick command in Hz
91-
92-
float stickCenterTolerance;
93-
9482
std::string roverName;
9583

9684
ros::NodeHandle nh;

0 commit comments

Comments
 (0)