11 #include "hrpsys/util/VectorConvert.h" 12 #include "hrpsys/idl/RobotHardwareService.hh" 14 #include <hrpModel/ModelLoaderUtil.h> 20 #define deg2rad(x)((x)*M_PI/180) 26 "implementation_id",
"SoftErrorLimiter",
27 "type_name",
"SoftErrorLimiter",
28 "description",
"soft error limiter",
29 "version", HRPSYS_PACKAGE_VERSION,
31 "category",
"example",
32 "activity_type",
"DataFlowComponent",
35 "lang_type",
"compile",
37 "conf.default.debugLevel",
"0",
42 static std::ostream&
operator<<(std::ostream& os,
const struct RTC::Time &tm)
44 int pre = os.precision();
45 os.setf(std::ios::fixed);
46 os << std::setprecision(6)
47 << (tm.sec + tm.nsec/1e9)
48 << std::setprecision(pre);
49 os.unsetf(std::ios::fixed);
56 m_qRefIn(
"qRef", m_qRef),
57 m_qCurrentIn(
"qCurrent", m_qCurrent),
58 m_servoStateIn(
"servoStateIn", m_servoState),
60 m_servoStateOut(
"servoStateOut", m_servoState),
61 m_beepCommandOut(
"beepCommand", m_beepCommand),
62 m_SoftErrorLimiterServicePort(
"SoftErrorLimiterService"),
66 is_beep_port_connected(false)
81 std::cerr <<
"[" <<
m_profile.instance_name <<
"] onInitialize()" << std::endl;
114 std::string nameServer = rtcManager.
getConfig()[
"corba.nameservers"];
115 int comPos = nameServer.find(
",");
117 comPos = nameServer.length();
119 nameServer = nameServer.substr(0, comPos);
122 CosNaming::NamingContext::_duplicate(
naming.getRootContext())
124 std::cerr <<
"[" <<
m_profile.instance_name <<
"] failed to load model[" << prop[
"model"] <<
"] in " 126 return RTC::RTC_ERROR;
129 std::cout <<
"[" <<
m_profile.instance_name <<
"] dof = " <<
m_robot->numJoints() << std::endl;
130 if (!
m_robot->init())
return RTC::RTC_ERROR;
133 for(
unsigned int i = 0;
i <
m_robot->numJoints();
i++) {
136 status |= 1<< OpenHRP::RobotHardwareService::CALIB_STATE_SHIFT;
137 status |= 1<< OpenHRP::RobotHardwareService::POWER_STATE_SHIFT;
138 status |= 1<< OpenHRP::RobotHardwareService::SERVO_STATE_SHIFT;
139 status |= 0<< OpenHRP::RobotHardwareService::SERVO_ALARM_SHIFT;
140 status |= 0<< OpenHRP::RobotHardwareService::DRIVER_TEMP_SHIFT;
159 for(
int i = 0;
i < ijoints.size();
i++) {
161 if((!!lk) && (lk->
jointId >= 0)) {
162 std::cout <<
"[" <<
m_profile.instance_name <<
"] " 163 <<
"disable ErrorLimit, joint : " << ijoints[i]
164 <<
" (id = " << lk->
jointId <<
")" << std::endl;
195 std::cerr <<
"[" <<
m_profile.instance_name<<
"] onActivated(" << ec_id <<
")" << std::endl;
201 std::cerr <<
"[" <<
m_profile.instance_name<<
"] onDeactivated(" << ec_id <<
")" << std::endl;
209 static bool debug_print_velocity_first =
false;
210 static bool debug_print_position_first =
false;
211 static bool debug_print_error_first =
false;
221 std::cerr <<
"[" <<
m_profile.instance_name<<
"] beepCommand data port connection found! Use BeeperRTC." << std::endl;
249 bool soft_limit_error =
false;
250 bool velocity_limit_error =
false;
251 bool position_limit_error =
false;
255 static std::vector<double> prev_angle;
256 if ( prev_angle.size() !=
m_qRef.data.length() ) {
257 prev_angle.resize(
m_qRef.data.length(), 0);
258 for (
unsigned int i = 0;
i <
m_qRef.data.length();
i++ ){
262 std::vector<int> servo_state;
263 servo_state.resize(
m_qRef.data.length(), 0);
264 for (
unsigned int i = 0;
i <
m_qRef.data.length();
i++ ){
265 servo_state[
i] = (
m_servoState.data[
i][0] & OpenHRP::RobotHardwareService::SERVO_STATE_MASK) >> OpenHRP::RobotHardwareService::SERVO_STATE_SHIFT;
285 for (
unsigned int i = 0;
i <
m_qRef.data.length();
i++ ){
293 double qvel = (
m_qRef.data[i] - prev_angle[i]) /
dt;
294 double lvlimit =
m_robot->joint(i)->lvlimit + 0.000175;
295 double uvlimit =
m_robot->joint(i)->uvlimit - 0.000175;
297 if ( servo_state[i] == 1 && (lvlimit < uvlimit) && ((lvlimit > qvel) || (uvlimit < qvel)) ) {
300 <<
"] velocity limit over " <<
m_robot->joint(i)->name <<
"(" << i <<
"), qvel=" << qvel
301 <<
", lvlimit =" << lvlimit
302 <<
", uvlimit =" << uvlimit
303 <<
", servo_state = " << ( servo_state[i] ?
"ON" :
"OFF");
307 if ( lvlimit > qvel ) {
308 limited = total_lower_limit =
std::max(prev_angle[i] + lvlimit *
dt, total_lower_limit);
310 if ( uvlimit < qvel ) {
311 limited = total_upper_limit =
std::min(prev_angle[i] + uvlimit *
dt, total_upper_limit);
314 std::cerr <<
", q(limited) = " << limited << std::endl;
316 velocity_limit_error =
true;
322 double llimit =
m_robot->joint(i)->llimit;
323 double ulimit =
m_robot->joint(i)->ulimit;
326 llimit = it->second.getLlimit(
m_qRef.data[it->second.getTargetJointId()]);
327 ulimit = it->second.getUlimit(
m_qRef.data[it->second.getTargetJointId()]);
330 bool servo_limit_state = (llimit < ulimit) && ((llimit >
m_qRef.data[i]) || (ulimit <
m_qRef.data[i]));
331 if ( servo_state[i] == 1 && servo_limit_state ) {
334 <<
"] position limit over " <<
m_robot->joint(i)->name <<
"(" << i <<
"), qRef=" <<
m_qRef.data[i]
335 <<
", llimit =" << llimit
336 <<
", ulimit =" << ulimit
337 <<
", servo_state = " << ( servo_state[i] ?
"ON" :
"OFF")
338 <<
", prev_angle = " << prev_angle[i];
342 if ( llimit >
m_qRef.data[i] && prev_angle[i] >
m_qRef.data[i] ) {
343 limited = total_lower_limit =
std::max(llimit, total_lower_limit);
345 if ( ulimit <
m_qRef.data[i] && prev_angle[i] <
m_qRef.data[i] ) {
346 limited = total_upper_limit =
std::min(ulimit, total_upper_limit);
349 std::cerr <<
", q(limited) = " << limited << std::endl;
351 m_servoState.data[i][0] |= (0x200 << OpenHRP::RobotHardwareService::SERVO_ALARM_SHIFT);
352 position_limit_error =
true;
361 double limit =
m_robot->m_servoErrorLimit[i];
363 if ( servo_state[i] == 1 && fabs(error) > limit ) {
366 <<
"] error limit over " <<
m_robot->joint(i)->name <<
"(" << i <<
"), qRef=" << pos_vel_limited_angle
368 <<
", Error=" << error <<
" > " << limit <<
" (limit)" 369 <<
", servo_state = " << ( 1 ?
"ON" :
"OFF");
372 if ( error > limit ) {
373 limited = total_upper_limit =
std::min(
m_qCurrent.data[i] + limit, total_upper_limit);
375 limited = total_lower_limit =
std::max(
m_qCurrent.data[i] - limit, total_lower_limit);
378 std::cerr <<
", q(limited) = " << limited << std::endl;
380 m_servoState.data[i][0] |= (0x040 << OpenHRP::RobotHardwareService::SERVO_ALARM_SHIFT);
381 soft_limit_error =
true;
389 debug_print_velocity_first = !velocity_limit_error;
390 debug_print_position_first = !position_limit_error;
391 debug_print_error_first = !soft_limit_error;
394 if ( soft_limit_error ) {
401 }
else if ( position_limit_error || velocity_limit_error ) {
423 if ( loop % 100 == 1 ) {
424 std::cerr <<
"SoftErrorLimiter is not working..." << std::endl;
425 std::cerr <<
" m_qRef " <<
m_qRef.data.length() << std::endl;
426 std::cerr <<
" m_qCurrent " <<
m_qCurrent.data.length() << std::endl;
427 std::cerr <<
" m_servoState " <<
m_servoState.data.length() << std::endl;
482 RTC::Create<SoftErrorLimiter>,
483 RTC::Delete<SoftErrorLimiter>);
ComponentProfile m_profile
png_infop png_charpp int png_charpp profile
virtual RTC::ReturnCode_t onDeactivated(RTC::UniqueId ec_id)
void SoftErrorLimiterInit(RTC::Manager *manager)
std::vector< bool > m_joint_mask
bool stringTo(To &val, const char *str)
OutPort< TimedLongSeq > m_beepCommandOut
static std::ostream & operator<<(std::ostream &os, const struct RTC::Time &tm)
virtual RTC::ReturnCode_t onExecute(RTC::UniqueId ec_id)
TimedLongSeq m_beepCommand
vstring split(const std::string &input, const std::string &delimiter, bool ignore_empty)
void error(char *msg) const
coil::Properties & getProperties()
SoftErrorLimiter(RTC::Manager *manager)
Constructor.
void readJointLimitTableFromProperties(std::map< std::string, hrp::JointLimitTable > &joint_mm_tables, hrp::BodyPtr m_robot, const std::string &prop_string, const std::string &instance_name)
static Manager & instance()
bool addOutPort(const char *name, OutPortBase &outport)
void setDataPort(RTC::TimedLongSeq &out_data)
int get_num_beep_info() const
void setRobot(boost::shared_ptr< robot > &i_robot)
std::vector< std::string > vstring
SoftErrorLimiterService_impl m_service0
coil::Properties & getConfig()
OutPort< OpenHRP::TimedLongSeqSeq > m_servoStateOut
InPort< OpenHRP::TimedLongSeqSeq > m_servoStateIn
void startBeep(const int _freq, const int _length=50)
ExecutionContextHandle_t UniqueId
boost::shared_ptr< robot > m_robot
bool bindParameter(const char *param_name, VarType &var, const char *def_val, bool(*trans)(VarType &, const char *)=coil::stringTo)
OpenHRP::TimedLongSeqSeq m_servoState
int loadBodyFromModelLoader(::World *world, const char *name, const char *url, CosNaming::NamingContext_var cxt)
OutPort< TimedDoubleSeq > m_qOut
std::map< std::string, hrp::JointLimitTable > joint_limit_tables
unsigned int m_debugLevel
static const char * softerrorlimiter_spec[]
bool is_beep_port_connected
int soft_limit_error_beep_freq
int position_limit_error_beep_freq
InPort< TimedDoubleSeq > m_qCurrentIn
InPort< TimedDoubleSeq > m_qRefIn
TimedDoubleSeq m_qCurrent
bool addPort(PortBase &port)
virtual bool write(DataType &value)
RTC::CorbaPort m_SoftErrorLimiterServicePort
virtual RTC::ReturnCode_t onInitialize()
const std::vector< OutPortConnector *> & connectors()
bool addInPort(const char *name, InPortBase &inport)
virtual RTC::ReturnCode_t onActivated(RTC::UniqueId ec_id)
bool registerFactory(coil::Properties &profile, RtcNewFunc new_func, RtcDeleteFunc delete_func)
void start_beep(int freq, int length)
virtual ~SoftErrorLimiter()
Destructor.
bool registerProvider(const char *instance_name, const char *type_name, PortableServer::RefCountServantBase &provider)