Dynamixel  2.9.5
RoadNarrows Robotics Dynamixel Package
DynaRecording Class Reference

#include <dynashell_recording.h>

Public Member Functions

 DynaRecording ()
 Default constructor.
 
virtual ~DynaRecording ()
 Default destructor.
 
virtual void Init (int nSamplePeriod, const char *sDate=NULL)
 (Re)Initialize recording. More...
 
virtual int FirstRecord ()
 Get the first record number in the recording. More...
 
virtual int NextRecord (int nRecNum)
 Get the next record number in the recording after the given record number. More...
 
virtual int AddRecord ()
 Add a new empty record to the recording. More...
 
virtual int CopyRecord (int nRecNumDst, int nRecNumSrc)
 Copy source record to destination record. More...
 
virtual int FirstField (int nRecNum)
 Get the first field number in the given record. More...
 
virtual int NextField (int nRecNum, int nFldNum)
 Get the next field number in the record after the given field number. More...
 
virtual int AddFieldTuple (int nRecNum, int nServoId, int nPos, int nSpeed)
 Add new recording field tuple. More...
 
virtual const DynaRecord::FieldTuple_TGetField (const int nRecNum, const int nFldNum) const
 Get the given recorded field tuple. More...
 
virtual int GetNumOfRecords ()
 Get the number of records in the recording. More...
 
virtual int GetNumOfServosInRecording ()
 Get the number of servos in the recording. More...
 
virtual int RegisterServoInfo (int nServoId, uint_t uModelNum)
 Register servo information in recording header. More...
 
virtual const int GetServoId (int nFldNum)
 Get the servo id associated with the given field number. More...
 
virtual bool HasServoAt (int nFldNum, int nServoId) const
 Check if the given servo is associated with the given field number, as per the ordering of the registered servos in the recording header. More...
 
virtual bool HasServo (int nServoId)
 Check if the given servo is in the list of registered servos in the recording header. More...
 
virtual uint_t GetServoModelNumber (int nServoId)
 Get the registered servo model number. More...
 
virtual int GetSamplePeriod () const
 Get the sample period of the recording. More...
 
virtual int SetSamplePeriod (int nSamplePeriod)
 Set the sample period of the recording. More...
 
virtual const char * GetDate () const
 Get the recording data. More...
 
virtual void SetDate (const char *sDate)
 
DynaRecord const & operator[] (const int nRecNum) const
 Subscript operator. More...
 

Static Public Attributes

static const int MaxRecords = 5000
 maximum number of records
 
static const int END = -1
 past-the-end mark
 

Protected Types

typedef vector< ServoInfo_TVecServoInfo
 Map of record servos ids and model numbers indexed by field number.
 
typedef map< int, int > MapIIdx
 Indirect indexing by servo id of servo info map.
 

Protected Attributes

char * m_sDate
 recording date
 
VecServoInfo m_vecServoInfo
 vector of recorded servo information
 
MapIIdx m_mapIIdx
 indirect index map
 
int m_nSamplePeriod
 recording sample period (ms)
 
int m_nRecordCnt
 number of records recorded
 
DynaRecord m_record [MaxRecords]
 the recording data
 

Detailed Description

Dynamixel recording. The recording is usually made by a training session, but does not have to be.

Definition at line 186 of file dynashell_recording.h.

Member Function Documentation

int DynaRecording::AddFieldTuple ( int  nRecNum,
int  nServoId,
int  nPos,
int  nSpeed 
)
virtual

Add new recording field tuple.

The field position must match the registered servo position listed in the header.

Parameters
nRecNumRecord number.
nServoIdServo id of field tuple data.
nPosServo position.
nSpeedServo speed.
Returns
Returns added field number on success.
Returns END of failure to add.

Definition at line 208 of file dynashell_recording.cxx.

Referenced by DynaShellCmdLoadRecording::ParseRecordedData(), and DynaShellCmdTrain::RecordTraining().

212 {
213  int nFieldCnt;
214 
215  nFieldCnt = m_record[nRecNum].GetFieldCount();
216 
217  if( nRecNum == END )
218  {
219  return END;
220  }
221  else if( nRecNum >= m_nRecordCnt )
222  {
223  LOGERROR("Record number %d >= record count = %d.", nRecNum, m_nRecordCnt);
224  return END;
225  }
226  else if( nFieldCnt >= GetNumOfServosInRecording() )
227  {
228  LOGERROR("Record %d field count %d at maximum.", nRecNum, nFieldCnt);
229  return END;
230  }
231  else if( !HasServoAt(nFieldCnt, nServoId) )
232  {
233  LOGERROR("Servo %d not found in recording header at field position %d.",
234  nServoId, nFieldCnt);
235  return END;
236  }
237  else
238  {
239  return m_record[nRecNum].AddField(nPos, nSpeed);
240  }
241 }
DynaRecord m_record[MaxRecords]
the recording data
static const int END
past-the-end mark
virtual bool HasServoAt(int nFldNum, int nServoId) const
Check if the given servo is associated with the given field number, as per the ordering of the regist...
int m_nRecordCnt
number of records recorded
virtual int GetNumOfServosInRecording()
Get the number of servos in the recording.
virtual int AddField(int nPos, int nSpeed)
Add a field tuple to the record.
int GetFieldCount() const
Get the current number of field tuples in record.
int DynaRecording::AddRecord ( )
virtual

Add a new empty record to the recording.

Returns
If the number of records already in the recording is MaxRecords and there are registered servos listed in the recording header, then the new record number is returned.
Otherwise END is returned.

Definition at line 176 of file dynashell_recording.cxx.

Referenced by DynaShellCmdLoadRecording::ParseRecordedData(), and DynaShellCmdTrain::RecordTraining().

177 {
178  int nRecNum;
179 
180  if( (m_nRecordCnt >= MaxRecords) || (GetNumOfServosInRecording() == 0) )
181  {
182  return END;
183  }
184  else
185  {
186  nRecNum = m_nRecordCnt;
187  m_record[nRecNum].ResetFieldCount();
188  ++m_nRecordCnt;
189  return nRecNum;
190  }
191 }
DynaRecord m_record[MaxRecords]
the recording data
static const int END
past-the-end mark
int m_nRecordCnt
number of records recorded
virtual int GetNumOfServosInRecording()
Get the number of servos in the recording.
void ResetFieldCount()
Reset the field count to zero.
static const int MaxRecords
maximum number of records
virtual int DynaRecording::CopyRecord ( int  nRecNumDst,
int  nRecNumSrc 
)
inlinevirtual

Copy source record to destination record.

Parameters
nRecNumDstDestination record number.
nRecNumSrcSource record number.
Returns
On success, DYNA_OK is returned.
On error, the appropriate < 0 negated Dynamixel Error Code is returned.

Definition at line 256 of file dynashell_recording.h.

References DYNA_ECODE_BAD_VAL, and DYNA_OK.

257  {
258  if( (nRecNumDst == END) || (nRecNumDst >= m_nRecordCnt) ||
259  (nRecNumSrc == END) || (nRecNumSrc >= m_nRecordCnt) )
260  {
261  return -DYNA_ECODE_BAD_VAL;
262  }
263 
264  m_record[nRecNumDst] = m_record[nRecNumSrc];
265 
266  return DYNA_OK;
267  }
#define DYNA_OK
not an error, success
Definition: Dynamixel.h:78
DynaRecord m_record[MaxRecords]
the recording data
static const int END
past-the-end mark
int m_nRecordCnt
number of records recorded
#define DYNA_ECODE_BAD_VAL
bad value
Definition: Dynamixel.h:85
virtual int DynaRecording::FirstField ( int  nRecNum)
inlinevirtual

Get the first field number in the given record.

Parameters
nRecNumRecord number.
Returns
If a field is present, returns the first field number (0).
Otherwise END is returned.

Definition at line 278 of file dynashell_recording.h.

Referenced by DynaShellCmdPlay::ControlToGoals(), DynaShellCmdSaveRecording::Save(), and DynaShellCmdPlay::SetRecordGoals().

279  {
280  if( (nRecNum < m_nRecordCnt) && (m_record[nRecNum].GetFieldCount() > 0) )
281  {
282  return 0;
283  }
284  else
285  {
286  return END;
287  }
288  }
DynaRecord m_record[MaxRecords]
the recording data
static const int END
past-the-end mark
int m_nRecordCnt
number of records recorded
virtual int DynaRecording::FirstRecord ( )
inlinevirtual

Get the first record number in the recording.

Returns
If a recording is present, returns the first record number (0).
Otherwise END is returned.

Definition at line 220 of file dynashell_recording.h.

Referenced by DynaShellCmdPlay::Play(), and DynaShellCmdSaveRecording::Save().

221  {
222  return m_nRecordCnt > 0? 0: END;
223  }
static const int END
past-the-end mark
int m_nRecordCnt
number of records recorded
virtual const char* DynaRecording::GetDate ( ) const
inlinevirtual

Get the recording data.

Returns
Returns date string.

Definition at line 457 of file dynashell_recording.h.

Referenced by DynaShellCmdLoadRecording::Load(), DynaShellCmdPlay::Play(), DynaShellCmdPlay::PlotInit(), and DynaShellCmdSaveRecording::Save().

458  {
459  return m_sDate;
460  }
char * m_sDate
recording date
virtual const DynaRecord::FieldTuple_T& DynaRecording::GetField ( const int  nRecNum,
const int  nFldNum 
) const
inlinevirtual

Get the given recorded field tuple.

Parameters
nRecNumRecord number.
nFldNumField number.
Returns
On success, returns the specified field tuple.
On failure, returns DynaRecord::NoField. (use DynaRecord::NaF() to test);

Definition at line 327 of file dynashell_recording.h.

References DynaRecord::NoField.

Referenced by DynaShellCmdSaveRecording::Save(), and DynaShellCmdPlay::SetRecordGoals().

329  {
330  if( (nRecNum < m_nRecordCnt) &&
331  (nFldNum < m_record[nRecNum].GetFieldCount()) )
332  {
333  return m_record[nRecNum][nFldNum];
334  }
335  else
336  {
338  }
339  }
DynaRecord m_record[MaxRecords]
the recording data
static const FieldTuple_T NoField
int m_nRecordCnt
number of records recorded
virtual int DynaRecording::GetNumOfRecords ( )
inlinevirtual

Get the number of records in the recording.

Number of records.

Definition at line 346 of file dynashell_recording.h.

Referenced by DynaShellCmdPlay::ControlToGoals(), DynaShellCmdSaveRecording::Exec(), DynaShellCmdPlay::Exec(), DynaShellCmdLoadRecording::Load(), DynaShellCmdPlay::Play(), DynaShellCmdPlay::PlotInit(), and DynaShellCmdSaveRecording::Save().

347  {
348  return m_nRecordCnt;
349  }
int m_nRecordCnt
number of records recorded
virtual int DynaRecording::GetNumOfServosInRecording ( )
inlinevirtual

Get the number of servos in the recording.

The number of servos is the count of registered servos in the recording header.

Returns
Number of servos.

Definition at line 359 of file dynashell_recording.h.

Referenced by DynaShellCmdPlay::Exec(), DynaShellCmdLoadRecording::Load(), and DynaShellCmdSaveRecording::Save().

360  {
361  return (int)m_vecServoInfo.size();
362  }
VecServoInfo m_vecServoInfo
vector of recorded servo information
virtual int DynaRecording::GetSamplePeriod ( ) const
inlinevirtual

Get the sample period of the recording.

Returns
Returns the sample period in milliseconds.

Definition at line 433 of file dynashell_recording.h.

Referenced by DynaShellCmdPlay::ControlToGoals(), DynaShellCmdLoadRecording::Load(), DynaShellCmdPlay::PlotInit(), and DynaShellCmdSaveRecording::Save().

434  {
435  return m_nSamplePeriod;
436  }
int m_nSamplePeriod
recording sample period (ms)
virtual const int DynaRecording::GetServoId ( int  nFldNum)
inlinevirtual

Get the servo id associated with the given field number.

Returns
On success, returns the servo id. Otherwise DYNA_ID_NONE is returned.

Definition at line 372 of file dynashell_recording.h.

References DYNA_ID_NONE.

Referenced by DynaShellCmdPlay::ControlToGoals(), DynaShellCmdLoadRecording::ParseRecordedData(), DynaShellCmdSaveRecording::Save(), and DynaShellCmdPlay::SetRecordGoals().

373  {
374  return (nFldNum < (int)m_vecServoInfo.size())?
375  m_vecServoInfo[nFldNum].m_nServoId: DYNA_ID_NONE;
376  }
VecServoInfo m_vecServoInfo
vector of recorded servo information
#define DYNA_ID_NONE
no servo id
Definition: Dynamixel.h:145
virtual uint_t DynaRecording::GetServoModelNumber ( int  nServoId)
inlinevirtual

Get the registered servo model number.

Parameters
nServoidServo Id.
Returns
Returns the model number. If no servo is found returns DYNA_MODEL_NUM_GENERIC.

Definition at line 414 of file dynashell_recording.h.

References DYNA_MODEL_NUM_GENERIC.

Referenced by DynaShellCmdPlay::Exec(), and DynaShellCmdSaveRecording::Save().

415  {
416  MapIIdx::iterator iter;
417 
418  if( (iter = m_mapIIdx.find(nServoId)) != m_mapIIdx.end() )
419  {
420  return m_vecServoInfo[m_mapIIdx[nServoId]].m_uModelNum;
421  }
422  else
423  {
424  return DYNA_MODEL_NUM_GENERIC;
425  }
426  }
#define DYNA_MODEL_NUM_GENERIC
generic, base model
Definition: Dynamixel.h:119
VecServoInfo m_vecServoInfo
vector of recorded servo information
MapIIdx m_mapIIdx
indirect index map
virtual bool DynaRecording::HasServo ( int  nServoId)
inlinevirtual

Check if the given servo is in the list of registered servos in the recording header.

Parameters
nServoidServo Id.
Returns
Returns true or false.

Definition at line 401 of file dynashell_recording.h.

Referenced by DynaShellCmdPlay::Exec().

402  {
403  return m_mapIIdx.find(nServoId) != m_mapIIdx.end();
404  }
MapIIdx m_mapIIdx
indirect index map
virtual bool DynaRecording::HasServoAt ( int  nFldNum,
int  nServoId 
) const
inlinevirtual

Check if the given servo is associated with the given field number, as per the ordering of the registered servos in the recording header.

Parameters
nFldNumField number.
nServoidServo Id.
Returns
Returns true or false.

Definition at line 387 of file dynashell_recording.h.

388  {
389  return (nFldNum < (int)m_vecServoInfo.size()) &&
390  (m_vecServoInfo[nFldNum].m_nServoId == nServoId);
391  }
VecServoInfo m_vecServoInfo
vector of recorded servo information
void DynaRecording::Init ( int  nSamplePeriod,
const char *  sDate = NULL 
)
virtual

(Re)Initialize recording.

Any previously recorded data is 'erased'.

Parameters
nSamplePeriodRecording sample period (msec).
sDataRecording date string.

Definition at line 147 of file dynashell_recording.cxx.

Referenced by DynaShell::RecordingInit().

148 {
149  int nRecNum; // working record number
150 
151  // clear vector of information about the servos in recording
152  m_vecServoInfo.clear();
153  m_mapIIdx.clear();
154 
155  SetDate(sDate);
156 
157  m_nSamplePeriod = nSamplePeriod;
158  m_nRecordCnt = 0;
159 
160  // clear any old records
161  for(nRecNum=0; nRecNum<MaxRecords; ++nRecNum)
162  {
163  m_record[nRecNum].ResetFieldCount();
164  }
165 }
DynaRecord m_record[MaxRecords]
the recording data
virtual void SetDate(const char *sDate)
VecServoInfo m_vecServoInfo
vector of recorded servo information
int m_nRecordCnt
number of records recorded
MapIIdx m_mapIIdx
indirect index map
int m_nSamplePeriod
recording sample period (ms)
void ResetFieldCount()
Reset the field count to zero.
static const int MaxRecords
maximum number of records
virtual int DynaRecording::NextField ( int  nRecNum,
int  nFldNum 
)
inlinevirtual

Get the next field number in the record after the given field number.

Parameters
nRecNumRecord number.
nFldNumField number.
Returns
If there is a next field present in the record, returns the next field number.
Otherwise END is returned.

Definition at line 302 of file dynashell_recording.h.

Referenced by DynaShellCmdPlay::ControlToGoals(), DynaShellCmdSaveRecording::Save(), and DynaShellCmdPlay::SetRecordGoals().

303  {
304  if( (nFldNum == END) || (nRecNum >= m_nRecordCnt) )
305  {
306  return END;
307  }
308 
309  ++nFldNum;
310 
311  return nFldNum < m_record[nRecNum].GetFieldCount()? nFldNum: END;
312  }
DynaRecord m_record[MaxRecords]
the recording data
static const int END
past-the-end mark
int m_nRecordCnt
number of records recorded
int GetFieldCount() const
Get the current number of field tuples in record.
virtual int DynaRecording::NextRecord ( int  nRecNum)
inlinevirtual

Get the next record number in the recording after the given record number.

Parameters
nRecNumRecord number.
Returns
If there is a next record present in the recording, returns the next record number.
Otherwise END is returned.

Definition at line 236 of file dynashell_recording.h.

Referenced by DynaShellCmdPlay::Play(), and DynaShellCmdSaveRecording::Save().

237  {
238  if( nRecNum == END )
239  {
240  return END;
241  }
242  ++nRecNum;
243  return nRecNum < m_nRecordCnt? nRecNum: END;
244  }
static const int END
past-the-end mark
int m_nRecordCnt
number of records recorded
DynaRecord const& DynaRecording::operator[] ( const int  nRecNum) const
inline

Subscript operator.

Parameters
nRecNumRecord number subscript.
Returns
DynaRecord.

Definition at line 471 of file dynashell_recording.h.

472  {
473  // TODO return nRecNum < m_nFieldCnt? m_record[nRecNum]: NoRecord;
474  return m_record[nRecNum];
475  }
DynaRecord m_record[MaxRecords]
the recording data
int DynaRecording::RegisterServoInfo ( int  nServoId,
uint_t  uModelNum 
)
virtual

Register servo information in recording header.

Note
The order of registration is important and must match field order.
All relevant servos must be registered prior to adding records.
Parameters
nServoIdServo Id.
uModelNumServo model number.
Returns
On success, DYNA_OK is returned.
On error, the appropriate < 0 negated Dynamixel Error Code is returned.

Definition at line 254 of file dynashell_recording.cxx.

References DYNA_ECODE_BAD_VAL, DYNA_ECODE_RSRC, DYNA_ID_NUMOF, DYNA_OK, ServoInfo_T::m_nServoId, and ServoInfo_T::m_uModelNum.

Referenced by DynaShellCmdTrain::Exec(), and DynaShellCmdLoadRecording::ParseServoList().

255 {
256  ServoInfo_T info;
257 
258  // duplicate servo registration
259  if( m_mapIIdx.find(nServoId) != m_mapIIdx.end() )
260  {
261  return -DYNA_ECODE_BAD_VAL;
262  }
263  else if( (int)m_vecServoInfo.size() >= DYNA_ID_NUMOF )
264  {
265  return -DYNA_ECODE_RSRC;
266  }
267 
268  info.m_nServoId = nServoId;
269  info.m_uModelNum = uModelNum;
270 
271  m_vecServoInfo.push_back(info);
272  m_mapIIdx[nServoId] = (int)m_vecServoInfo.size() - 1;
273 
274  return DYNA_OK;
275 }
#define DYNA_OK
not an error, success
Definition: Dynamixel.h:78
Key Recorded Servo Information Structure Type.
#define DYNA_ID_NUMOF
number of unique servo id&#39;s
Definition: Dynamixel.h:148
int m_nServoId
servo id
VecServoInfo m_vecServoInfo
vector of recorded servo information
#define DYNA_ECODE_RSRC
no resource
Definition: Dynamixel.h:96
MapIIdx m_mapIIdx
indirect index map
uint_t m_uModelNum
servo model number
#define DYNA_ECODE_BAD_VAL
bad value
Definition: Dynamixel.h:85
void DynaRecording::SetDate ( const char *  sDate)
virtual

Set recording date string.

Parameters
sDataRecording date string. If NULL, then the current local time and date are used.

Definition at line 283 of file dynashell_recording.cxx.

References DELOBJ, and newstr().

Referenced by DynaShellCmdLoadRecording::SetDateField().

284 {
285  size_t n = 32; // size of data buffer
286  time_t tnow; // time now
287  struct tm bdtime; // broken-down time
288 
289  // delete old recording date string
290  DELOBJ(m_sDate);
291 
292  // default recording date string is 'now'
293  if( sDate == NULL )
294  {
295  m_sDate = new char[n];
296  tnow = time(NULL);
297  if( (localtime_r(&tnow, &bdtime) == NULL) ||
298  (strftime(m_sDate, n, "%a %b %H:%M:%S %Y", &bdtime) == 0) )
299  {
300  strcpy(m_sDate, "Unknown Date");
301  }
302  }
303 
304  // set recording date string
305  else
306  {
307  m_sDate = newstr(sDate);
308  }
309 }
char * newstr(const char *s)
Allocate new duplicated string.
char * m_sDate
recording date
#define DELOBJ(p)
Delete an allocated object convenience macro.
virtual int DynaRecording::SetSamplePeriod ( int  nSamplePeriod)
inlinevirtual

Set the sample period of the recording.

Parameters
nSamplePeriodSample period in milliseconds.

Definition at line 443 of file dynashell_recording.h.

Referenced by DynaShellCmdLoadRecording::Load().

444  {
445  if( nSamplePeriod < 1 )
446  {
447  nSamplePeriod = 1;
448  }
449  m_nSamplePeriod = nSamplePeriod;
450  }
int m_nSamplePeriod
recording sample period (ms)

The documentation for this class was generated from the following files: