-
Notifications
You must be signed in to change notification settings - Fork 41
Expand file tree
/
Copy pathRobotDriver.h
More file actions
228 lines (192 loc) · 7.18 KB
/
Copy pathRobotDriver.h
File metadata and controls
228 lines (192 loc) · 7.18 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
//
// Driver.h
// example-simple
//
// Created by Dan Moore on 12/22/20.
//
#pragma once
#include "ofMain.h"
#include "ofxTiming.h"
#include "Pose.h"
#include "Synchronized.h"
namespace ofxRobotArm
{
class RobotDriver : public ofThread
{
public:
RobotDriver(){
};
~RobotDriver(){
};
virtual void setAllowReconnect(bool bDoReconnect) = 0;
virtual void setup() = 0;
virtual void setup(string ipAddress, int port, double minPayload = 0.0, double maxPayload = 1.0) = 0;
virtual void setup(string ipAddress, double minPayload = 0.0, double maxPayload = 1.0) = 0;
virtual void setup(int port, double minPayload = 0.0, double maxPayload = 1.0) = 0;
virtual void start() = 0;
virtual bool isConnected() = 0;
virtual void disconnect() = 0;
virtual void stopThread() = 0;
virtual void toggleTeachMode() = 0;
virtual void setTeachMode(bool enabled) = 0;
virtual void threadedFunction() = 0;
virtual vector<double> getInitPose() = 0;
vector<double> getAchievablePosition(vector<double> position)
{
float maxAccelDeg = 500.0;
float maxSpeedPct = 1.0;
if (!bMove && deccelCount > 0)
{
maxSpeedPct = ofMap(deccelCount, 1, numDeccelSteps - 1, 0.0, 1.0, true);
if (maxSpeedPct < 0.9)
{
acceleratePct = 0;
}
//cout << " deccelCount " << deccelCount << " maxSpeedPct " << maxSpeedPct << endl;
}
if (bMove)
{
acceleratePct += 0.02;
if (acceleratePct > 1.0)
{
acceleratePct = 1.0;
}
}
maxSpeedPct *= acceleratePct;
//this seeems to do much better with a hardcoded timedelta
float timeDiff = 1.0 / 120.0; //timeNow-lastTimeSentMove;
if (currentPoseRadian.size() && position.size())
{
vector<double> lastSpeed = calculatedSpeed;
if (calculatedSpeed.size() != position.size())
{
calculatedSpeed.assign(position.size(), 0);
lastSpeed = calculatedSpeed;
}
for (unsigned int d = 0; d < position.size(); d++)
{
calculatedSpeed[d] = (position[d] - currentPoseRadian[d]) / timeDiff;
}
vector<double> acceleration;
acceleration.assign(calculatedSpeed.size(), 0);
for (unsigned int d = 0; d < acceleration.size(); d++)
{
acceleration[d] = (calculatedSpeed[d] - lastSpeed[d]) / timeDiff;
float accelDegPerSec = ofRadToDeg(acceleration[d]);
//this is the max accel reccomended.
//if we are over it we limit the new position to being the old position plus the current speed, plus the max acceleration
//this seems to actually work - fuck yes!
if (fabs(accelDegPerSec) > maxAccelDeg)
{
//cout << d << " currentRobotPositionRadians is " << ofRadToDeg( currentRobotPositionRadians[d] ) << " request is " << ofRadToDeg(position[d]) << " speed is " << ofRadToDeg( calculatedSpeed[d] ) << " prev Speed is " << ofRadToDeg(lastSpeed[d]) << " accel is " << accelDegPerSec << endl;
float newAccel = ofDegToRad(maxAccelDeg) * (float)ofSign(accelDegPerSec);
float speedDiff = newAccel * timeDiff;
float targetSpeed = lastSpeed[d] + speedDiff;
position[d] = currentPoseRadian[d] + (targetSpeed * timeDiff * maxSpeedPct);
//cout << "---- hit limit: accel is " << ofRadToDeg(newAccel) << " targetSpeed is now " << ofRadToDeg(targetSpeed) << " pos is now " << position[d] << endl;
}
else if (maxSpeedPct < 1.0)
{
position[d] = currentPoseRadian[d] + (currentSpeed[d] * timeDiff * maxSpeedPct);
}
}
}
return position;
}
float getThreadFPS(){
float fps = 0;
lock();
fps = timer.getFrameRate();
unlock();
return fps;
}
bool isDataReady(){
if(bDataReady){
bDataReady = false;
return true;
}else{
return false;
}
}
vector<double> getToolPointRaw(){
vector<double> ret;
lock();
toolPoseRaw.swapFront();
ret = toolPoseRaw.getFront();
unlock();
return ret;
}
vector<double> getCurrentPose(){
vector<double> ret;
lock();
poseRaw.swapFront();
ret = poseRaw.getFront();
unlock();
return ret;
}
ofVec4f getCalculatedTCPOrientation(){
ofVec4f ret;
lock();
ret = ofVec4f(dtoolPoint.orientation.x(), dtoolPoint.orientation.y(), dtoolPoint.orientation.z(), dtoolPoint.orientation.w());
unlock();
return ret;
}
ofxRobotArm::Pose getToolPose(){
ofxRobotArm::Pose ret;
lock();
ret = tool;
unlock();
return ret;
}
void setSpeed(vector<double> speeds, double accel){
lock();
currentSpeed = speeds;
acceleration = accel;
bMove = true;
bMoveWithPos = false;
unlock();
}
void setPose(vector<double> pose){
lock();
currentPose = pose;
bMove = true;
bMoveWithPos = true;
deccelCount = numDeccelSteps+4;
bStop = false;
unlock();
}
// Robot Arm
bool bTeachModeEnabled;
bool bDataReady;
bool bStarted;
bool bTryReconnect = true;
vector<double> currentSpeed;
vector<double> calculatedSpeed;
vector<double> currentPoseRadian;
vector<double> currentPose;
vector<double> targetPose;
vector<double> initPose;
double acceleration;
RateTimer timer;
float epslion = 0.00000000000000001;
float lastTimeSentMove = -1;
float timeNow = 0;
deque<vector<double>> poseBuffers;
deque<vector<double>> speedBuffers;
bool bMove;
Synchronized<vector<double>> poseProcessed;
Synchronized<vector<double>> poseRaw;
Synchronized<vector<double>> toolPoseRaw;
ofxRobotArm::Pose tool;
ofxRobotArm::Pose dtoolPoint;
vector<ofxRobotArm::Pose> pose;
bool bTriedOnce = false;
bool bMoveWithPos = false;
bool bMoveWithSpeed = false;
bool bStop = true;
float acceleratePct = 0.0;
int deccelCount = 0;
int numDeccelSteps = 60;
int numJoints = 6;
};
}