model_actuator.cc
Go to the documentation of this file.
1 //
3 // File: model_laser.c
4 // Author: Richard Vaughan
5 // Date: 10 June 2004
6 //
7 // CVS info:
8 // $Source: /home/tcollett/stagecvs/playerstage-cvs/code/stage/libstage/model_position.cc,v $
9 // $Author: rtv $
10 // $Revision$
11 //
13 
14 
15 #include <sys/time.h>
16 #include <math.h>
17 #include <stdlib.h>
18 
19 //#define DEBUG
20 #include "stage.hh"
21 #include "worldfile.hh"
22 
23 using namespace Stg;
24 
59 static const double WATTS_KGMS = 5.0; // cost per kg per meter per second
60 static const double WATTS_BASE = 2.0; // base cost of position device
61 
63  Model* parent,
64  const std::string& type ) :
65  Model( world, parent, type ),
66  goal(0),
67  pos(0),
68  max_speed(1),
69  min_position(0),
70  max_position(1),
71  start_position(0),
72  control_mode( CONTROL_VELOCITY ),
73  actuator_type( TYPE_LINEAR ),
74  axis(0,0,0)
75 {
76  this->SetWatts( WATTS_BASE );
77 
78  // sensible position defaults
79  //this->SetVelocity( Velocity(0,0,0,0) );
80  this->SetBlobReturn(true);
81 
82  // Allow the models to move
83  //VelocityEnable();
84 }
85 
86 
88 {
89  // nothing to do
90 }
91 
92 void ModelActuator::Load( void )
93 {
94  Model::Load();
95 
96  // load steering mode
97  if( wf->PropertyExists( wf_entity, "type" ) )
98  {
99  const std::string& type_str =
100  wf->ReadString( wf_entity, "type", "linear" );
101 
102  if( type_str == "linear" )
104  else if ( type_str == "rotational" )
106  else
107  {
108  PRINT_ERR1( "invalid actuator type specified: \"%s\" - should be one of: \"linear\" or \"rotational\". Using \"linear\" as default.", type_str.c_str() );
109  }
110  }
111 
112  if (actuator_type == TYPE_LINEAR)
113  {
114  // if we are a linear actuator find the axis we operate in
115  if( wf->PropertyExists( wf_entity, "axis" ) )
116  {
117  wf->ReadTuple( wf_entity, "axis", 0, 3, "fff", &axis.x, &axis.y, &axis.z );
118 
119  // normalise the axis
120  double length = sqrt(axis.x*axis.x + axis.y*axis.y + axis.z*axis.z);
121  if (length == 0)
122  {
123  PRINT_ERR( "zero length vector specified for actuator axis, using (1,0,0) instead" );
124  axis.x = 1;
125  }
126  else
127  {
128  axis.x /= length;
129  axis.y /= length;
130  axis.z /= length;
131  }
132  }
133  }
134 
135  if( wf->PropertyExists( wf_entity, "max_speed" ) )
136  {
137  max_speed = wf->ReadFloat ( wf_entity, "max_speed", 1 );
138  }
139 
140  if( wf->PropertyExists( wf_entity, "max_position" ) )
141  {
142  max_position = wf->ReadFloat( wf_entity, "max_position", 1 );
143  }
144 
145  if( wf->PropertyExists( wf_entity, "min_position" ) )
146  {
147  min_position = wf->ReadFloat( wf_entity, "min_position", 0 );
148  }
149 
150  if( wf->PropertyExists( wf_entity, "start_position" ) )
151  {
152  start_position = wf->ReadFloat ( wf_entity, "start_position", 0 );
153 
154  Pose DesiredPose = InitialPose;
155 
156  cosa = cos(InitialPose.a);
157  sina = sin(InitialPose.a);
158 
159  switch (actuator_type)
160  {
161  case TYPE_LINEAR:
162  {
163  double cosa = cos(DesiredPose.a);
164  double sina = sin(DesiredPose.a);
165 
166  DesiredPose.x += (axis.x * cosa - axis.y * sina) * start_position;
167  DesiredPose.y += (axis.x * sina + axis.y * cosa) * start_position;
168  DesiredPose.z += axis.z * start_position;
169  SetPose( DesiredPose );
170  } break;
171 
172  case TYPE_ROTATIONAL:
173  {
174  DesiredPose.a += start_position;
175  SetPose( DesiredPose);
176  }break;
177  default:
178  PRINT_ERR1( "unrecognized actuator type %d", actuator_type );
179  }
180  }
181 
182 }
183 
185 {
186  PRINT_DEBUG1( "[%lu] actuator update", 0 );
187 
188  // stop by default
189  double velocity = 0;
190 
191  // update current position
192  Pose CurrentPose = GetPose();
193  // just need to get magnitude difference;
194  Pose PoseDiff( CurrentPose.x - InitialPose.x,
195  CurrentPose.y - InitialPose.y,
196  CurrentPose.z - InitialPose.z,
197  CurrentPose.a - InitialPose.a );
198 
199  switch (actuator_type)
200  {
201  case TYPE_LINEAR:
202  {
203  // When the velocity is applied, it will automatically be rotated by the angle the model is at
204  // So, rotate the axis of movement by the model angle before doing a dot product to find the actuator position
205  pos = (PoseDiff.x * cosa - PoseDiff.y * sina)*axis.x + (PoseDiff.x * sina+ PoseDiff.y * cosa)* axis.y + PoseDiff.z * axis.z;
206  } break;
207  case TYPE_ROTATIONAL:
208  {
209  pos = PoseDiff.a;
210  } break;
211  default:
212  PRINT_ERR1( "unrecognized actuator type %d", actuator_type );
213  }
214 
215 
216  if( this->subs ) // no driving if noone is subscribed
217  {
218  switch( control_mode )
219  {
220  case CONTROL_VELOCITY :
221  {
222  PRINT_DEBUG( "actuator velocity control mode" );
223  PRINT_DEBUG2( "model %s command(%.2f)",
224  this->token,
225  this->goal);
226  if ((pos <= min_position && goal < 0) || (pos >= max_position && goal > 0))
227  velocity = 0;
228  else
229  velocity = goal;
230  } break;
231 
232  case CONTROL_POSITION:
233  {
234  PRINT_DEBUG( "actuator position control mode" );
235 
236 
237  // limit our axis
238  if (goal < min_position)
239  goal = min_position;
240  else if (goal > max_position)
241  goal = max_position;
242 
243  double error = goal - pos;
244  velocity = error;
245 
246  PRINT_DEBUG1( "error: %.2f\n", error);
247 
248  }
249  break;
250 
251  default:
252  PRINT_ERR1( "unrecognized actuator command mode %d", control_mode );
253  }
254 
255  // simple model of power consumption
256  //TODO power consumption
257 
258  // speed limits for controller
259  if (velocity < -max_speed)
260  velocity = -max_speed;
261  else if (velocity > max_speed)
262  velocity = max_speed;
263 
264  Velocity outvel;
265  switch (actuator_type)
266  {
267  case TYPE_LINEAR:
268  {
269  outvel.x = axis.x * velocity;
270  outvel.y = axis.y * velocity;
271  outvel.z = axis.z * velocity;
272  outvel.a = 0;
273  } break;
274  case TYPE_ROTATIONAL:
275  {
276  outvel.x = outvel.y = outvel.z = 0;
277  outvel.a = velocity;
278  }break;
279  default:
280  PRINT_ERR1( "unrecognized actuator type %d", actuator_type );
281  }
282 
283  // TODO - deal with velocity
284  //this->SetVelocity( outvel );
285  //this->GPoseDirtyTree();
286  PRINT_DEBUG4( " Set Velocity: [ %.4f %.4f %.4f %.4f ]\n",
287  outvel.x, outvel.y, outvel.z, outvel.a );
288  }
289 
290  Model::Update();
291 }
292 
294 {
295  Model::Startup();
296 
297  PRINT_DEBUG( "position startup" );
298 }
299 
301 {
302  PRINT_DEBUG( "position shutdown" );
303 
304  // safety features!
305  goal = 0;
306 
307  // velocity.Zero();
308 
309  Model::Shutdown();
310 }
311 
312 void ModelActuator::SetSpeed( double speed)
313 {
315  goal = speed;
316 }
317 
318 
320 {
322  goal = pos;
323 }
Model class
Definition: stage.hh:1742
meters_t y
Definition: stage.hh:485
virtual void Startup()
int subs
the number of subscriptions to this model
Definition: stage.hh:1959
meters_t x
Definition: stage.hh:485
World class
Definition: stage.hh:814
The Stage library uses its own namespace.
Definition: canvas.hh:8
void SetPose(const Pose &pose)
Definition: model.cc:1396
static const double WATTS_KGMS
ActuatorType actuator_type
Definition: stage.hh:3078
virtual void Shutdown()
const std::string ReadString(int entity, const char *name, const std::string &value)
Definition: worldfile.cc:1376
std::string token
Definition: stage.hh:696
meters_t z
Definition: stage.hh:485
int ReadTuple(const int entity, const char *name, const unsigned int first, const unsigned int num, const char *format,...)
Definition: worldfile.cc:1506
ControlMode control_mode
Definition: stage.hh:3077
#define PRINT_DEBUG2(m, a, b)
Definition: stage.hh:668
point3_t axis
Definition: stage.hh:3079
virtual void Load()
double max_speed
Definition: stage.hh:3071
void GoTo(double pose)
meters_t z
location in 3 axes
Definition: stage.hh:251
meters_t y
Definition: stage.hh:251
Worldfile * wf
Definition: stage.hh:2015
void SetSpeed(double speed)
int wf_entity
Definition: stage.hh:2016
bool PropertyExists(int section, const char *token)
Definition: worldfile.cc:1324
#define PRINT_ERR(m)
Definition: stage.hh:625
void SetBlobReturn(bool val)
Definition: model.cc:1303
virtual void Startup()
Definition: model.cc:707
virtual void Update()
Definition: model.cc:735
double max_position
Definition: stage.hh:3073
virtual void Load()
Definition: model.cc:1422
virtual void Update()
#define PRINT_DEBUG1(m, a)
Definition: stage.hh:667
double ReadFloat(int entity, const char *name, double value)
Definition: worldfile.cc:1434
void SetWatts(watts_t watts)
Definition: model.cc:1338
#define PRINT_DEBUG4(m, a, b, c, d)
Definition: stage.hh:670
#define PRINT_ERR1(m, a)
Definition: stage.hh:626
double start_position
Definition: stage.hh:3074
double min_position
Definition: stage.hh:3072
ModelActuator(World *world, Model *parent, const std::string &type)
virtual void Shutdown()
Definition: model.cc:723
Pose GetPose() const
Definition: stage.hh:2382
radians_t a
rotation about the z axis.
Definition: stage.hh:252
#define PRINT_DEBUG(m)
Definition: stage.hh:666
static const double WATTS_BASE
meters_t x
Definition: stage.hh:251


stage
Author(s): Richard Vaughan , Brian Gerkey , Reed Hedges , Andrew Howard , Toby Collett , Pooya Karimian , Jeremy Asher , Alex Couture-Beil , Geoff Biggs , Rich Mattes , Abbas Sadat
autogenerated on Mon Jun 10 2019 15:06:09