forked from srmq/MobileSim
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathEmulatePioneer.hh
More file actions
742 lines (622 loc) · 24.9 KB
/
EmulatePioneer.hh
File metadata and controls
742 lines (622 loc) · 24.9 KB
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
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
/*
* Pioneer emulator over TCP.
* Copyright (C) 2005, ActivMedia Robotics
* Copyright (C) 2006-2010 MobileRobots, Inc.
* Copyright (C) 2011-2015 Adept Technology
* Copyright (C) 2016-2017 Omron Adept Technologies
*
* 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
*
*/
#ifndef EP_EMULATE_PIONEER_HH_
#define EP_EMULATE_PIONEER_HH_
#include "RobotInterface.hh"
#include "MapLoader.hh"
#include <assert.h>
//#include <pthread.h>
#include <string>
#include <set>
#include <list>
#include "ariaUtil.h"
#include "ArSocket.h"
#include "ArFunctor.h"
#include "ArRobotPacket.h"
#include "ArTcpConnection.h"
#include "ClientPacketReceiver.h"
#include "ArRobotPacketSender.h"
class ArDeviceConnection;
/* Constants required for compatability, shouldn't change these */
#define DEFAULT_PIONEER_SIM_PORT 8101
#define ROBOT_IDENTIFIER_LEN 20 // Max length of robot type, subtype and name strings.
#define MAX_PACKET_PAYLOAD_SIZE 200 // Ignore or truncate packets which claim to have more bytes of data than this
/* What version of ARCOS to claim we are in the CONFIG packet */
#define FIRMWARE_VER_MAJ 'S'
#define FIRMWARE_VER_MIN '0'
/* Defauts for settings in the config file: */
#define DEFAULT_SIP_FREQ 100 // defaut SIP cycle time, ms.
#define DEFAULT_WATCHDOG_TIME 2000 // ms of no activity to stop robot.
#define DEFAULT_MAX_SONAR_READINGS_PER_SIP 32 // Set to a low number like 4 to crudely simulate sonar timing delay
#define DEFAULT_ROBOT_NAME "MobileSim"
#define DEFAULT_ROBOT_CLASS "Pioneer"
#define DEFAULT_ROBOT_SUBCLASS "p3dx"
#define DEFAULT_DIFFCONV 0.0056
#define DEFAULT_ANGLECONV 0.001534
#define DEFAULT_DISTCONV 0.485
#define DEFAULT_VELCONV 1.0
#define DEFAULT_RANGECONV 1.0
#define DEFAULT_VEL2DIV 20.0
#define DEFAULT_STALL_ON_ROT false
#define DEFAULT_BATTERY_TYPE 0
#define DEFAULT_BATTERY_VOLTAGE 13.0
#define DEFAULT_STATE_OF_CHARGE 100
#define DEFAULT_DIGIN_STATE 0xFF
#define DEFAULT_DIGOUT_STATE 0xFF
/* These are values clients can set during a session, but not saved between. */
#define DEFAULT_TRANSVELMAX 3000
#define DEFAULT_TRANSACCEL 300
#define DEFAULT_TRANSDECEL 300
#define DEFAULT_ROTVELMAX 150
#define DEFAULT_ROTACCEL 100
#define DEFAULT_ROTDECEL 100
#define DEFAULT_LATVELMAX 3000
#define DEFAULT_LATACCEL 300
#define DEFAULT_LATDECEL 300
#define DEFAULT_BUMPSTALL_FRONT false
#define DEFAULT_BUMPSTALL_REAR false
/* We don't have any "Top" limits like ARCOS does, you can
* set any acceleration you want. But we need to tell
* the client something, so use these values.
*/
#define FAKE_TRANSVELTOP 9999
#define FAKE_ROTVELTOP 9999
#define FAKE_ROTACCTOP 9999
#define FAKE_TRANSACCTOP 9999
#define FAKE_LATVELTOP 9999
#define FAKE_LATACCTOP 9999
/* Operation codes used in the SIM_CTRL command */
enum {
SIM_CTRL_LOAD_MAP = 1,
SIM_CTRL_MASTER_LOAD_MAP = 2,
SIM_CTRL_CLEAR_MASTER_MAP = 3,
SIM_CTRL_ROTATE_LOGFILES = 4,
SIM_CTRL_LOG_STATE = 5,
SIM_CTRL_SIM_INFO = 6,
SIM_CTRL_SET_GHOST = 7,
SIM_CTRL_SNAPSHOT = 8
};
/** Robot parameters. There are read from the
* Stage (or other) configuration. Clients cannot change these at runtime.
* @todo Read more params from model definition in Stage world. Need mechanism
* in Stage worldfile for adding arbitrary properties to models.
*/
class RobotParams {
public:
char RobotName[ROBOT_IDENTIFIER_LEN]; ///< Note, this is used in the MobileSim UI, but the name is always reported to clients as "MobileSim".
char RobotClass[ROBOT_IDENTIFIER_LEN];
char RobotSubclass[ROBOT_IDENTIFIER_LEN];
int SIPFreq;
int WatchdogTime;
double DistConvFactor;
double DiffConvFactor;
double AngleConvFactor;
double RangeConvFactor;
double Vel2DivFactor;
double VelConvFactor;
int RotVelMax;
int RotAccel;
int RotDecel;
int TransVelMax;
int TransAccel;
int TransDecel;
int LatVelMax;
int LatAccel;
int LatDecel;
// Note, we don't have any "Top" limits like ARCOS does. You can set the
// vels and accels to any value you want.
/** Set this to a low number for a crude stand-in for sonar timing delay: */
int Sim_MaxSonarReadingsPerSIP;
/** Normally motor stall flag is only set if trying to drive forward and
* backwards. Set this to true if stall flag should be set even if
* there is no translational velocity; however this could make it hard
* for robots to extract themselves from a collision by rotating.
* (Real robots usually have enough power to scrape out of a collision
* through rotation, their motors will only stall on a direct hit.)
*/
bool Sim_StallOnRot;
/** What type of batteries the robot has.
As of Nov. 2011/ARCOS 3 Battery Type 2 means nickel with State of Charge, all others are either lead or act the same.
*/
int BatteryType;
int GPSPosX, GPSPosY; // GPS mounting position, robot local coordinate system, mm
RobotParams() :
SIPFreq(DEFAULT_SIP_FREQ),
WatchdogTime(DEFAULT_WATCHDOG_TIME),
DistConvFactor(DEFAULT_DISTCONV),
DiffConvFactor(DEFAULT_DIFFCONV),
AngleConvFactor(DEFAULT_ANGLECONV),
RangeConvFactor(DEFAULT_RANGECONV),
Vel2DivFactor(DEFAULT_VEL2DIV),
VelConvFactor(DEFAULT_VELCONV),
RotVelMax(DEFAULT_ROTVELMAX),
RotAccel(DEFAULT_ROTACCEL),
RotDecel(DEFAULT_ROTDECEL),
TransVelMax(DEFAULT_TRANSVELMAX),
TransAccel(DEFAULT_TRANSACCEL),
TransDecel(DEFAULT_TRANSDECEL),
LatVelMax(DEFAULT_LATVELMAX),
LatAccel(DEFAULT_LATACCEL),
LatDecel(DEFAULT_LATDECEL),
Sim_MaxSonarReadingsPerSIP(DEFAULT_MAX_SONAR_READINGS_PER_SIP),
Sim_StallOnRot(DEFAULT_STALL_ON_ROT),
BatteryType(DEFAULT_BATTERY_TYPE),
GPSPosX(0), GPSPosY(0)
{
memset(RobotName, 0, ROBOT_IDENTIFIER_LEN);
memset(RobotClass, 0, ROBOT_IDENTIFIER_LEN);
memset(RobotSubclass, 0, ROBOT_IDENTIFIER_LEN);
strncpy(RobotName, DEFAULT_ROBOT_NAME, ROBOT_IDENTIFIER_LEN);
strncpy(RobotClass, DEFAULT_ROBOT_CLASS, ROBOT_IDENTIFIER_LEN);
strncpy(RobotSubclass, DEFAULT_ROBOT_SUBCLASS, ROBOT_IDENTIFIER_LEN);
}
};
/** These are values clients can set during a session, but not saved between */
class CurrentSettings {
public:
int TransVelMax;
int TransAccel;
int TransDecel;
int RotVelMax;
int RotAccel;
int RotDecel;
int LatVelMax;
int LatAccel;
int LatDecel;
bool BumpStallFront, BumpStallRear;
public:
CurrentSettings(RobotParams* params = NULL) :
TransVelMax(DEFAULT_TRANSVELMAX),
TransAccel(DEFAULT_TRANSACCEL),
TransDecel(DEFAULT_TRANSACCEL),
RotVelMax(DEFAULT_ROTVELMAX),
RotAccel(DEFAULT_ROTACCEL),
RotDecel(DEFAULT_ROTACCEL),
BumpStallFront(DEFAULT_BUMPSTALL_FRONT),
BumpStallRear(DEFAULT_BUMPSTALL_REAR)
{
if(params)
{
TransVelMax = params->TransVelMax;
TransAccel = params->TransAccel;
TransDecel = params->TransDecel;
RotVelMax = params->RotVelMax;
RotAccel = params->RotAccel;
RotDecel = params->RotDecel;
LatVelMax = params->LatVelMax;
LatAccel = params->LatAccel;
LatDecel = params->LatDecel;
}
}
};
/** Base class for packet generators */
class PacketGenerator
{
protected:
RobotInterface* robotInterface; ///< Robot interface to obtain data from
RobotParams* params; ///< Pointer to global robot paremeters struct
bool started; ///< Whether we should return packets or NULL
ArRobotPacket pkt; ///< A packet object to set data fields in, then return in getPacket()
unsigned short deviceIndex; ///< If there is more than one packet generator, this identifies them.
public:
/** Constructor, initialize driver table and parameters, and initialize
* 'started' to false. */
PacketGenerator(RobotInterface* _iface = 0, RobotParams* _params = 0, unsigned short _deviceIndex = 0) :
robotInterface(_iface),
params(_params),
started(false),
deviceIndex(_deviceIndex)
{
//print_debug("PacketGenerator[0x%x]::PacketGenerator: robotInterface=0x%x, params=0x%x", this, robotInterface, params);
}
void init(RobotInterface *_if, RobotParams* _params)
{
robotInterface = _if;
params = _params;
}
virtual ~PacketGenerator()
{}
/** set started to true. */
void start() {
started = true;
}
/** Set started to false. */
void stop() {
started = false;
}
/** Return value of started. */
bool isStarted() const { return started; }
/** Fill in a packet (a member object, pkt) with new data and return its
* pointer (so please don't deallocate the returned object!)
*/
virtual ArRobotPacket* getPacket() = 0;
RobotInterface* getRobotInterface() const { return robotInterface; }
unsigned short getDeviceIndex() const { return deviceIndex; }
};
/** Packet generator for standard SIPs.
* Create one instance of this class per client session.
*
* @todo One possible optimization is instead of recreating the SIP all the time from scratch,
* use a quickly updatable SIP: keep pointers into its buffer for the things
* you could change (However, you will still need to truncate it back and add
* the sonar data in each time, then add the stuff that comes after it).
*/
class SIPGenerator : public virtual PacketGenerator
{
protected:
ArTypes::Byte2 xPosAccum, yPosAccum;
int firstSonarReadingToSend;
bool logDataSent;
bool robotMoved;
public:
SIPGenerator(RobotInterface* _robotInterface = 0, RobotParams* _params = 0) :
PacketGenerator(_robotInterface, _params),
xPosAccum(0), yPosAccum(0),
firstSonarReadingToSend(0),
logDataSent(false),
robotMoved(false)
{
}
void setLogDataSent(bool l) { logDataSent = l; }
void setRobotMoved(bool m = true) { robotMoved = m; }
/** Get a finalized SIP with current data.
* @return packet filled with current data, or NULL if we haven't been
* "started" yet.
*/
virtual ArRobotPacket* getPacket();
};
/** Generate laser data packets */
class LaserPacketGenerator : public virtual PacketGenerator
{
protected:
size_t currentReading;
bool extendedInfoFormat;
bool robotMoved;
public:
LaserPacketGenerator(RobotInterface* _interface = 0, RobotParams* _params = 0, unsigned short deviceIndex = 0) :
PacketGenerator(_interface, _params, deviceIndex),
currentReading(0), extendedInfoFormat(false), robotMoved(false)
{}
/* Construct one laser data packet, if available. This will only contain some of the laser
* readings: there are too many to put in one packet. After a call to this
* method returns the last packet in a group, it returns NULL. The next call
* will return a packet with the first group of packets.
* If there is no laser data available, or we haven't been started with
* start(), then return NULL.
* If the ExtraReadingInfo flag is set, then the packet ID
* will be 0x61 instead of 0x60, and each reading in the packet will be followed
* by two bytes of extra info. The first 4 bits of the first byte contains a
* reflectance value (0 for normal).
*
* So, you can use this method like this:
* @code
* ArRobotPacket *laserPacket;
* while(laserPacket = laserGenerator.getPacket())
* send(laserPacket);
* @endcode
*/
virtual ArRobotPacket* getPacket();
virtual void start(bool enableExtraReadingInfo = false) {
extendedInfoFormat = enableExtraReadingInfo;
robotInterface->openLaser();
PacketGenerator::start();
}
virtual void stop() {
robotInterface->closeLaser();
PacketGenerator::stop();
}
/** A debugging tool, assert that this is a laser packet, then print it out */
void printLaserPacket(ArRobotPacket* pkt) const;
void setRobotMoved(bool m = true) {
robotMoved = true;
}
};
/** This class represents the state of one client session. It is created at the
* beginning of the session when the client connects, and is destroyed when the client disconnects.
*/
class Session {
public:
Session();
bool requestedOpenSonar;
ArTime started;
bool inWatchdogState;
bool eStopInProgress;
bool sendingSimstats;
ArTcpConnection connection;
ClientPacketReceiver packetReceiver;
ArRobotPacketSender packetSender;
CurrentSettings settings;
//ArSocket *clientSocket; use EmulatePioneer::myClientSocket instead
int syncSeq;
int handshakeAttempt;
ArTime syncStart;
SIPGenerator sipGen;
LaserPacketGenerator laserGen;
ArTime sentLastSIP;
ArTime gotLastCommand;
ArTime gotLastValidCommand;
unsigned long packetsSent;
unsigned long packetsReceived;
unsigned long laserPacketsSent;
unsigned long miscPacketsSent;
ArTime loggedStats;
ArTime lastSimSetPoseTime;
ArPose lastSimSetPose;
bool gotSimSetPose;
void init(RobotInterface *robotInterface, RobotParams *params, bool logSipsSent) {
sipGen.init(robotInterface, params);
sipGen.setLogDataSent(logSipsSent);
laserGen.init(robotInterface, params);
sentLastSIP.setToNow();
gotLastCommand.setToNow();
gotLastValidCommand.setToNow();
sendingSimstats = false;
eStopInProgress = false;
inWatchdogState = false;
requestedOpenSonar = false;
packetsSent = 0;
packetsReceived = 0;
laserPacketsSent = 0;
miscPacketsSent = 0;
gotSimSetPose = false;
//print_debug("Session[0x%x]::init(params=0x%x): sipGen=0x%x, laserGen=0x%x", this, params, &sipGen, &laserGen);
}
void checkLogStats(LogInterface *l);
void gotPacket();
void sentPacket();
void sentLaserPacket();
void sentMiscPacket();
void logStats(LogInterface *l);
void gotSetSimPose(int x, int y, int th)
{
gotSimSetPose = true;
lastSimSetPoseTime.setToNow();
lastSimSetPose.setPose(x, y, th);
laserGen.setRobotMoved();
sipGen.setRobotMoved();
}
};
/** Pioneer emulator over TCP.
*
* Communicates with a client over a TCP socket, recieve ARCOS command
* packets and call the appropriate methods in a RobotInterface object, and
* recieve data from the RobotInterface to return in information packets.
*
* EmulatePioneer can be used in two ways. You can either give it
* an already-connected client socket when constructing it (see
* EmulatePioneer::EmulatePioneer(RobotInterface*, std::string, ArTCPSocket*)),
* or it can listen for new clients itself on the first available port number
* after the default (see EmulatePioneer::EmulatePioneer(RobotInterface*,
* std::string, int)
*
* @todo eliminate some of the useless accessors
*
*/
class EmulatePioneer : public LogInterface
{
public:
// exception to request deletion of this EmulatePioneer instance, thrown by a callback function within that instance
class DeletionRequest : public virtual MobileSim::DeletionRequest
{
private:
EmulatePioneer* instance;
public:
DeletionRequest(EmulatePioneer* _inst) : instance(_inst) {}
virtual void doDelete();
};
// indicates that a session disconnected and no longer active, and should not be processed for cu
class Disconnected : public std::runtime_error
{
public:
Disconnected() : std::runtime_error("Session disconnected and inactive") {}
};
/** Create a new EmulatePioneer using the given RobotInterface and robot
* model, and use the given client TCP socket for communication.
* The socket must already be connected to the client. EmulatePioneer will
* not reopen it.
*/
EmulatePioneer(RobotInterface *rif, std::string robotModel, ArSocket *clientSocket, bool deleteRobotInterfaceOnDisconnect = false, bool deleteClientSocketOnDisconnect = true, const MobileSim::Options *userOptions = NULL);
/** Create a new EmulatePioneer using the given RobotInterface for the given
* robot model. Use @a port as the default when opening a new listening
* socket
*/
EmulatePioneer(RobotInterface *rif, std::string robotModel, int port =
DEFAULT_PIONEER_SIM_PORT, bool deleteRobotInterface = false, bool
trySubsequentPorts = true, const MobileSim::Options *userOptions = NULL);
private:
void init(const std::string& robotName, const MobileSim::Options *userOptions); // helper for the above constructors
public:
virtual ~EmulatePioneer();
/** Set identifier strings which may be returned in the SIMSTAT packet. */
void setSimulatorIdentification(const char* appName, const char* appVersion) {
myApplicationName = appName;
myApplicationVersion = appVersion;
}
void setVerbose(bool v) {
myVerbose = v;
}
void setWarnUnsupportedCommands(bool v) {
warn_unsupported_commands = v;
}
void setListenAddress(std::string addr) {
myListenAddress = addr;
}
/** Access list of commands to ignore (returns reference to actual stored
* set, so you can just change or replace it directly)
*/
std::set<int>& commandsToIgnore() { return myCommandsToIgnore; }
void setCommandsToIgnore(const std::set<int>& ig) {
myCommandsToIgnore = ig;
}
void setSRISimCompat(bool compat, bool lasercompat) {
SRISimCompat = compat;
SRISimLaserCompat = lasercompat;
}
typedef enum {
DISCONNECTED = ArUtil::BIT0, ///< Client no longer seems to be connected
GOTDATA = ArUtil::BIT1, ///< Got data from client
NODATA = ArUtil::BIT2, ///< Nothing received from client
SENTDATA = ArUtil::BIT3, ///< Sent data to client
CMDERR = ArUtil::BIT4, ///< Error interpreting or executing command from client
CONERR = ArUtil::BIT5, ///< Tried to connect with a new client but there was an error
DELETEME = ArUtil::BIT6, ///< Caller should delete this instance, it's no longer needed
IDLE = ArUtil::BIT7, ///< No client connected
} ProcessStat;
public:
bool openSocket();
/** Send SIP packets to client, and update misc. status states.
* This method must be called at the client's desired SIP frequency interval (currently always 100ms) for correct SIP timing
* Sets @a status according to what happened.
* @sa ProcessStat
* @sa processAll()
* @sa stat
*/
bool processSession() throw(DeletionRequest, Disconnected);
/** Call processSession() on all EmulatePioneer instances.
* If instance indicated client disconnect and its deleteOnDisconnect value is
* true, delete it.
* @return the bitwise or of all processSession() status codes.
* @param maxTime if nonzero, stop processing after this amount of time (msec). Processing will resume with the next EmulatePioneer instance in the next call to processAll(). Note, this may result in clients not getting packets on time depending on how long until the next call to processAll(). If 0, process each client once.
*/
static int processAll(int maxTime = 0);
int status; // could be moved into session
void loadMapObjects(ArMap *newmap);
protected:
/** Called after successfuly handshake; session has started, get ready to send and receive normal commands
*/
bool beginSession();
/** Check if a client is trying to connect, and accept TCP connection if so (calls accept() on socket)
@return false on error
*/
void acceptNewClient(unsigned int maxTime = 0);
/** End session, close connection, clean up */
void endSession() throw (DeletionRequest, Disconnected);
void handlePacket(ArRobotPacket *pkt) throw (DeletionRequest, Disconnected);
bool handleSyncPacket(ArRobotPacket *pkt) throw (DeletionRequest, Disconnected);
bool handleCommand(ArRobotPacket *pkt) throw (DeletionRequest, Disconnected);
void newSession();
void setupSessionClientConnection();
/**
* If a client is connected, read and process commands from clien
*/
void readClientInput(unsigned int maxTime);
public:
/** Get the TCP port either in use if one has been openned, or the
* port that will try to be used with when a new socket is opened, or -1 if
* not listening for clients.
*/
int getPort() const;
/** Get reference to robot's permanent parameters. You should not modify them. */
const RobotParams* getParams() const { return ¶ms; }
/** Get robot interface. */
RobotInterface* getRobotInterface() const { return robotInterface; }
// static void deleteAllInstances();
bool deleteOnDisconnect() const { return myDeleteOnDisconnect; }
/// may be NULL
ArSocket* getClientSocket() { return myClientSocket; }
void setLogPacketsReceived(bool l) { logCommandsReceived = l; }
void setLogSIPsSent(bool l) { logSIPsSent = l; }
void setCommercialRelease(bool l) { myCommercial = l; }
private:
friend class EmulatePioneer::DeletionRequest;
static std::list<EmulatePioneer*> ourActiveInstances; ///< @todo change to "our connected instances"
static std::list<EmulatePioneer*>::iterator ourNextActiveInstance; ///< the next EmulatePioneer* to process in ourActiveInstances list
RobotInterface* robotInterface;
RobotParams params;
int myTCPPort, myRequestedTCPPort;
ArSocket myListenSocket;
ArSocket *myClientSocket;
bool portOpened;
bool sessionActive;
//ArFunctorC<EmulatePioneer> myClientDisconnectCB;
// TODO fix copy constructors so we can not use a pointer, and so have slightly faster access to session contents
Session *session;
ArDeviceConnection *clientConnection;
/** Get a signed short integer from a command packet.
* Unpack three bytes from the packet's data buffer. The first byte
* determines the sign of the integer, the second and third bytes are
* the absolute value of the integer.
* No check is made that the packet's buffer contains enough bytes.
*/
ArTypes::Byte2 getIntFromPacket(ArRobotPacket* pkt);
long initialPoseX, initialPoseY;
int initialPoseTheta;
bool haveInitialPose;
std::string myApplicationName;
std::string myApplicationVersion;
bool myDeleteOnDisconnect;
bool myDeleteClientSocketOnDisconnect;
std::set<int> myCommandsToIgnore;
bool myTrySubsequentPorts;
bool myVerbose;
static bool mapMasterEnabled;
std::string myListenAddress;
static unsigned long lifetimeConnectionCount;
static unsigned long lifetimeFailedConnectionCount;
static unsigned long currentEmulatorCount;
bool logCommandsReceived;
bool logSIPsSent;
bool myCommercial;
bool sendSIMSTAT(ArDeviceConnection *con);
bool SRISimCompat;
bool SRISimLaserCompat;
bool sendMapChanged(std::string mapname, bool user = false, ArTypes::Byte status=1);
void newMapLoaded(MapLoadedInfo info);
ArFunctor1C<EmulatePioneer, MapLoadedInfo> newMapLoadedCB;
ArFunctor1C<EmulatePioneer, ArRobotPacket*> handlePacketCB;
ArFunctor1C<EmulatePioneer, unsigned int> readInputCB;
ArFunctor1C<EmulatePioneer, unsigned int> acceptClientCB;
bool useListenSocket;
bool warn_unsupported_commands;
std::list< std::vector<ArPose> > badGPSSectorVertices;
bool insideBadGPSSector(const ArPose& p);
void changeDigout(ArTypes::UByte mask, ArTypes::UByte states)
{
const ArTypes::UByte curr = robotInterface->getDigoutState();
robotInterface->setDigoutState( (curr & ~mask) | (~curr & mask & states) );
inform("Pioneer digital output changed to %s", MobileSim::byte_as_bitstring(robotInterface->getDigoutState()).c_str());
}
void changeDigin(ArTypes::UByte mask, ArTypes::UByte states)
{
const ArTypes::UByte curr = robotInterface->getDiginState();
robotInterface->setDiginState( (curr & ~mask) | (~curr & mask & states) );
inform("Pioneer digital input changed to %s", MobileSim::byte_as_bitstring(robotInterface->getDiginState()).c_str());
}
virtual void error_s(const char *m) {
if(robotInterface) robotInterface->error_s(m);
else LogInterface::error_s(m);
}
virtual void warn_s(const char *m) {
if(robotInterface) robotInterface->warn_s(m);
else LogInterface::warn_s(m);
}
virtual void inform_s(const char *m) {
if(robotInterface) robotInterface->inform_s(m);
else LogInterface::inform_s(m);
}
virtual void log_s(const char *m) {
if(robotInterface) robotInterface->log_s(m);
else LogInterface::log_s(m);
}
};
#endif