Skip to content

Commit ad96967

Browse files
committed
Implemented bluetooth connection
1 parent 76fc127 commit ad96967

File tree

5 files changed

+155
-2
lines changed

5 files changed

+155
-2
lines changed

CMakeLists.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,8 @@ if(NOT BUILD_ONLY_DOCS)
2929
"src/app/app.cpp"
3030
"src/app/webots_app.hpp"
3131
"src/app/webots_app.cpp"
32+
"src/app/hs_app.hpp"
33+
"src/app/hs_app.cpp"
3234
)
3335

3436
add_executable(controller ${SOURCES})

src/app/hs_app.cpp

Lines changed: 108 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,108 @@
1+
#include "hs_app.hpp"
2+
3+
#include "../behaviour/manual.hpp"
4+
#include "../behaviour/line_follower.hpp"
5+
6+
#include <iostream>
7+
8+
using namespace hs::app;
9+
10+
#ifdef USE_BLUETOOTH
11+
12+
#include <unistd.h>
13+
#include <bluetooth/bluetooth.h>
14+
#include <bluetooth/rfcomm.h>
15+
16+
HSApp::HSApp(const robot::Robot& robot, behaviour::Manager& manager) : App(robot, manager)
17+
{
18+
this->joystickX = 0.0;
19+
this->joystickY = 0.0;
20+
this->cameraAngle = 0.0;
21+
22+
// Allocate socket
23+
this->socket = ::socket(AF_BLUETOOTH, SOCK_STREAM, BTPROTO_RFCOMM);
24+
25+
// Bind socket to port 1 of the first available local bluetooth adapter
26+
sockaddr_rc locAddr = {0};
27+
locAddr.rc_family = AF_BLUETOOTH;
28+
locAddr.rc_bdaddr = {{0, 0, 0, 0, 0, 0}};
29+
locAddr.rc_channel = HSApp::RFCOMM_CHANNEL;
30+
::bind(this->socket, (sockaddr*)&locAddr, sizeof(locAddr));
31+
32+
// Put socket into listening mode
33+
::listen(this->socket, 1);
34+
35+
// Accept one connection
36+
std::cout << "Waiting for HS Robot App to connect..." << std::endl;
37+
sockaddr_rc remAddr = {0};
38+
socklen_t remAddrLen = sizeof(remAddr);
39+
this->client = ::accept(this->socket, reinterpret_cast<sockaddr*>(&remAddr), &remAddrLen);
40+
if (this->client < 0)
41+
{
42+
std::cerr << "Connection to HS Robot App failed, couldn't accept socket connection (accept() returned < 0)"
43+
<< std::endl;
44+
abort();
45+
}
46+
47+
// Get address string from client and print it
48+
char addrStr[100];
49+
ba2str(reinterpret_cast<const bdaddr_t*>(&remAddr), addrStr);
50+
std::cout << "Connected to HS Robot App (" << addrStr << ")" << std::endl;
51+
}
52+
53+
HSApp::~HSApp()
54+
{
55+
std::cout << "Closed HS Robot App connection" << std::endl;
56+
::close(this->client);
57+
::close(this->socket);
58+
}
59+
60+
double HSApp::getJoystickX() const
61+
{
62+
return this->joystickX;
63+
}
64+
65+
double HSApp::getJoystickY() const
66+
{
67+
return this->joystickY;
68+
}
69+
70+
double HSApp::getCameraAngle() const
71+
{
72+
return this->cameraAngle;
73+
}
74+
75+
void HSApp::update()
76+
{
77+
// TODO:
78+
// Implement bluetooth protocol
79+
}
80+
81+
#else
82+
83+
HSApp::HSApp(const robot::Robot& robot, behaviour::Manager& manager) : App(robot, manager)
84+
{
85+
abort(); // Unsupported operation
86+
}
87+
88+
double HSApp::getJoystickX() const
89+
{
90+
abort(); // Unsupported operation
91+
}
92+
93+
double HSApp::getJoystickY() const
94+
{
95+
abort(); // Unsupported operation
96+
}
97+
98+
double HSApp::getCameraAngle() const
99+
{
100+
abort(); // Unsupported operation
101+
}
102+
103+
void HSApp::update()
104+
{
105+
abort(); // Unsupported operation
106+
}
107+
108+
#endif // USE_BLUETOOTH

src/app/hs_app.hpp

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
#ifndef HS_APP_HS_APP_HPP
2+
#define HS_APP_HS_APP_HPP
3+
4+
#include "app.hpp"
5+
6+
namespace hs::app
7+
{
8+
/// Implements the abstract App class using the real HS Robot application.
9+
class HSApp : public App
10+
{
11+
public:
12+
/// @param robot The robot which this app controls.
13+
/// @param manager The behaviour manager of the robot.
14+
HSApp(const robot::Robot& robot, behaviour::Manager& manager);
15+
virtual ~HSApp() override;
16+
17+
// Abstract methods implementation
18+
virtual double getJoystickX() const override;
19+
virtual double getJoystickY() const override;
20+
virtual double getCameraAngle() const override;
21+
virtual void update() override;
22+
23+
private:
24+
#ifdef USE_BLUETOOTH
25+
26+
static inline const uint8_t RFCOMM_CHANNEL = 1; ///< RFCOMM channel.
27+
28+
/// Registers the SDP service so that it can be discovered by the client.
29+
void registerSDPService();
30+
31+
int socket; ///< Listener socket.
32+
int client; ///< Client socket.
33+
34+
double joystickX; ///< Last joystick X axis value.
35+
double joystickY; ///< Last joystick Y axis value.
36+
double cameraAngle; ///< Target camera angle.
37+
38+
#endif // USE_BLUETOOTH
39+
};
40+
} // namespace hs::app
41+
42+
#endif // HS_APP_HS_APP_HPP

src/app/webots_app.hpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,4 +37,4 @@ namespace hs::app
3737
};
3838
} // namespace hs::app
3939

40-
#endif // HS_APP_WEBOTS_APP_HPP
40+
#endif // HS_APP_WEBOTS_APP_HPP

src/main.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
#include "behaviour/line_follower.hpp"
99

1010
#include "app/webots_app.hpp"
11+
#include "app/hs_app.hpp"
1112

1213
using namespace hs;
1314
using namespace hs::robot;
@@ -23,7 +24,7 @@ int main()
2324
Manager* manager = new Manager(*robot);
2425

2526
// Setup app
26-
App* app = new WebotsApp(*robot, *manager);
27+
App* app = new HSApp(*robot, *manager);
2728

2829
// Register behaviour types
2930
manager->registerType<Manual>(std::ref(*app));

0 commit comments

Comments
 (0)