reb.h
1 /*
2  * Player - One Hell of a Robot Server
3  * Copyright (C) 2000
4  * Brian Gerkey, Kasper Stoy, Richard Vaughan, & Andrew Howard
5  *
6  *
7  * This program is free software; you can redistribute it and/or modify
8  * it under the terms of the GNU General Public License as published by
9  * the Free Software Foundation; either version 2 of the License, or
10  * (at your option) any later version.
11  *
12  * This program is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15  * GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License
18  * along with this program; if not, write to the Free Software
19  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
20  *
21  */
22 
23 /* Copyright (C) 2002
24  * John Sweeney, UMASS, Amherst, Laboratory for Perceptual Robotics
25  *
26  * $Id: reb.h 9120 2013-01-07 00:18:52Z jpgr87 $
27  *
28  * Header for the REB device. This is the K-Team Robotics Extension
29  * Board attached to their Kameleon 376BC. We connect to it via
30  * the serial port of our ADS Bitsy (StrongARM based). SO the
31  * architecture is similar to the P2OS device, in that the position, IR and
32  * power services all need to go through a single serial port and
33  * base device class. So this code was copied from p2osdevice and
34  * modified to taste.
35  *
36  */
37 
38 #ifndef _REBDEVICE_H
39 #define _REBDEVICE_H
40 
41 #include <pthread.h>
42 #include <sys/time.h>
43 #include <errno.h>
44 
45 // for poll
46 #include <replace/replace.h>
47 #include <libplayercore/playercore.h>
48 
49 #include <reb_params.h>
50 
51 
52 #define REB_CONFIG_BUFFER_SIZE 1024
53 #define REB_BAUDRATE B38400
54 #define REB_DEFAULT_SERIAL_PORT "/dev/ttySA1"
55 
56 #define REB_MOTOR_RIGHT 0
57 #define REB_MOTOR_LEFT 2
58 
59 #define REB_BATTERY_CHANNEL 15
60 
61 #define REB_AD_OFF 0
62 #define REB_AD_ON 1
63 
64 #define REB_FIXED_FACTOR 10000
65 
66 #define REB_MAX_ACC 100
67 #define REB_MIN_ACC 10
68 
69 #define REB_POS_MODE_STRAIGHT 0
70 #define REB_POS_MODE_ROTATION 1
71 
72 #define REB_IR_START 1
73 #define REB_IR_STOP 0
74 
75 #define CRLF "\r\n"
76 #define REB_RESTART_COMMAND "restart\r\n"
77 #define REB_COMMAND_PROMPT ":\r\n"
78 
79 #ifndef ABS
80 #define ABS(x) ((x) < 0 ? -(x) : (x))
81 #endif
82 
83 #ifndef SGN
84 #define SGN(x) ((x) < 0 ? -1 : 1)
85 #endif
86 
87 /*
88 typedef struct {
89  player_position_data_t position;
90  player_ir_data_t ir;
91  player_power_data_t power;
92 } __attribute__ ((packed)) player_reb_data_t;
93 
94 typedef struct {
95  player_position_cmd_t position;
96 } __attribute__ ((packed)) player_reb_cmd_t;
97 */
98 
99 
100 class REB : public ThreadedDriver
101 {
102 public:
103 
104  REB(ConfigFile *cf, int section);
105 
106  /* the main thread */
107  virtual void Main();
108 
109  int ProcessMessage(ClientData * client, player_msghdr * hdr, uint8_t * data, uint8_t * resp_data, size_t * resp_len);
110 
111  // we override these, because we will maintain our own subscription count
112  virtual int Subscribe(player_device_id_t id);
113  virtual int Unsubscribe(player_device_id_t id);
114 
115  virtual int MainSetup();
116  virtual void MainQuit();
117 
118  void Restart();
119 
120  void ReadConfig();
121 
122  void SetOdometry(int, int, short);
123 
124  // handle IR
125  void SetIRState(int);
126 
127  void UpdateData(void);
128 
129  void UpdateIRData(player_ir_data_t *);
130  void UpdatePowerData(player_power_data_t *);
131  void UpdatePosData(player_position_data_t *);
132 
133  // the following are all interface functions to the REB
134  // this handles the A/D device which deals with IR for us
135  void ConfigAD(int, int);
136  unsigned short ReadAD(int);
137  void ReadAllIR(uint16_t * ir);
138 
139  // this handles motor control
140  void SetSpeed(int, int );
141  int ReadSpeed(int);
142 
143  void SetPos(int, int);
144 
145  void SetPosCounter(int, int);
146  int ReadPos(int);
147 
148  unsigned char ReadStatus(int, int *, int *);
149  void ConfigPosPID(int, int, int, int);
150  void ConfigSpeedPID(int, int, int, int);
151  void ConfigSpeedProfile(int, int, int);
152 
153 private:
154 
155  int write_serial(char *, int);
156  int read_serial_until(char *, int, char *, int);
157  int write_command(char *buf, int len, int maxsize);
158 
159  player_device_id_t ir_id;
160  player_device_id_t position_id;
161  player_device_id_t power_id;
162 
163  int ir_subscriptions;
164  int position_subscriptions;
165 
166  int param_index; // index in the RobotParams table for this robot
167  int reb_fd; // reb device file descriptor
168 
169  struct timeval last_position; // last position update
170  bool refresh_last_position;
171  int last_lpos, last_rpos;
172  int last_x_f, last_y_f;
173  double last_theta;
174 
175  struct timeval last_pos_update; // time of last pos update
176  struct timeval last_power_update;
177  struct timeval last_ir_update;
178 
179  int pos_update_period;
180 
181  int current_heading;
182  short desired_heading;
183 
184  int ir_sequence;
185  struct timeval last_ir;
186 
187  bool motors_enabled;
188  bool velocity_mode;
189  bool direct_velocity_control;
190 
191  // device used to communicate with reb
192  char reb_serial_port[MAX_FILENAME_SIZE];
193 
194  struct pollfd write_pfd, read_pfd;
195 
196  // holding vars for command processing
197  int ProcessCommand(player_position_cmd_t * poscmd);
198  short last_trans_command, last_rot_command;
199  int leftvel, rightvel;
200  int leftpos, rightpos;
201 };
202 
203 
204 #endif
Class for loading configuration file information.
Definition: configfile.h:196
Generic message header.
Definition: player.h:161
Base class for drivers which oeprate with a thread.
Definition: driver.h:552
Data: voltage (PLAYER_POWER_DATA_STATE)
Definition: player_interfaces.h:291
virtual int MainSetup()
Sets up the resources needed by the driver thread.
Definition: reb.cc:294
Data: ranges (PLAYER_IR_DATA_RANGES)
Definition: player_interfaces.h:2115
virtual void MainQuit()
Cleanup method for driver thread (called when main exits)
Definition: reb.cc:347
Definition: reb.h:100
virtual void Main()
Main method for driver thread.
Definition: reb.cc:408