segway_RMP400.cpp 8.46 KB
Newer Older
Guillem Alenyà's avatar
Guillem Alenyà committed
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
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
#include "segway_RMP400.h"
#include "segway_RMP400_exception.h"
#include <iridrivers/segway_rmp200_exceptions.h>
#include <cmath>

CSegwayRMP400::CSegwayRMP400() :
    status_(rmp400_off),
    forward_displacement_(0.0),
    yaw_displacement_(0.0),
    yaw_rate_ (0.0),
    wheel_radius_(default_wheel_radius_),
    use_correction_wheel_factor_(true)
{
    scan_ftdi_for_segways();
}

CSegwayRMP400::CSegwayRMP400(const std::string serial1, const std::string serial2) :
    status_(rmp400_off),
    forward_displacement_(0.0),
    yaw_displacement_(0.0),
    yaw_rate_ (0.0),
    wheel_radius_(default_wheel_radius_),
    use_correction_wheel_factor_(true)
{
    if (serial1 == serial2)
        throw RMP400IdemSerialException(_HERE_, serial1);

    scan_ftdi_for_segways();

    for (int i = 0; i < serial_ftdi_segway_devices_.size(); i++) {
        std::string serial = serial_ftdi_segway_devices_[i];

        if ((serial != serial1) && (serial != serial2))
            throw RMP400NotFoundSerialException(_HERE_, serial);
    }
}

void
CSegwayRMP400::scan_ftdi_for_segways()
{
    CFTDIServer * ftdi_server_ = CFTDIServer::instance();
    ftdi_server_->add_custom_PID(CSegwayRMP200::pid);

    std::vector<int> ftdi_devs = ftdi_server_->get_ids_by_description(CSegwayRMP200::description);

    for (int i = 0;i < ftdi_devs.size();i++) {
        serial_ftdi_segway_devices_.push_back(
                  ftdi_server_->get_serial_number(ftdi_devs.at(i)));
    }

    if (serial_ftdi_segway_devices_.size() != NUM_SEGWAY_200)
        throw RMP400NotValidNumberRMP200Exception(_HERE_, serial_ftdi_segway_devices_.size());
}

void
CSegwayRMP400::start()
{
    for (int i = 0;i < NUM_SEGWAY_200 ;i++) {

        try
        {
            CSegwayRMP200 * segway = new CSegwayRMP200(serial_ftdi_segway_devices_[i]);
            segways_.push_back(segway);
        }
        catch (CSegwayRMP200Exception & e)
        {
            // Something failed while starting engines
            // clean up previous (if any) started engine
            for (int i = 0; i < segways_.size(); i++) {
                segways_[i]->stop();
                delete segways_[i];
            }

            status_ = rmp400_off;
            throw;
        }
    }

    status_ = rmp400_connected;
}

std::ostream &
operator<< (std::ostream& data, CSegwayRMP400 & segway)
{
  for (int i=0; i<segway.segways_.size(); i++)
  {
    data << "Segway unit: "              << i << std::endl;
    data << "Pitch angle: "              << segway.segways_[i]->get_pitch_angle() << " degrees" << std::endl;
    data << "Pitch rate "                << segway.segways_[i]->get_pitch_rate() << " degrees/s" << std::endl;
    data << "Roll angle: "               << segway.segways_[i]->get_roll_angle() << " degrees" << std::endl;
    data << "Roll rate: "                << segway.segways_[i]->get_roll_rate() << " degrees/s" << std::endl;
    data << "Left wheel velocity: "      << segway.segways_[i]->get_left_wheel_velocity() << " m/s" << std::endl;
    data << "Right wheel velocity: "     << segway.segways_[i]->get_right_wheel_velocity() << " m/s" << std::endl;
    data << "Yaw rate: "                 << segway.segways_[i]->get_yaw_rate() << " degrees/s" << std::endl;
    data << "Servo frames: "             << segway.segways_[i]->get_servo_frames() << " frames/s" << std::endl;
    data << "Left wheel displacement: "  << segway.segways_[i]->get_left_wheel_displacement() << " m" << std::endl;
    data << "Right wheel displacement: " << segway.segways_[i]->get_right_wheel_displacement() << " m" << std::endl;
    data << "Forward displacement: "     << segway.segways_[i]->get_forward_displacement() << " m" << std::endl;
    data << "Yaw displacement: "         << segway.segways_[i]->get_yaw_displacement() << " rev" << std::endl;
    data << "Left motor torque: "        << segway.segways_[i]->get_left_motor_torque() << " Nm" << std::endl;
    data << "Right motor torque: "       << segway.segways_[i]->get_right_motor_torque() << " Nm" << std::endl;
    if(segway.segways_[i]->get_operation_mode()==tractor)
      data << "Operation mode: tractor" << std::endl;
    else
      data << "Operation mode: power down" << std::endl;
    if(segway.segways_[i]->get_gain_schedule()==light)
      data << "Gain schedule: light" << std::endl;
    else if(segway.segways_[i]->get_gain_schedule()==tall)
      data << "Gain schedule: tall" << std::endl;
    else
      data << "Gain schedule: heavy" << std::endl;
    data << "UI battery voltage: "        << segway.segways_[i]->get_ui_battery_voltage() << " V" << std::endl;
    data << "Powerbase battery voltage: " << segway.segways_[i]->get_powerbase_battery_voltage() << " V" << std::endl << std::endl;
  }
    data << std::endl;
}

void
CSegwayRMP400::set_operation_mode(op_mode mode)
{
    if (mode == balance)
        throw RMP400InvalidBalanceModeOperation(_HERE_);

    for (int i=0;i < segways_.size();i++)
    {
      segways_[i]->set_operation_mode(mode);
      usleep(10000);
    }
}

void
CSegwayRMP400::need_to_be_connected() const
{
    if (! is_connected())
        throw RMP400NoConnected(_HERE_);
}

void
CSegwayRMP400::disable_corretion_wheel_factor()
{
    use_correction_wheel_factor_ = false;
}

void
CSegwayRMP400::fix_with_wheel_factor(float & value)
{
   /* corretion factor is the diference between real wheel and supposed
    * firmware wheel (probaby segway using the rmp200 value) */
    value = value * (wheel_radius_ / RMP200_DEFAULT_WHEEL_RADIUS);
}

void
CSegwayRMP400::set_wheel_radius(const float radius)
{
    wheel_radius_ = radius;
}

float
CSegwayRMP400::get_forward_displacement()
{
    need_to_be_connected();

    // The get_forward_displacement function in segway RMP 200 doesn't
    // return the accumulate value of displacement, wheel_displacement
    // function do. Use them instead.

    double fwd_displ_segway0 = (segways_[0]->get_right_wheel_displacement() +
                               segways_[0]->get_left_wheel_displacement()) / 2;
    double fwd_displ_segway1 = (segways_[1]->get_right_wheel_displacement() +
                               segways_[1]->get_left_wheel_displacement()) / 2;

    forward_displacement_ = (fwd_displ_segway0 + fwd_displ_segway1) / 2;

    if (use_correction_wheel_factor_)
        fix_with_wheel_factor(forward_displacement_);

    return forward_displacement_;
}

float
CSegwayRMP400::get_yaw_displacement()
{
    need_to_be_connected();

    float  yaw_displacement_mts = (segways_[0]->get_yaw_displacement() +
                                   segways_[1]->get_yaw_displacement()) / 2;

    if (use_correction_wheel_factor_)
        fix_with_wheel_factor(yaw_displacement_mts);

    yaw_displacement_ = yaw_displacement_mts * 2 * PI; // convert to radians
    return yaw_displacement_;
}

float
CSegwayRMP400::get_yaw_rate()
{
    need_to_be_connected();

    yaw_rate_ = get_yaw_displacement() * (PI / 180);
    return yaw_rate_;
}

void
CSegwayRMP400::reset_integrators()
{
    need_to_be_connected();

    do {
      for (int i=0; i < segways_.size(); i++) {
          segways_[i]->reset_right_wheel_integrator();
          segways_[i]->reset_left_wheel_integrator();
          segways_[i]->reset_yaw_integrator();
          segways_[i]->reset_forward_integrator();
      }
      // Need to leave the segwayrmp 200 driver to read need values
      sleep(1);
    } while (! is_valid_reset_integrators());
}

bool
CSegwayRMP400::is_valid_reset_integrators()
{
    float fd = get_forward_displacement();
    float yd = get_yaw_displacement();

    if (std::fabs(0.0 - fd) > 0.1) {
        std::cerr << "[segway] Invalid forward reset value "  << fd << std::endl;
        return false;
    }

    if (std::fabs(0.0 - yd) > 0.1) {
        std::cerr << "[segway] Invalid yaw reset value "  << yd << std::endl;
        return false;
    }

    return true;
}

void
CSegwayRMP400::move(float vT, float vR)
{
    need_to_be_connected();

    float vRradsec = vR * 2.0 * PI;  // revs per sec to rads per sec

    if (fabs(vT) < fabs(RMP400_b * vRradsec / 2)) {
        // minimum translational velocity needed to enforce rotation about a ICR outside of the robot base
        if(vT>=0.0)
            vT =  fabs(RMP400_b * vRradsec / 2);
        else
            vT = -fabs(RMP400_b * vRradsec / 2);
    }

    for (int i=0 ;i < segways_.size(); i++)
        segways_[i]->move(vT,vR);
}

void
CSegwayRMP400::stop()
{
    need_to_be_connected();

    for (int i=0;i < segways_.size();i++)
        segways_[i]->stop();

    status_ = rmp400_off;
}

CSegwayRMP400::~CSegwayRMP400()
{
    for (int i=0;i < segways_.size(); i++) 
        delete segways_[i];
}