wg05.cpp
Go to the documentation of this file.
1 /*********************************************************************
2  * Software License Agreement (BSD License)
3  *
4  * Copyright (c) 2008, Willow Garage, Inc.
5  * All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  *
11  * * Redistributions of source code must retain the above copyright
12  * notice, this list of conditions and the following disclaimer.
13  * * Redistributions in binary form must reproduce the above
14  * copyright notice, this list of conditions and the following
15  * disclaimer in the documentation and/or other materials provided
16  * with the distribution.
17  * * Neither the name of the Willow Garage nor the names of its
18  * contributors may be used to endorse or promote products derived
19  * from this software without specific prior written permission.
20  *
21  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
24  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
25  * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
26  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
27  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
28  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
29  * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
31  * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
32  * POSSIBILITY OF SUCH DAMAGE.
33  *********************************************************************/
34 
35 #include <iomanip>
36 
37 #include <math.h>
38 #include <stddef.h>
39 
40 #include <ethercat_hardware/wg05.h>
41 
42 #include <dll/ethercat_dll.h>
43 #include <al/ethercat_AL.h>
44 #include <dll/ethercat_device_addressed_telegram.h>
45 #include <dll/ethercat_frame.h>
46 
47 #include <boost/crc.hpp>
48 #include <boost/static_assert.hpp>
49 
51 
52 
53 void WG05::construct(EtherCAT_SlaveHandler *sh, int &start_address)
54 {
55  WG0X::construct(sh, start_address);
56 
57  unsigned int base_status = sizeof(WG0XStatus);
58 
59  // As good a place as any for making sure that compiler actually packed these structures correctly
60  BOOST_STATIC_ASSERT(sizeof(WG0XStatus) == WG0XStatus::SIZE);
61 
62  command_size_ = sizeof(WG0XCommand);
63  status_size_ = sizeof(WG0XStatus);
64 
65 
66  EtherCAT_FMMU_Config *fmmu = new EtherCAT_FMMU_Config(2);
67  //ROS_DEBUG("device %d, command 0x%X = 0x10000+%d", (int)sh->get_ring_position(), start_address, start_address-0x10000);
68  (*fmmu)[0] = EC_FMMU(start_address, // Logical start address
69  command_size_,// Logical length
70  0x00, // Logical StartBit
71  0x07, // Logical EndBit
72  COMMAND_PHY_ADDR, // Physical Start address
73  0x00, // Physical StartBit
74  false, // Read Enable
75  true, // Write Enable
76  true); // Enable
77 
78  start_address += command_size_;
79 
80  //ROS_DEBUG("device %d, status 0x%X = 0x10000+%d", (int)sh->get_ring_position(), start_address, start_address-0x10000);
81  (*fmmu)[1] = EC_FMMU(start_address, // Logical start address
82  base_status, // Logical length
83  0x00, // Logical StartBit
84  0x07, // Logical EndBit
85  STATUS_PHY_ADDR, // Physical Start address
86  0x00, // Physical StartBit
87  true, // Read Enable
88  false, // Write Enable
89  true); // Enable
90 
91  start_address += base_status;
92 
93  sh->set_fmmu_config(fmmu);
94 
95  EtherCAT_PD_Config *pd = new EtherCAT_PD_Config(4);
96 
97  // Sync managers
98  (*pd)[0] = EC_SyncMan(COMMAND_PHY_ADDR, command_size_, EC_BUFFERED, EC_WRITTEN_FROM_MASTER);
99  (*pd)[0].ChannelEnable = true;
100  (*pd)[0].ALEventEnable = true;
101 
102  (*pd)[1] = EC_SyncMan(STATUS_PHY_ADDR, base_status);
103  (*pd)[1].ChannelEnable = true;
104 
105  (*pd)[2] = EC_SyncMan(WGMailbox::MBX_COMMAND_PHY_ADDR, WGMailbox::MBX_COMMAND_SIZE, EC_QUEUED, EC_WRITTEN_FROM_MASTER);
106  (*pd)[2].ChannelEnable = true;
107  (*pd)[2].ALEventEnable = true;
108 
109  (*pd)[3] = EC_SyncMan(WGMailbox::MBX_STATUS_PHY_ADDR, WGMailbox::MBX_STATUS_SIZE, EC_QUEUED);
110  (*pd)[3].ChannelEnable = true;
111 
112  sh->set_pd_config(pd);
113 }
114 
115 
117 {
118  if ((fw_major_ == 1) && (fw_minor_ >= 21))
119  {
121  }
122 
123  int retval = WG0X::initialize(hw, allow_unprogrammed);
124 
125  EthercatDirectCom com(EtherCAT_DataLinkLayer::instance());
126 
127  // Determine if device supports application RAM
128  if (!retval)
129  {
130  if (use_ros_)
131  {
132  // WG005B has very poor motor voltage measurement, don't use meaurement for dectecting problems.
133  bool poor_measured_motor_voltage = (board_major_ <= 2);
134  double max_pwm_ratio = double(0x3C00) / double(PWM_MAX);
135  double board_resistance = 0.8;
136  if (!WG0X::initializeMotorModel(hw, "WG005", max_pwm_ratio, board_resistance, poor_measured_motor_voltage))
137  {
138  ROS_FATAL("Initializing motor trace failed");
139  sleep(1); // wait for ros to flush rosconsole output
140  return -1;
141  }
142  }
143 
144  }// end if !retval
145  return retval;
146 }
147 
148 void WG05::packCommand(unsigned char *buffer, bool halt, bool reset)
149 {
150  WG0X::packCommand(buffer, halt, reset);
151 }
152 
153 bool WG05::unpackState(unsigned char *this_buffer, unsigned char *prev_buffer)
154 {
155  bool rv = true;
156 
157  unsigned char* this_status = this_buffer + command_size_;
158  if (!verifyChecksum(this_status, status_size_))
159  {
160  status_checksum_error_ = true;
161  rv = false;
162  goto end;
163  }
164 
165  if (!WG0X::unpackState(this_buffer, prev_buffer))
166  {
167  rv = false;
168  }
169 
170  end:
171  return rv;
172 }
173 
174 
#define ROS_FATAL(...)
unsigned int command_size_
void packCommand(unsigned char *buffer, bool halt, bool reset)
Definition: wg0x.cpp:562
static const unsigned SIZE
Definition: wg0x.h:183
EtherCAT driver for WG005 motor controller.
Definition: wg05.h:43
uint8_t board_major_
Printed circuit board revision (for this value 0==&#39;A&#39;, 1==&#39;B&#39;)
Definition: wg0x.h:260
bool status_checksum_error_
Definition: wg0x.h:298
static const unsigned STATUS_PHY_ADDR
Definition: wg0x.h:357
void packCommand(unsigned char *buffer, bool halt, bool reset)
Definition: wg05.cpp:148
bool unpackState(unsigned char *this_buffer, unsigned char *prev_buffer)
Definition: wg0x.cpp:613
static const int PWM_MAX
Definition: wg0x.h:347
PLUGINLIB_EXPORT_CLASS(WG05, EthercatDevice)
bool unpackState(unsigned char *this_buffer, unsigned char *prev_buffer)
Definition: wg05.cpp:153
int initialize(pr2_hardware_interface::HardwareInterface *, bool allow_unprogrammed=true)
Definition: wg05.cpp:116
uint8_t fw_minor_
Definition: wg0x.h:259
AppRamStatus app_ram_status_
Definition: wg0x.h:324
void construct(EtherCAT_SlaveHandler *sh, int &start_address)
< Construct EtherCAT device
Definition: wg0x.cpp:197
bool initializeMotorModel(pr2_hardware_interface::HardwareInterface *hw, const string &device_description, double max_pwm_ratio, double board_resistance, bool poor_measured_motor_voltage)
Allocates and initialized motor trace for WG0X devices than use it (WG006, WG005) ...
Definition: wg0x.cpp:237
unsigned int status_size_
uint8_t fw_major_
Definition: wg0x.h:258
bool verifyChecksum(const void *buffer, unsigned size)
Definition: wg0x.cpp:664
static const unsigned COMMAND_PHY_ADDR
Definition: wg0x.h:356
void construct(EtherCAT_SlaveHandler *sh, int &start_address)
< Construct EtherCAT device
Definition: wg05.cpp:53
virtual int initialize(pr2_hardware_interface::HardwareInterface *, bool allow_unprogrammed=true)
Definition: wg0x.cpp:394


ethercat_hardware
Author(s): Rob Wheeler , Derek King
autogenerated on Fri Mar 15 2019 02:53:29