PageRenderTime 55ms CodeModel.GetById 20ms RepoModel.GetById 1ms app.codeStats 0ms

/src/hokuyo_aist/python/hokuyo_aist.cpp

https://github.com/naderman/gearbox
C++ | 162 lines | 117 code | 15 blank | 30 comment | 3 complexity | 4b94754f11d495701174d3456a529096 MD5 | raw file
  1. #include <hokuyo_aist.h>
  2. using namespace hokuyo_aist;
  3. #include <boost/python.hpp>
  4. #include <boost/python/def.hpp>
  5. #include <iostream>
  6. class HokuyoErrorWrap : public HokuyoError, public boost::python::wrapper<HokuyoError>
  7. {
  8. public:
  9. HokuyoErrorWrap (unsigned int code, std::string desc)
  10. : HokuyoError (code, desc)
  11. {}
  12. unsigned int Code (void) const throw ()
  13. {
  14. if (boost::python::override f = get_override ("Code"))
  15. return f ();
  16. return HokuyoError::Code ();
  17. }
  18. unsigned int DefaultCode (void) const throw ()
  19. { return HokuyoError::Code (); }
  20. const char* what (void) const throw ()
  21. {
  22. if (boost::python::override f = get_override ("what"))
  23. return f ();
  24. return HokuyoError::what ();
  25. }
  26. const char* Defaultwhat (void) const throw ()
  27. { return HokuyoError::what (); }
  28. std::string AsString (void) const throw ()
  29. {
  30. if (boost::python::override f = get_override ("AsString"))
  31. return f ();
  32. return HokuyoError::AsString ();
  33. }
  34. std::string DefaultAsString (void) const throw ()
  35. { return HokuyoError::AsString (); }
  36. };
  37. class HokuyoDataWrap : public HokuyoData, public boost::python::wrapper<HokuyoData>
  38. {
  39. public:
  40. HokuyoDataWrap (void)
  41. : HokuyoData ()
  42. {}
  43. HokuyoDataWrap (uint32_t *ranges, unsigned int length, bool error, unsigned int time)
  44. : HokuyoData (ranges, length, error, time)
  45. {}
  46. HokuyoDataWrap (uint32_t *ranges, uint32_t *intensities, unsigned int length, bool error,
  47. unsigned int time)
  48. : HokuyoData (ranges, intensities, length, error, time)
  49. {}
  50. uint32_t Range (unsigned int index)
  51. { return _ranges[index]; }
  52. uint32_t Intensity (unsigned int index)
  53. { return _intensities[index]; }
  54. };
  55. BOOST_PYTHON_MEMBER_FUNCTION_OVERLOADS (HokuyoLaserOverloads1, GetRanges, 1, 4)
  56. BOOST_PYTHON_MEMBER_FUNCTION_OVERLOADS (HokuyoLaserOverloads2, GetRangesByAngle, 3, 4)
  57. BOOST_PYTHON_MEMBER_FUNCTION_OVERLOADS (HokuyoLaserOverloads3, GetNewRanges, 1, 4)
  58. BOOST_PYTHON_MEMBER_FUNCTION_OVERLOADS (HokuyoLaserOverloads4, GetNewRangesByAngle, 3, 4)
  59. BOOST_PYTHON_MEMBER_FUNCTION_OVERLOADS (HokuyoLaserOverloads5, GetNewRangesAndIntensities, 1, 4)
  60. BOOST_PYTHON_MEMBER_FUNCTION_OVERLOADS (HokuyoLaserOverloads6, GetNewRangesAndIntensitiesByAngle, 3, 4)
  61. BOOST_PYTHON_MODULE (hokuyo_aist)
  62. {
  63. using namespace boost::python;
  64. class_<HokuyoErrorWrap, boost::noncopyable> ("HokuyoError", init<unsigned int, std::string> ())
  65. .def ("Code", &HokuyoError::Code, &HokuyoErrorWrap::DefaultCode)
  66. .def ("what", &HokuyoError::what, &HokuyoErrorWrap::Defaultwhat)
  67. .def ("AsString", &HokuyoError::AsString, &HokuyoErrorWrap::DefaultAsString)
  68. ;
  69. scope ().attr ("HOKUYO_ERR_READ") = HOKUYO_ERR_READ;
  70. scope ().attr ("HOKUYO_ERR_WRITE") = HOKUYO_ERR_WRITE;
  71. scope ().attr ("HOKUYO_ERR_PROTOCOL") = HOKUYO_ERR_PROTOCOL;
  72. scope ().attr ("HOKUYO_ERR_CHANGEBAUD") = HOKUYO_ERR_CHANGEBAUD;
  73. scope ().attr ("HOKUYO_ERR_CONNECT_FAILED") = HOKUYO_ERR_CONNECT_FAILED;
  74. scope ().attr ("HOKUYO_ERR_CLOSE_FAILED") = HOKUYO_ERR_CLOSE_FAILED;
  75. scope ().attr ("HOKUYO_ERR_NODESTINATION") = HOKUYO_ERR_NODESTINATION;
  76. scope ().attr ("HOKUYO_ERR_BADFIRMWARE") = HOKUYO_ERR_BADFIRMWARE;
  77. scope ().attr ("HOKUYO_ERR_SCIPVERSION") = HOKUYO_ERR_SCIPVERSION;
  78. scope ().attr ("HOKUYO_ERR_MEMORY") = HOKUYO_ERR_MEMORY;
  79. scope ().attr ("HOKUYO_ERR_UNSUPPORTED") = HOKUYO_ERR_UNSUPPORTED;
  80. scope ().attr ("HOKUYO_ERR_BADARG") = HOKUYO_ERR_BADARG;
  81. scope ().attr ("HOKUYO_ERR_NODATA") = HOKUYO_ERR_NODATA;
  82. scope ().attr ("HOKUYO_ERR_NOTSERIAL") = HOKUYO_ERR_NOTSERIAL;
  83. // TODO: this causes an undefined symbol error when importing into Python
  84. // class_<HokuyoSensorInfo> ("HokuyoSensorInfo")
  85. // .def ("AsString", &HokuyoSensorInfo::AsString)
  86. // .def_readonly ("vendor", &HokuyoSensorInfo::vendor)
  87. // .def_readonly ("product", &HokuyoSensorInfo::product)
  88. // .def_readonly ("firmware", &HokuyoSensorInfo::firmware)
  89. // .def_readonly ("protocol", &HokuyoSensorInfo::protocol)
  90. // .def_readonly ("serial", &HokuyoSensorInfo::serial)
  91. // .def_readonly ("model", &HokuyoSensorInfo::model)
  92. // .def_readonly ("minRange", &HokuyoSensorInfo::minRange)
  93. // .def_readonly ("maxRange", &HokuyoSensorInfo::maxRange)
  94. // .def_readonly ("steps", &HokuyoSensorInfo::steps)
  95. // .def_readonly ("firstStep", &HokuyoSensorInfo::firstStep)
  96. // .def_readonly ("lastStep", &HokuyoSensorInfo::lastStep)
  97. // .def_readonly ("frontStep", &HokuyoSensorInfo::frontStep)
  98. // .def_readonly ("standardSpeed", &HokuyoSensorInfo::standardSpeed)
  99. // .def_readonly ("power", &HokuyoSensorInfo::power)
  100. // .def_readonly ("speed", &HokuyoSensorInfo::speed)
  101. // .def_readonly ("speedLevel", &HokuyoSensorInfo::speedLevel)
  102. // .def_readonly ("measureState", &HokuyoSensorInfo::measureState)
  103. // .def_readonly ("baud", &HokuyoSensorInfo::baud)
  104. // .def_readonly ("time", &HokuyoSensorInfo::time)
  105. // .def_readonly ("sensorDiagnostic", &HokuyoSensorInfo::sensorDiagnostic)
  106. // .def_readonly ("minAngle", &HokuyoSensorInfo::minAngle)
  107. // .def_readonly ("maxAngle", &HokuyoSensorInfo::maxAngle)
  108. // .def_readonly ("resolution", &HokuyoSensorInfo::resolution)
  109. // .def_readonly ("scanableSteps", &HokuyoSensorInfo::scanableSteps)
  110. // ;
  111. class_ <HokuyoDataWrap, boost::noncopyable> ("HokuyoData")
  112. .def (init<uint32_t*, unsigned int, bool, unsigned int> ())
  113. .def (init<uint32_t*, uint32_t*, unsigned int, bool, unsigned int> ())
  114. // TODO: write a wrapper function to copy the data into a python array, because this doesn't work
  115. // .def ("Ranges", &HokuyoData::Ranges, return_value_policy<reference_existing_object> (), with_custodian_and_ward_postcall<1, 0> ())
  116. .def ("Range", &HokuyoDataWrap::Range)
  117. .def ("Intensity", &HokuyoDataWrap::Intensity)
  118. .def ("Length", &HokuyoData::Length)
  119. .def ("GetErrorStatus", &HokuyoData::GetErrorStatus)
  120. .def ("ErrorCodeToString", &HokuyoData::ErrorCodeToString)
  121. .def ("TimeStamp", &HokuyoData::TimeStamp)
  122. .def ("AsString", &HokuyoData::AsString)
  123. .def ("CleanUp", &HokuyoData::CleanUp)
  124. ;
  125. class_ <HokuyoLaser> ("HokuyoLaser")
  126. .def ("Open", &HokuyoLaser::Open)
  127. .def ("Close", &HokuyoLaser::Close)
  128. .def ("IsOpen", &HokuyoLaser::IsOpen)
  129. .def ("SetPower", &HokuyoLaser::SetPower)
  130. .def ("SetBaud", &HokuyoLaser::SetBaud)
  131. .def ("Reset", &HokuyoLaser::Reset)
  132. .def ("SetMotorSpeed", &HokuyoLaser::SetMotorSpeed)
  133. .def ("SetHighSensitivity", &HokuyoLaser::SetHighSensitivity)
  134. .def ("GetSensorInfo", &HokuyoLaser::GetSensorInfo)
  135. .def ("GetTime", &HokuyoLaser::GetTime)
  136. .def ("GetRanges", &HokuyoLaser::GetRanges, HokuyoLaserOverloads1 ())
  137. .def ("GetRangesByAngle", &HokuyoLaser::GetRangesByAngle, HokuyoLaserOverloads2 ())
  138. .def ("GetNewRanges", &HokuyoLaser::GetNewRanges, HokuyoLaserOverloads3 ())
  139. .def ("GetNewRangesByAngle", &HokuyoLaser::GetNewRangesByAngle, HokuyoLaserOverloads4 ())
  140. .def ("GetNewRangesAndIntensities", &HokuyoLaser::GetNewRangesAndIntensities, HokuyoLaserOverloads5 ())
  141. .def ("GetNewRangesAndIntensitiesByAngle", &HokuyoLaser::GetNewRangesAndIntensitiesByAngle, HokuyoLaserOverloads6 ())
  142. .def ("SCIPVersion", &HokuyoLaser::SCIPVersion)
  143. .def ("SetVerbose", &HokuyoLaser::SetVerbose)
  144. .def ("StepToAngle", &HokuyoLaser::StepToAngle)
  145. .def ("AngleToStep", &HokuyoLaser::AngleToStep)
  146. ;
  147. }