~shadowrobot/sr-ros-interface-ethercat/tactiles_software_version

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
/**
 * @file   motor_data_checker.cpp
 * @author toni <toni@shadowrobot.com>
 * @date   25 Oct 2011
 *
 * Copyright 2011 Shadow Robot Company Ltd.
 *
 * This program is free software: you can redistribute it and/or modify it
 * under the terms of the GNU General Public License as published by the Free
 * Software Foundation, either version 2 of the License, or (at your option)
 * any later version.
 *
 * This program is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for
 * more details.
 *
 * You should have received a copy of the GNU General Public License along
 * with this program.  If not, see <http://www.gnu.org/licenses/>.
 *
 *
 * @brief This is a class to check that all expected initialization data have been received from each motor.
 *
 *
 */

#include "sr_robot_lib/motor_data_checker.hpp"

namespace generic_updater
{
  const double MotorDataChecker::timeout = 5.0;

  MotorDataChecker::MotorDataChecker(boost::ptr_vector<shadow_joints::Joint> joints_vector,
                                     std::vector<UpdateConfig> initialization_configs_vector)
      : nh_tilde("~"), update_state(operation_mode::device_update_state::INITIALIZATION), init_max_duration(timeout)
  {
    init(joints_vector, initialization_configs_vector);
  }

  MotorDataChecker::~MotorDataChecker()
  {
    for(unsigned int i=0; i<msg_checkers_.size();i++)
    {
      for(unsigned int j=0; j<msg_checkers_.at(i).msg_from_motor_checkers.size();j++)
      {
        delete msg_checkers_.at(i).msg_from_motor_checkers.at(j);
      }
    }
  }

  void MotorDataChecker::init(boost::ptr_vector<shadow_joints::Joint> joints_vector,
                              std::vector<UpdateConfig> initialization_configs_vector)
  {
    //Create a one-shot timer
    check_timeout_timer = nh_tilde.createTimer(init_max_duration,
                                               boost::bind(&MotorDataChecker::timer_callback, this, _1), true);
    update_state = operation_mode::device_update_state::INITIALIZATION;
    msg_checkers_.clear();

    std::vector<UpdateConfig>::iterator msg_it;

    for (msg_it = initialization_configs_vector.begin(); msg_it < initialization_configs_vector.end(); msg_it++)
    {
      MessageChecker tmp_msg_checker( static_cast<FROM_MOTOR_DATA_TYPE>(msg_it->what_to_update) );
      boost::ptr_vector<shadow_joints::Joint>::iterator joint;
      for (joint = joints_vector.begin(); joint < joints_vector.end(); joint++)
      {
        if (joint->has_motor)
        {
          if (msg_it->what_to_update == MOTOR_DATA_SLOW_MISC)
          {
            tmp_msg_checker.msg_from_motor_checkers.push_back(new SlowMessageFromMotorChecker(joint->motor->motor_id));
          }
          else
          {
            tmp_msg_checker.msg_from_motor_checkers.push_back(new MessageFromMotorChecker(joint->motor->motor_id));
          }
        }
      }
      msg_checkers_.push_back(tmp_msg_checker);
    }
  }

  bool MotorDataChecker::check_message(boost::ptr_vector<shadow_joints::Joint>::iterator joint_tmp,
                                       FROM_MOTOR_DATA_TYPE motor_data_type, int16u motor_slow_data_type)
  {
    int index_motor_data_type = 0;

    index_motor_data_type = find(motor_data_type);
    if (index_motor_data_type != (-1))
    {
      int index_motor_id = 0;
      index_motor_id = msg_checkers_.at(index_motor_data_type).find(joint_tmp->motor->motor_id);
      if (index_motor_id != (-1))
      {
        if (motor_data_type == MOTOR_DATA_SLOW_MISC)
        {
          SlowMessageFromMotorChecker* ptr_tmp_checker = dynamic_cast<SlowMessageFromMotorChecker*>( msg_checkers_.at(index_motor_data_type).msg_from_motor_checkers.at(index_motor_id) );

          if(ptr_tmp_checker != NULL)
          {
            ptr_tmp_checker->set_received( static_cast<FROM_MOTOR_SLOW_DATA_TYPE>(motor_slow_data_type) );
          }
          else
          {
            ROS_ERROR_STREAM("Checker conversion failed");
          }
        }
        else
        {
          msg_checkers_.at(index_motor_data_type).msg_from_motor_checkers.at(index_motor_id)->set_received();
        }
      }
      else
      {
        ROS_ERROR_STREAM("Motor id not found: " << joint_tmp->motor->motor_id );
      }
    }
    return ((update_state == operation_mode::device_update_state::OPERATION) || is_everything_checked());
  }

  bool MotorDataChecker::is_everything_checked()
  {
    std::vector<MessageChecker>::iterator it;

    for (it = msg_checkers_.begin(); it < msg_checkers_.end(); it++)
    {
      std::vector<MessageFromMotorChecker*>::iterator it2;

      for (it2 = it->msg_from_motor_checkers.begin(); it2 < it->msg_from_motor_checkers.end(); it2++)
      {
        if (!(*it2)->get_received())
        {
          return false;
        }
      }
    }

    //all the motors are initialized -> we stop the timeout timer
    check_timeout_timer.stop();
    update_state = operation_mode::device_update_state::OPERATION;
    return true;
  }

  int MotorDataChecker::find(FROM_MOTOR_DATA_TYPE motor_data_type)
  {
    for(unsigned int i=0;i<msg_checkers_.size();i++)
    {
      if (msg_checkers_.at(i).msg_type == motor_data_type)
        return i;
    }
    return (-1);
  }

  void MotorDataChecker::timer_callback(const ros::TimerEvent& event)
  {
    if( update_state == operation_mode::device_update_state::INITIALIZATION )
    {
      update_state = operation_mode::device_update_state::OPERATION;
      ROS_ERROR_STREAM("Motor Initialization Timeout: the static information in the diagnostics may not be uptodate.");
    }
  }

  int MessageChecker::find(int motor_id)
  {
    for (unsigned int i=0; i< msg_from_motor_checkers.size(); i++)
    {
      if (msg_from_motor_checkers.at(i)->motor_id_ == motor_id)
        return i;
    }
    return (-1);
  }

  SlowMessageFromMotorChecker::SlowMessageFromMotorChecker(int id)
      : MessageFromMotorChecker(id)
  {
    for (int i = 0; i <= MOTOR_SLOW_DATA_LAST; i++)
    {
      slow_data_received.at(i) = false;
    }
  }

  void SlowMessageFromMotorChecker::set_received(FROM_MOTOR_SLOW_DATA_TYPE slow_data_type)
  {
    if (received_ == false)
    {
      //Check the slow data type as received
      if ( slow_data_type > MOTOR_SLOW_DATA_LAST )
      {
        ROS_ERROR_STREAM("Received bad slow_data_type: " << slow_data_type << " > " << slow_data_received.size() );
        return;
      }
      slow_data_received.at(slow_data_type) = true;

      //look if every type is received, then change FROM_MOTOR_SLOW_DATA_TYPE general received state accordingly
      bool checked = true;
      for (int i = MOTOR_SLOW_DATA_SVN_REVISION; i <= MOTOR_SLOW_DATA_LAST; i++)
      {
        checked = checked && slow_data_received.at(i);
        if (!checked)
        {
          break;
        }
      }
      if (checked)
        received_ = true;
    }
  }

  void MessageFromMotorChecker::set_received()
  {
    received_ = true;
  }

  bool MessageFromMotorChecker::get_received()
  {
    return received_;
  }

}

/* For the emacs weenies in the crowd.
 Local Variables:
 c-basic-offset: 2
 End:
 */