1
+ /* Generated from orogen/lib/orogen/templates/tasks/Task.cpp */
2
+
3
+ #include " RockTutorialControl.hpp"
4
+
5
+ using namespace rock_tutorial ;
6
+
7
+ RockTutorialControl::RockTutorialControl (std::string const & name, TaskCore::TaskState initial_state)
8
+ : RockTutorialControlBase(name, initial_state)
9
+ {
10
+ }
11
+
12
+ RockTutorialControl::RockTutorialControl (std::string const & name, RTT::ExecutionEngine* engine, TaskCore::TaskState initial_state)
13
+ : RockTutorialControlBase(name, engine, initial_state)
14
+ {
15
+ }
16
+
17
+ RockTutorialControl::~RockTutorialControl ()
18
+ {
19
+ }
20
+
21
+ /* *
22
+ * Makes sure that angles are between PI and -PI.
23
+ */
24
+ void RockTutorialControl::constrainAngle (double & angle)
25
+ {
26
+ if (angle < -M_PI)
27
+ angle = angle + 2 * M_PI;
28
+ else if (angle > M_PI)
29
+ angle = angle - 2 * M_PI;
30
+ }
31
+
32
+ /* *
33
+ * This method constrains the relativ rotation and translation
34
+ * of a 2d motion command.
35
+ * Rotation should be between PI an -PI.
36
+ * Translation should be between 10 and -10.
37
+ */
38
+ void RockTutorialControl::constrainValues (base::MotionCommand2D& motionCommand)
39
+ {
40
+ if (motionCommand.rotation > M_PI)
41
+ motionCommand.rotation = M_PI;
42
+ else if (motionCommand.rotation < -M_PI)
43
+ motionCommand.rotation = -M_PI;
44
+
45
+ if (motionCommand.translation > 10 )
46
+ motionCommand.translation = 10 ;
47
+ else if (motionCommand.translation < -10 )
48
+ motionCommand.translation = -10 ;
49
+ }
50
+
51
+ // / The following lines are template definitions for the various state machine
52
+ // hooks defined by Orocos::RTT. See RockTutorialControl.hpp for more detailed
53
+ // documentation about them.
54
+
55
+ // bool RockTutorialControl::configureHook()
56
+ // {
57
+ // if (! RockTutorialControlBase::configureHook())
58
+ // return false;
59
+ // return true;
60
+ // }
61
+
62
+ bool RockTutorialControl::startHook ()
63
+ {
64
+ // check if input ports are connected
65
+ if (!_motion_command.connected ())
66
+ {
67
+ std::cerr << TaskContext::getName () << " : "
68
+ << " Input port 'motion_command' is not connected." << std::endl;
69
+ return false ;
70
+ }
71
+
72
+ // set variables to zero
73
+ actualCommand.rotation = 0 ;
74
+ actualCommand.translation = 0 ;
75
+ taskPeriod = 0 ;
76
+ currentHeading = 0 ;
77
+ currentRoll = 0 ;
78
+ currentPose.orientation = base::Quaterniond::Identity ();
79
+ currentPose.position = base::Vector3d::Zero ();
80
+ return true ;
81
+ }
82
+
83
+ void RockTutorialControl::updateHook ()
84
+ {
85
+ // get triggering interval of this task context
86
+ if (taskPeriod <= 0 )
87
+ {
88
+ taskPeriod = TaskContext::getPeriod ();
89
+ if (taskPeriod <= 0 )
90
+ {
91
+ std::cerr << TaskContext::getName () << " : "
92
+ << " This task needs to be a periodic triggering task." << std::endl;
93
+ error ();
94
+ return ;
95
+ }
96
+ }
97
+
98
+ // read new motion command if available
99
+ base::MotionCommand2D motionCommand;
100
+ if (_motion_command.readNewest (motionCommand) == RTT::NewData)
101
+ {
102
+ actualCommand = motionCommand;
103
+ constrainValues (actualCommand);
104
+ }
105
+
106
+ // translation and rotation relativ to the task period
107
+ double delta_translation = actualCommand.translation * taskPeriod;
108
+ double delta_rotation = actualCommand.rotation * taskPeriod;
109
+
110
+ // set current yaw and roll
111
+ currentHeading += delta_rotation;
112
+ currentRoll += delta_translation * -2 ;
113
+ constrainAngle (currentHeading);
114
+ constrainAngle (currentRoll);
115
+
116
+ // calculate new absolut values for position and orientation
117
+ currentPose.position += Eigen::AngleAxis<double >(currentHeading, Eigen::Vector3d::UnitZ ()) * Eigen::Vector3d (0 , delta_translation, 0 );
118
+ currentPose.orientation = Eigen::AngleAxis<double >(currentHeading, Eigen::Vector3d::UnitZ ()) * Eigen::AngleAxis<double >(currentRoll, Eigen::Vector3d::UnitX ());
119
+ currentPose.orientation .normalize ();
120
+
121
+ // write pose on output port
122
+ if (_pose.connected ())
123
+ _pose.write (currentPose);
124
+ }
125
+
126
+ // void RockTutorialControl::errorHook()
127
+ // {
128
+ //
129
+ // }
130
+
131
+ // void RockTutorialControl::stopHook()
132
+ // {
133
+ //
134
+ // }
135
+
136
+ // void RockTutorialControl::cleanupHook()
137
+ // {
138
+ //
139
+ // }
0 commit comments