Permalink
Browse files

Simple RPC Client added for basic RPC usage demonstration.

Bumpers support added to RPC Server
  • Loading branch information...
mani-monaj committed Mar 11, 2012
1 parent cca2299 commit 3b7f4a549c9ad603aa8a525913f7adb5f8d9444a
Showing with 109 additions and 5 deletions.
  1. +1 −0 .gitignore
  2. +1 −1 build
  3. +38 −1 librapicore/robotrpcserver.cpp
  4. +17 −2 librapicore/robotrpcserver.h
  5. +12 −0 librapirpc/CMakeLists.txt
  6. +2 −1 librapirpc/client.cpp
  7. +38 −0 librapirpc/simplerpcclient.cpp
View
@@ -24,3 +24,4 @@ autolab-rapi.cbp
autolab-rapi.layout
.project
.cproject
+/nbproject/private/
View
2 build
@@ -1 +1 @@
-196
+204
@@ -8,7 +8,7 @@ RobotRpcServer::RobotRpcServer( ARobot * robot, int port,
pthread_mutex_t * dataMutex,
ADrivetrain2dof * drivetrain,
APowerPack * powerpack,
- ARangeFinder * rangefinder )
+ ARangeFinder * rangefinder , ABinarySensorArray *bumper)
: mServer( port )
{
// initialize data members
@@ -17,6 +17,7 @@ RobotRpcServer::RobotRpcServer( ARobot * robot, int port,
mDrivetrain = drivetrain;
mPowerPack = powerpack;
mRangeFinder = rangefinder;
+ mBumper = bumper;
// setup handlers as appropriate
if ( mDrivetrain ) {
@@ -43,6 +44,17 @@ RobotRpcServer::RobotRpcServer( ARobot * robot, int port,
( this, &RobotRpcServer::getRanges ),
"getRanges" );
}
+
+ if (mBumper)
+ {
+ mServer.addMethodHandler(new Server::RPCMethod<RobotRpcServer>
+ (this, &RobotRpcServer::getBumperDev),
+ "getBumperDev");
+ mServer.addMethodHandler(new Server::RPCMethod<RobotRpcServer>
+ (this, &RobotRpcServer::getBumpers),
+ "getBumpers");
+ }
+
}
//------------------------------------------------------------------------------
RobotRpcServer::~RobotRpcServer()
@@ -124,6 +136,17 @@ void RobotRpcServer::getRangeFinderDev( variant params,
results[ "beamPose" ] = toVariant<array> ( beamPose );
}
//------------------------------------------------------------------------------
+
+void RobotRpcServer::getBumperDev(jsonrpc::variant params,
+ jsonrpc::object& results,
+ const std::string& ip,
+ int port)
+{
+ pthread_mutex_lock(mRobotMutex);
+ results["numSamples"] = toVariant<int> (mBumper->getNumSamples());
+ pthread_mutex_unlock(mRobotMutex);
+}
+//------------------------------------------------------------------------------
void RobotRpcServer::getDrivetrain( variant params,
object& results,
const std::string& ip,
@@ -175,4 +198,18 @@ void RobotRpcServer::getRanges( variant params,
}
//------------------------------------------------------------------------------
+void RobotRpcServer::getBumpers(jsonrpc::variant params,
+ jsonrpc::object& results,
+ const std::string& ip, int port)
+{
+ pthread_mutex_lock(mRobotMutex);
+ array bumpers;
+ for (unsigned int i = 0; i < mBumper->getNumSamples(); ++i)
+ {
+ bumpers.push_back(toVariant<bool> (mBumper->mBitData[i]) );
+ }
+ results["bumpers"] = toVariant( bumpers );
+ pthread_mutex_unlock(mRobotMutex);
+}
+//------------------------------------------------------------------------------
} // namespace
@@ -24,6 +24,7 @@
#include "rangefinder.h"
#include "drivetrain2dof.h"
#include "powerpack.h"
+#include "binarysensorarray.h"
#include "pose2d.h"
#include "velocity2d.h"
#include "printerror.h"
@@ -40,7 +41,7 @@ class RobotRpcServer
public:
/**
* Constructs a JSON RPC server to export the robot data. At the moment,
- * drivetrain, rangefinder, and powerpack data is exported. Note that the
+ * drivetrain, rangefinder, powerpack and bumper data is exported. Note that the
* server runs in a separate thread from the robot controller so access to
* robot data should be controller. Typically, you create a pthread mutex
* when you initialize your controller and lock it during your updateData()
@@ -52,7 +53,7 @@ class RobotRpcServer
*/
RobotRpcServer( ARobot * robot, int port, pthread_mutex_t * dataMutex,
ADrivetrain2dof * drivetrain, APowerPack * powerpack,
- ARangeFinder * rangefinder );
+ ARangeFinder * rangefinder, ABinarySensorArray* bumper );
/** default destructor */
~RobotRpcServer();
/** start the server in its own thread */
@@ -79,6 +80,13 @@ class RobotRpcServer
jsonrpc::object& results,
const std::string& ip,
int port );
+
+ /** Send the current bumper configuration */
+ void getBumperDev(jsonrpc::variant params,
+ jsonrpc::object& results,
+ const std::string& ip,
+ int port);
+
//---- device get/set calls ------------------------------------------------
/** send position and velocity information */
void getDrivetrain( jsonrpc::variant params,
@@ -95,6 +103,11 @@ class RobotRpcServer
jsonrpc::object& results,
const std::string& ip,
int port );
+ /** send bumpers data */
+ void getBumpers(jsonrpc::variant params,
+ jsonrpc::object& results,
+ const std::string& ip,
+ int port);
/** utility routine to pack a CVelocity2d object into a jsonrpc::variant */
jsonrpc::variant packVelocity( CVelocity2d velocity );
@@ -114,6 +127,8 @@ class RobotRpcServer
APowerPack * mPowerPack;
/** the rangefinder object we are serving (if it exists) */
ARangeFinder * mRangeFinder;
+ /** the bumper object we are serving (if it exists) */
+ ABinarySensorArray *mBumper;
};
} // namespace
View
@@ -39,6 +39,9 @@ SET_TARGET_PROPERTIES( ${TARGET} PROPERTIES
VERSION ${VERSION}
)
+
+
+
INSTALL(TARGETS ${TARGET}
LIBRARY DESTINATION lib )
@@ -76,3 +79,12 @@ IF( QT_FOUND )
# RUNTIME DESTINATION bin )
ENDIF( QT_FOUND )
ENDIF( WITH_GUI )
+
+# Simple Client
+ADD_EXECUTABLE( simpleclient
+ simplerpcclient.cpp
+)
+
+TARGET_LINK_LIBRARIES( simpleclient
+ autolab-rapi-rpc
+ )
View
@@ -2,7 +2,8 @@
#include <string>
#include <pthread.h>
#include "RapiGui"
-#include "rpcrobot.h"
+//#include "rpcrobot.h"
+#include "RapiRpc"
/** a robot thread container */
struct robot_t {
@@ -0,0 +1,38 @@
+#include <string.h>
+#include "RapiRpc"
+#include "cbrobot.h"
+
+
+int main (int argc, char* argv[])
+{
+ if( argc != 3 )
+ {
+ std::cerr << "Arguments is a host IP and a port number" << std::endl;
+ return -1;
+ }
+
+ Rapi::RobotRpcClient* robot;
+ std::string address= argv[1];
+ int port = atoi(argv[2]);
+
+ robot = new Rapi::RobotRpcClient(address, port);
+ //robot->init();
+
+ std::cout << "Init Done ..." << std::endl;
+
+ double maxCapacity;
+ std::vector<float> ranges;
+ while (true)
+ {
+ std::cout << "Testing robot " << address << std::endl;
+ robot->getPowerPackDev(maxCapacity);
+ ranges = robot->getRanges();
+ std::cout << "Range[0] :" << ranges.at(0) << std::endl;
+ }
+ //robot->quit();
+
+ std::cout << "Quit!" << std::endl;
+
+ return 0;
+
+}

0 comments on commit 3b7f4a5

Please sign in to comment.