plugin.cpp
Go to the documentation of this file.
1 /*
2  * Copyright 2017 Fraunhofer Institute for Manufacturing Engineering and Automation (IPA)
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9 
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 
18 #include <canopen_402/motor.h>
21 
22 
23 namespace cob_elmo_homing {
24 
26 
29 
30  static const uint64_t byte_3_bit_6 = (1 << (3*8+6));;
31  static const uint64_t compare_mask = 0xFFFFFFFF ^ byte_3_bit_6;
32 
33  int32_t offset_;
34  int32_t event_;
35  int32_t speed_;
36  uint32_t timeout_;
37 
38  uint64_t readResponse(uint64_t command) {
39  boost::this_thread::sleep_for(boost::chrono::milliseconds(10));
40  uint64_t response = response_entry_.get();
41  return (response & compare_mask) == (command & compare_mask) ? response : 0;
42  }
43 
44  bool set(char c1, char c2, uint16_t index, uint32_t val){
45  uint64_t response = 0;
46  uint64_t command = c1 | (c2 << (1*8)) | ((index & 0x3FFF) << (2*8)) | static_cast<uint64_t>(val) << (4*8);
47  command_entry_.set(command);
48 
49  canopen::time_point timeout = canopen::get_abs_time(boost::chrono::seconds(1));
50  do{
51  response = readResponse(command);
52  }while(response != command && (response & byte_3_bit_6) == 0 && canopen::get_abs_time() < timeout);
53 
54  bool ok = response == command && (response & byte_3_bit_6) == 0;
55  return ok;
56  }
57  template<typename T> bool test(char c1, char c2, uint16_t index, uint32_t val, T dur){
58  uint64_t response = 0;
59  uint64_t command = c1 | (c2 << (1*8)) | ((index & 0x3FFF) << (2*8)) | byte_3_bit_6;
60 
62  do{
63  command_entry_.set(command);
64  response = readResponse(command);
65  }while( (response >> (4*8)) != val && canopen::get_abs_time() < timeout);
66 
67  return (response & byte_3_bit_6) == 0 && (response >> (4*8)) == val;
68  }
69 
70 public:
71  ElmoMotor402(const std::string &name, canopen::ObjectStorageSharedPtr storage, const canopen::Settings &settings)
72  : Motor402(name, storage, settings)
73  {
74  storage->entry(command_entry_, 0x2012);
75  storage->entry(response_entry_, 0x2013);
76  event_ = settings.get_optional<int32_t>("homing_event", -1);
77  speed_ = settings.get_optional<int32_t>("homing_speed", 50000);
78  offset_ = settings.get_optional<int32_t>("homing_offset", 0);
79  timeout_= settings.get_optional<uint32_t>("homing_timeout", 60);
80  }
81 
83  Motor402::handleInit(status);
84  if(status.bounded<canopen::LayerStatus::Ok>() && event_ >= 0){
85  if(!command_entry_.valid()){
86  status.error("Command entry is not valid");
87  }else if(!response_entry_.valid()){
88  status.error("Response entry is not valid");
90  status.error("Could not switch mode");
91  }else{
92  if(!set('H','M', 1, 0) || // reset homing
93  !set('H','M', 2, offset_) ||
94  !set('H','M', 3, event_) ||
95  !set('H','M', 4, 2) || // do not stop after homing
96  !set('H','M', 5, 0) || // set PX to offset
97  !set('H','M', 1, 1) || // start homing
98  !setTarget(speed_)){
99  status.error("could not initialize homing");
100  return;
101  }
102 
103  if(!test('H','M', 1, 0, boost::chrono::seconds(timeout_))) status.error("homing timeout");
104 
105  if(!setTarget(0)) status.error("could not stop motor");
106  if(!set('H','M', 1, 0)) status.error("could not stop homing");
107  }
108  }
109  }
110 
112  public:
113  virtual canopen::MotorBaseSharedPtr allocate(const std::string& name,
115  const canopen::Settings& settings)
116  {
117  return ROSCANOPEN_MAKE_SHARED<ElmoMotor402>(name, storage, settings);
118  }
119  };
120 };
121 
122 }
123 
canopen::ObjectStorage::Entry< uint64_t > response_entry_
Definition: plugin.cpp:28
uint64_t readResponse(uint64_t command)
Definition: plugin.cpp:38
static const uint64_t byte_3_bit_6
Definition: plugin.cpp:30
virtual bool enterModeAndWait(uint16_t mode)
boost::chrono::high_resolution_clock::time_point time_point
ROSLIB_DECL std::string command(const std::string &cmd)
void handleInit(canopen::LayerStatus &status)
Definition: plugin.cpp:82
virtual bool setTarget(double val)
bool test(char c1, char c2, uint16_t index, uint32_t val, T dur)
Definition: plugin.cpp:57
const std::string name
canopen::ObjectStorage::Entry< uint64_t > command_entry_
Definition: plugin.cpp:27
Motor402(const std::string &name, ObjectStorageSharedPtr storage, const canopen::Settings &settings)
const void error(const std::string &r)
T get_optional(const std::string &n, const T &def) const
virtual canopen::MotorBaseSharedPtr allocate(const std::string &name, canopen::ObjectStorageSharedPtr storage, const canopen::Settings &settings)
Definition: plugin.cpp:113
static const uint64_t compare_mask
Definition: plugin.cpp:31
time_point get_abs_time(const time_duration &timeout)
#define PLUGINLIB_EXPORT_CLASS(class_type, base_class_type)
ElmoMotor402(const std::string &name, canopen::ObjectStorageSharedPtr storage, const canopen::Settings &settings)
Definition: plugin.cpp:71
bool bounded() const


cob_elmo_homing
Author(s): Mathias Lüdtke
autogenerated on Wed Apr 7 2021 02:11:38