9 #include <robotcontrol/model/robotmodel.h>
10 #include <robotcontrol/motionmodule.h>
11 #include <head_control/LookAtTarget.h>
12 #include <head_control/HeadControlStatus.h>
13 #include <config_server/parameter.h>
14 #include <rc_utils/math_spline.h>
15 #include <visualization_msgs/Marker.h>
16 #include <plot_msgs/plot_manager.h>
17 #include <sensor_msgs/Joy.h>
18 #include <std_srvs/Empty.h>
26 struct BoundarySegment
29 BoundarySegment() { a = b = d = 0.0; c = 1.0; tStart = 0.0; tEnd = 1.0; useX =
true; }
30 BoundarySegment(
double a,
double b,
double c,
double d = 0.0,
double tStart = 0.0,
double tEnd = 1.0,
bool useX =
true) : a(a), b(b), c(c), d(d), tStart(tStart), tEnd(tEnd), useX(useX) {}
33 bool invalid()
const {
return (a == 0.0 && b == 0.0 && d == 0.0); }
34 static bool isInvalid(
const BoundarySegment& B) {
return B.invalid(); }
37 void shiftBy(
double margin);
40 static std::string serialiseArray(
const std::vector<BoundarySegment>& array);
41 static bool deserialiseArray(
const std::string& str, std::vector<BoundarySegment>& array);
53 static const double minC = 0.01;
66 virtual bool applyLimit(
double& yaw,
double& pitch)
const {
return false; }
72 class SimpleLimiter :
public HeadLimiter
76 static const double minYawCmd = -1.57;
77 static const double maxYawCmd = 1.57;
78 static const double minPitchCmd = -0.5;
79 static const double maxPitchCmd = 0.5;
85 virtual bool applyLimit(
double& yaw,
double& pitch)
const;
91 class BoundaryLimiter :
public HeadLimiter
95 BoundaryLimiter() : m_params(NULL) {}
96 BoundaryLimiter(
const std::vector<BoundarySegment>* params) : m_params(params) {}
99 void setParams(
const std::vector<BoundarySegment>* params) { m_params = params; }
100 const std::vector<BoundarySegment>* getParams()
const {
return m_params; }
103 virtual bool applyLimit(
double& yaw,
double& pitch)
const;
107 const std::vector<BoundarySegment>* m_params;
113 class HeadControl :
public robotcontrol::MotionModule
118 virtual ~HeadControl();
121 virtual bool init(robotcontrol::RobotModel* model);
122 virtual bool isTriggered();
127 bool active()
const {
return m_headControlEnabled() && m_haveTarget && (m_model->state() != m_state_kicking) && (m_model->state() != m_state_standingUp); }
133 void handleLookAtTarget(
const head_control::LookAtTargetConstPtr& msg);
136 ros::NodeHandle m_nh;
139 ros::Subscriber m_sub_lookAtTarget;
140 ros::Publisher m_pub_headControlStatus;
143 void updateHeadControlStatus();
144 head_control::HeadControlStatus m_msgStatus;
148 robotcontrol::RobotModel* m_model;
151 robotcontrol::Joint::Ptr m_headYawJoint;
152 robotcontrol::Joint::Ptr m_headPitchJoint;
155 robotcontrol::RobotModel::State m_state_kicking;
156 robotcontrol::RobotModel::State m_state_standingUp;
159 config_server::Parameter<bool> m_headControlEnabled;
160 config_server::Parameter<bool> m_yawRelax;
161 config_server::Parameter<float> m_yawMaxVel;
162 config_server::Parameter<float> m_yawMaxAcc;
163 config_server::Parameter<float> m_yawDefaultEffort;
164 config_server::Parameter<bool> m_pitchRelax;
165 config_server::Parameter<float> m_pitchMaxVel;
166 config_server::Parameter<float> m_pitchMaxAcc;
167 config_server::Parameter<float> m_pitchDefaultEffort;
173 double m_yawCurEffort;
174 double m_yawEffortTarget;
175 rc_utils::TrapVelSpline m_yawSpline;
176 rc_utils::LinearSpline m_yawEffortSpline;
179 double m_pitchTarget;
182 double m_pitchCurEffort;
183 double m_pitchEffortTarget;
184 rc_utils::TrapVelSpline m_pitchSpline;
185 rc_utils::LinearSpline m_pitchEffortSpline;
188 config_server::Parameter<bool> m_enableJoystick;
189 ros::Subscriber m_sub_joystickData;
190 void handleJoystickData(
const sensor_msgs::JoyConstPtr& msg);
191 bool m_joystickButton9Pressed;
192 bool m_joystickSaysEnabled;
196 BoundaryLimiter m_posLimiter;
199 typedef std::pair<double, double> Point;
202 void initCalibration();
207 std::vector<Point> m_recordedData;
211 void subscribeClickedPoint() { m_sub_clickedPoint = m_nh.subscribe(
"/clicked_point", 1, &HeadControl::handleClickedPoint,
this); }
212 void unsubscribeClickedPoint() { m_sub_clickedPoint.shutdown(); }
213 void handleClickedPoint(
const geometry_msgs::PointStampedConstPtr& msg);
214 void handleCalibParamString();
215 BoundarySegment fitLine(
const Point& p1,
const Point& p2);
216 BoundarySegment fitParabola(
const Point& p1,
const Point& p2,
const Point& p3);
217 ros::Subscriber m_sub_clickedPoint;
218 std::vector<Point> m_calibData;
219 std::vector<BoundarySegment> m_calibParams;
220 std::vector<BoundarySegment> m_curCalibParams;
221 config_server::Parameter<float> m_calibSafetyMargin;
222 config_server::Parameter<std::string> m_calibParamString;
225 ros::ServiceServer m_srv_startRecording;
226 ros::ServiceServer m_srv_stopRecording;
227 ros::ServiceServer m_srv_startCalibration;
228 ros::ServiceServer m_srv_stopCalibration;
229 ros::ServiceServer m_srv_showHeadLimits;
230 bool startRecording(std_srvs::EmptyRequest&, std_srvs::EmptyResponse&) {
return startRecording(); }
231 bool stopRecording(std_srvs::EmptyRequest&, std_srvs::EmptyResponse&) {
return stopRecording(); }
232 bool startCalibration(std_srvs::EmptyRequest&, std_srvs::EmptyResponse&) {
return startCalibration(); }
233 bool stopCalibration(std_srvs::EmptyRequest&, std_srvs::EmptyResponse&) {
return stopCalibration(); }
234 bool showHeadLimits(std_srvs::EmptyRequest&, std_srvs::EmptyResponse&) {
return showHeadLimits(); }
235 bool startRecording();
236 bool stopRecording();
237 bool startCalibration();
238 bool stopCalibration();
239 bool showHeadLimits();
240 void calcCalibParams(
bool shrink);
241 void plotCalibParams();
244 void updatePlotCalibData();
245 config_server::Parameter<bool> m_plotCalibData;
246 visualization_msgs::Marker m_dataMarker;
247 visualization_msgs::Marker m_calibMarker;
248 ros::Publisher m_pub_dataMarker;
249 ros::Publisher m_pub_calibMarker;
255 config_server::Parameter<bool> m_plotData;
256 plot_msgs::PlotManagerFS m_PM;
257 void configurePlotManager();
258 void callbackPlotData();
266 PM_TARGETPITCHEFFORT,