summaryrefslogtreecommitdiff
path: root/digital/io-hub/src/common-cc/asserv.cc
blob: acffda9969ae130467069fe5705abc71dc4d49e0 (plain)
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
// io-hub - Modular Input/Output. {{{
//
// Copyright (C) 2013 Nicolas Schodet
//
// APBTeam:
//        Web: http://apbteam.org/
//      Email: team AT apbteam DOT org
//
// 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, write to the Free Software
// Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
//
// }}}
#include "asserv.hh"

#include "ucoolib/utils/bytes.hh"

Asserv::Asserv (I2cQueue &queue, float scale)
    : I2cQueue::Slave (queue, 0x04, 10 + aux_nb * 2),
      status_flag_ (0),
      input_port_ (0),
      last_moving_direction_ (DIRECTION_NONE),
      scale_ (scale), scale_inv_ (1 / scale)
{
    position_.v.x = 0;
    position_.v.y = 0;
    position_.a = 0;
}

void
Asserv::recv_status (const uint8_t *status)
{
    status_flag_ = status[0];
    input_port_ = status[1];
    position_.v.x = ucoo::bytes_pack (0, status[2], status[3], status[4])
        * scale_;
    position_.v.y = ucoo::bytes_pack (0, status[5], status[6], status[7])
        * scale_;
    position_.a = ucoo::bytes_pack (status[8], status[9]);
    Direction d = get_moving_direction ();
    if (d != DIRECTION_NONE)
        last_moving_direction_ = d;
}

void
Asserv::free ()
{
    uint8_t buf[] = { 'w' };
    send (buf, sizeof (buf));
}

void
Asserv::stop ()
{
    uint8_t buf[] = { 's' };
    send (buf, sizeof (buf));
}

void
Asserv::move_distance (int distance)
{
    distance *= scale_inv_;
    uint8_t buf[] = { 'l',
        ucoo::bytes_unpack (distance, 2),
        ucoo::bytes_unpack (distance, 1),
        ucoo::bytes_unpack (distance, 0),
    };
    send (buf, sizeof (buf));
}

void
Asserv::move_angle (int16_t angle)
{
    uint8_t buf[] = { 'a',
        ucoo::bytes_unpack (angle, 1),
        ucoo::bytes_unpack (angle, 0),
    };
    send (buf, sizeof (buf));
}

void
Asserv::goto_angle (uint16_t angle)
{
    uint8_t buf[] = { 'y',
        ucoo::bytes_unpack (angle, 1),
        ucoo::bytes_unpack (angle, 0),
    };
    send (buf, sizeof (buf));
}

void
Asserv::goto_xya (const Position &pos,
                  DirectionConsign direction_consign)
{
    int x = pos.v.x * scale_inv_;
    int y = pos.v.y * scale_inv_;
    uint8_t buf[] = { 'X',
        ucoo::bytes_unpack (x, 2),
        ucoo::bytes_unpack (x, 1),
        ucoo::bytes_unpack (x, 0),
        ucoo::bytes_unpack (y, 2),
        ucoo::bytes_unpack (y, 1),
        ucoo::bytes_unpack (y, 0),
        ucoo::bytes_unpack (pos.a, 1),
        ucoo::bytes_unpack (pos.a, 0),
        direction_consign,
    };
    send (buf, sizeof (buf));
}

void
Asserv::goto_xy (const vect_t &vect,
                 DirectionConsign direction_consign)
{
    int x = vect.x * scale_inv_;
    int y = vect.y * scale_inv_;
    uint8_t buf[] = { 'x',
        ucoo::bytes_unpack (x, 2),
        ucoo::bytes_unpack (x, 1),
        ucoo::bytes_unpack (x, 0),
        ucoo::bytes_unpack (y, 2),
        ucoo::bytes_unpack (y, 1),
        ucoo::bytes_unpack (y, 0),
        direction_consign,
    };
    send (buf, sizeof (buf));
}

void
Asserv::push_wall (DirectionConsign direction_consign, int init_x, int init_y,
                   int16_t init_a)
{
    if (init_x != -1)
        init_x *= scale_inv_;
    if (init_y != -1)
        init_y *= scale_inv_;
    uint8_t buf[] = { 'G',
        direction_consign,
        ucoo::bytes_unpack (init_x, 2),
        ucoo::bytes_unpack (init_x, 1),
        ucoo::bytes_unpack (init_x, 0),
        ucoo::bytes_unpack (init_y, 2),
        ucoo::bytes_unpack (init_y, 1),
        ucoo::bytes_unpack (init_y, 0),
        ucoo::bytes_unpack (init_a, 1),
        ucoo::bytes_unpack (init_a, 0),
    };
    send (buf, sizeof (buf));
}

void
Asserv::follow (DirectionConsign direction_consign)
{
    uint8_t buf[] = { 'o',
        direction_consign,
    };
    send (buf, sizeof (buf));
}

void
Asserv::follow_update (int16_t consign)
{
    uint8_t buf[] = {
        ucoo::bytes_unpack (consign, 1),
        ucoo::bytes_unpack (consign, 0),
    };
    send (buf, sizeof (buf), I2cQueue::TRANSIENT);
}

void
Asserv::set_speed (uint16_t linear_hi, uint16_t angular_hi, uint16_t
                   linear_lo, uint16_t angular_lo)
{
    uint8_t buf[] = { 'p', 's',
        ucoo::bytes_unpack (linear_hi, 1),
        ucoo::bytes_unpack (linear_hi, 0),
        ucoo::bytes_unpack (angular_hi, 1),
        ucoo::bytes_unpack (angular_hi, 0),
        ucoo::bytes_unpack (linear_lo, 1),
        ucoo::bytes_unpack (linear_lo, 0),
        ucoo::bytes_unpack (angular_lo, 1),
        ucoo::bytes_unpack (angular_lo, 0),
    };
    send (buf, sizeof (buf));
}

void
Asserv::set_acceleration (uint16_t linear, uint16_t angular)
{
    uint8_t buf[] = { 'p', 'a',
        ucoo::bytes_unpack (linear, 1),
        ucoo::bytes_unpack (linear, 0),
        ucoo::bytes_unpack (angular, 1),
        ucoo::bytes_unpack (angular, 0),
    };
    send (buf, sizeof (buf));
}