Skip to content

Commit

Permalink
Merge pull request #1 from esquires/add_auctioneer
Browse files Browse the repository at this point in the history
add auctioneer to wrap AuctionPlugin
  • Loading branch information
SyllogismRXS authored Oct 20, 2017
2 parents ec1e3db + 6aa01f9 commit 1a18210
Show file tree
Hide file tree
Showing 6 changed files with 287 additions and 0 deletions.
29 changes: 29 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ add_message_files(DIRECTORY msg FILES
ID.msg
Contact.msg
ContactArray.msg
RosBidAuction.msg
)

## Generate services in the 'srv' folder
Expand Down Expand Up @@ -169,6 +170,26 @@ endif()
find_package(scrimmage)
include_directories(${SCRIMMAGE_INCLUDE_DIRS})

if (SCRIMMAGE_ENABLE_PYTHON_BINDINGS)

add_definitions(-DENABLE_PYTHON_BINDINGS=1)
find_package(PythonInterp ${SCRIMMAGE_PYTHONLIBS_VERSION_STRING} REQUIRED)
find_package(PythonLibs ${SCRIMMAGE_PYTHONLIBS_VERSION_STRING} REQUIRED)

message(STATUS "Python Versions Found: ${PYTHONLIBS_VERSION_STRING}")
message(STATUS "PYTHON_LIBRARIES : ${PYTHON_LIBRARIES}")
message(STATUS "PYTHON_INCLUDE_DIRS: ${PYTHON_INCLUDE_DIRS}")

set(PYTHON_FOUND TRUE)
find_package(pybind11 REQUIRED)
include_directories(${pybind11_INCLUDE_DIRS})
set(PYBIND11_INCLUDE_DIRS ${pybind11_INCLUDE_DIRS})
include_directories("${PYTHON_INCLUDE_DIRS}")

else()
add_definitions(-DENABLE_PYTHON_BINDINGS=0)
endif()

# Allow scrimmage to find the maps in this repo:
set(SCRIMMAGE_LOCAL_CONFIG_DIR "$ENV{HOME}/.scrimmage")
set(SCRIMMAGE_ENV_DIR "${SCRIMMAGE_LOCAL_CONFIG_DIR}/env")
Expand All @@ -191,6 +212,11 @@ set_target_properties(scrimmage_autonomy_node PROPERTIES scrimmage_autonomy_node
target_link_libraries(scrimmage_autonomy_node ${catkin_LIBRARIES} ${SCRIMMAGE_LIBRARIES})
add_dependencies(scrimmage_autonomy_node ${${PROJECT_NAME}_EXPORTED_TARGETS} ${catkin_EXPORTED_TARGETS})

# the auction node
add_executable(auctioneer src/auctioneer.cpp)
target_link_libraries(auctioneer ${catkin_LIBRARIES} ${SCRIMMAGE_LIBRARIES})
add_dependencies(auctioneer scrimmage_ros_generate_messages_cpp)

#############
## Install ##
#############
Expand Down Expand Up @@ -238,3 +264,6 @@ add_dependencies(scrimmage_autonomy_node ${${PROJECT_NAME}_EXPORTED_TARGETS} ${c

## Add folders to be run by python nosetests
# catkin_add_nosetests(test)
find_package(rostest REQUIRED)
add_rostest_gtest(test_auction test/test_auction.launch test/test_auction.cpp)
target_link_libraries(test_auction ${catkin_LIBRARIES} ${SCRIMMAGE_LIBRARIES} gtest_main)
15 changes: 15 additions & 0 deletions launch/auction.launch
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<launch>
<node
launch-prefix="gdb -x ~/scrimmage/.gdbinit -f --args"
pkg="scrimmage_ros"
name="auctioneer1"
type="auctioneer"
output="screen"
args="1"/>
<node
pkg="scrimmage_ros"
name="auctioneer2"
type="auctioneer"
output="screen"
args="2"/>
</launch>
2 changes: 2 additions & 0 deletions msg/RosBidAuction.msg
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
int16 id
float32 bid
142 changes: 142 additions & 0 deletions src/auctioneer.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,142 @@
/*!
* @file
*
* @section LICENSE
*
* Copyright (C) 2017 by the Georgia Tech Research Institute (GTRI)
*
* This file is part of SCRIMMAGE.
*
* SCRIMMAGE is free software: you can redistribute it and/or modify it under
* the terms of the GNU Lesser General Public License as published by the
* Free Software Foundation, either version 3 of the License, or (at your
* option) any later version.
*
* SCRIMMAGE 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 Lesser General Public
* License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with SCRIMMAGE. If not, see <http://www.gnu.org/licenses/>.
*
* @author Kevin DeMarco <[email protected]>
* @author Eric Squires <[email protected]>
* @date 31 July 2017
* @version 0.1.0
* @brief Brief file description.
* @section DESCRIPTION
* A Long description goes here.
*
*/

#include <ros/ros.h>
#include <std_msgs/Int16.h>

#include <scrimmage/autonomy/Autonomy.h>
#include <scrimmage/entity/Entity.h>
#include <scrimmage/entity/External.h>
#include <scrimmage/pubsub/Message.h>
#include <scrimmage/msgs/AuctionMsgs.pb.h>

#include <scrimmage_ros/RosBidAuction.h>

#if ENABLE_PYTHON_BINDINGS == 1
#include <Python.h>
#endif

namespace sc = scrimmage;
using BidMsg = sc::Message<auction::BidAuction>;
using scrimmage_ros::RosBidAuction;

sc::MessageBase ros2sc_start_auction(const std_msgs::Int16 &ros_msg) {
sc::MessageBase sc_msg;
sc_msg.sender = ros_msg.data;
return sc_msg;
}

std_msgs::Int16 sc2ros_start_auction(const sc::MessageBasePtr &sc_msg) {
std_msgs::Int16 ros_msg;
ros_msg.data = sc_msg->sender;
return ros_msg;
}

BidMsg ros2sc_bid_auction(const RosBidAuction &ros_msg) {
BidMsg sc_msg;
sc_msg.sender = ros_msg.id;
sc_msg.data.set_bid(ros_msg.bid);
return sc_msg;
}

RosBidAuction sc2ros_bid_auction(const std::shared_ptr<BidMsg> &sc_msg) {
RosBidAuction ros_msg;
ros_msg.id = sc_msg->sender;
ros_msg.bid = sc_msg->data.bid();
return ros_msg;
}

int main(int argc, char **argv) {
if (argc < 2) {
std::cout << "need an argument for id" << std::endl;
return 1;
}

#if ENABLE_PYTHON_BINDINGS == 1
Py_Initialize();
#endif

sc::ID id(std::stoi(argv[1]), 0, 0);
ros::init(argc, argv, "auctioneer");

ros::NodeHandle nh;

std::map<std::string, std::string> info
{{"x", "0"}, {"y", "0"}, {"autonomy0", "AuctionAssign"}};

sc::External external;
external.create_entity(100, id, info, "/tmp");

auto pubs = external.entity()->autonomies().front()->pubs();

ros::Publisher pub_start_auction =
nh.advertise<std_msgs::Int16>("StartAuction", 1000);
external.pub_cb(sc2ros_start_auction,
pubs["StartAuction"], pub_start_auction);

ros::Publisher pub_bid_auction =
nh.advertise<RosBidAuction>("BidAuction", 1000);
external.pub_cb<auction::BidAuction>(sc2ros_bid_auction,
pubs["BidAuction"], pub_bid_auction);

auto subs = external.entity()->autonomies().front()->subs();

ros::Subscriber sub_start_auction = nh.subscribe("StartAuction", 1000,
external.sub_cb<std_msgs::Int16>(ros2sc_start_auction, subs["StartAuction"]));

ros::Subscriber sub_bid_auction = nh.subscribe("BidAuction", 1000,
external.sub_cb<RosBidAuction>(ros2sc_bid_auction, subs["BidAuction"]));

const double loop_rate_hz = 10;
const double startup_delay = 1;
const double runtime = 10;
ros::Rate loop_rate(loop_rate_hz);

// wait for all nodes to startup
for (int i = 0; i < startup_delay * loop_rate_hz; i++) {
loop_rate.sleep();
}

int ct = 0;
while (ros::ok() && ct < runtime * loop_rate_hz) {
external.step(ros::Time::now().toSec());
loop_rate.sleep();
ros::spinOnce();
++ct;
}

#if ENABLE_PYTHON_BINDINGS == 1
Py_Finalize();
#endif

return 0;
}
83 changes: 83 additions & 0 deletions test/test_auction.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
/*!
* @file
*
* @section LICENSE
*
* Copyright (C) 2017 by the Georgia Tech Research Institute (GTRI)
*
* This file is part of SCRIMMAGE.
*
* SCRIMMAGE is free software: you can redistribute it and/or modify it under
* the terms of the GNU Lesser General Public License as published by the
* Free Software Foundation, either version 3 of the License, or (at your
* option) any later version.
*
* SCRIMMAGE 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 Lesser General Public
* License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with SCRIMMAGE. If not, see <http://www.gnu.org/licenses/>.
*
* @author Kevin DeMarco <[email protected]>
* @author Eric Squires <[email protected]>
* @date 31 July 2017
* @version 0.1.0
* @brief Brief file description.
* @section DESCRIPTION
* A Long description goes here.
*
*/
#include <ros/ros.h>
#include <ros/time.h>
#include <gtest/gtest.h>

#include <std_msgs/Int16.h>
#include <scrimmage_ros/RosBidAuction.h>

using scrimmage_ros::RosBidAuction;

class AuctionTest : public testing::Test {
protected:
virtual void SetUp() {
ros::NodeHandle nh_;
sub_start_ =
nh_.subscribe("StartAuction", 20,
&AuctionTest::start_auction_callback, this);
sub_bid_ =
nh_.subscribe("BidAuction", 20,
&AuctionTest::bid_auction_callback, this);
}

int start_hx_ct_ = 0;
int bid_hx_ct_ = 0;
ros::Subscriber sub_start_;
ros::Subscriber sub_bid_;

public:
void start_auction_callback(const std_msgs::Int16::ConstPtr& msg) {
start_hx_ct_ += 1;
}

void bid_auction_callback(const RosBidAuction::ConstPtr &msg) {
bid_hx_ct_ += 1;
}
};

TEST_F(AuctionTest, msg) {

ros::Rate loop_rate(0.1);
loop_rate.sleep();
ros::spinOnce();

EXPECT_EQ(bid_hx_ct_, 2);
EXPECT_EQ(start_hx_ct_, 1);
}

int main(int argc, char **argv) {
::testing::InitGoogleTest(&argc, argv);
ros::init(argc, argv, "auction_test");
return RUN_ALL_TESTS();
}

16 changes: 16 additions & 0 deletions test/test_auction.launch
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<launch>
<node
pkg="scrimmage_ros"
name="auctioneer1"
type="auctioneer"
output="screen"
args="1"/>
<node
pkg="scrimmage_ros"
name="auctioneer2"
type="auctioneer"
output="screen"
args="2"/>
<test test-name="test_auction" pkg="scrimmage_ros" type="test_auction" />
</launch>

0 comments on commit 1a18210

Please sign in to comment.