Skip to content

Commit 959ab5d

Browse files
committed
robot_plugin.cpp
1 parent bacbb28 commit 959ab5d

File tree

4 files changed

+269
-0
lines changed

4 files changed

+269
-0
lines changed
Lines changed: 103 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,103 @@
1+
// ibcs/plugins/external/robotic_adapter_plugin/robotic_adapter_plugin.cpp
2+
#include "robot_plugin.h"
3+
#include <iostream>
4+
#include <algorithm>
5+
6+
namespace ibcs::plugin::external::robot {
7+
8+
RoboticAdapterPlugin::RoboticAdapterPlugin()
9+
: proto_(make_default_robot_protocol())
10+
{
11+
}
12+
13+
RoboticAdapterPlugin::~RoboticAdapterPlugin() {
14+
}
15+
16+
ibcs::plugin::PluginInfo RoboticAdapterPlugin::info() const {
17+
return {
18+
"external.robotic_adapter",
19+
"1.0.0",
20+
"IBCS External Robotics"
21+
};
22+
}
23+
24+
bool RoboticAdapterPlugin::on_load() {
25+
std::cout << "[robotic_adapter] plugin loaded\n";
26+
return true;
27+
}
28+
29+
bool RoboticAdapterPlugin::on_unload() {
30+
std::cout << "[robotic_adapter] plugin unloaded\n";
31+
if (proto_) proto_->disconnect();
32+
return true;
33+
}
34+
35+
std::vector<std::string> RoboticAdapterPlugin::capabilities() const {
36+
return {
37+
"robot.send",
38+
"robot.read",
39+
"robot.motion_adapt",
40+
"safety.guard"
41+
};
42+
}
43+
44+
bool RoboticAdapterPlugin::configure(const std::string &endpoint) {
45+
endpoint_ = endpoint;
46+
if (!proto_->connect(endpoint_)) {
47+
std::cerr << "[robotic_adapter] could not connect to " << endpoint << "\n";
48+
return false;
49+
}
50+
if (!proto_->ping()) {
51+
std::cerr << "[robotic_adapter] ping failed\n";
52+
return false;
53+
}
54+
std::cout << "[robotic_adapter] configured successfully\n";
55+
return true;
56+
}
57+
58+
// safety: clamp ranges & duration
59+
bool RoboticAdapterPlugin::safety_filter(MotionCommand &cmd) const {
60+
if (cmd.duration_seconds <= 0.0 || cmd.duration_seconds > 5.0) {
61+
std::cerr << "[robotic_adapter] duration out of bounds\n";
62+
return false;
63+
}
64+
65+
if (!cmd.joint_positions.empty())
66+
clamp_vec(cmd.joint_positions, -2.0, 2.0); // rad or m
67+
if (!cmd.joint_velocities.empty())
68+
clamp_vec(cmd.joint_velocities, -1.0, 1.0);
69+
70+
return true;
71+
}
72+
73+
void RoboticAdapterPlugin::clamp_vec(std::vector<double> &v, double lo, double hi) const {
74+
for (auto &x : v) {
75+
if (x < lo) x = lo;
76+
if (x > hi) x = hi;
77+
}
78+
}
79+
80+
bool RoboticAdapterPlugin::send_motion_command(MotionCommand cmd) {
81+
if (!proto_) return false;
82+
83+
if (!safety_filter(cmd)) {
84+
std::cerr << "[robotic_adapter] safety_filter blocked motion\n";
85+
return false;
86+
}
87+
return proto_->send_motion(cmd);
88+
}
89+
90+
optional<RobotState> RoboticAdapterPlugin::get_state() {
91+
if (!proto_) return nullopt;
92+
return proto_->read_state();
93+
}
94+
95+
extern "C" ibcs::plugin::IPlugin* create_plugin() {
96+
return new RoboticAdapterPlugin();
97+
}
98+
99+
extern "C" void destroy_plugin(ibcs::plugin::IPlugin* p) {
100+
delete p;
101+
}
102+
103+
} // namespace ibcs::plugin::external::robot
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
#pragma once
2+
#include "../../base/plugin_interface.h"
3+
#include "robot_protocol.h"
4+
5+
#include <memory>
6+
#include <string>
7+
8+
#include <vector>
9+
#include <optional>
10+
11+
namespace ibcs :: plugin :: external :: robot
12+
{
13+
class RoboticAdapterPlugin : public ibcs :: plugin :: IPlugin
14+
{
15+
public:
16+
RoboticAdapterPlugin();
17+
~RoboticAdapterPlugin();
18+
19+
// IPlugin
20+
ibcs :: plugin :: PluginInfo info() const override;
21+
bool on_load() override;
22+
bool on_unload() override;
23+
24+
vector<string> capabilities() const override;
25+
26+
// setup protocol and (endpoint = Ip/serial...)
27+
bool configure(const string &endpoint);
28+
29+
// control operations..
30+
bool send_motion_command(const MotionCommand cmd);
31+
optional<RobotState> get_state();
32+
33+
// health check..
34+
bool ping();
35+
36+
private:
37+
unique_ptr<IRobotProtocol> proto_;
38+
string endpoint_;
39+
40+
// small safety guard..
41+
bool safety_filter(MotionCommand &cmd) const ;
42+
43+
// Saturation helpers..
44+
void clamp_vec(vector<double> &v, double lo, double hi) const;
45+
};
46+
extern "C"
47+
{
48+
ibcs :: plugin :: IPlugin *create_plugin();
49+
void destroy_plugin(ibcs :: plugin :: IPlugin *p);
50+
}
51+
} // namespace ibcs :: plugin :: external :: robot
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
#include "robot_protocol.h"
2+
3+
#include <iostream>
4+
using namespace std;
5+
6+
namespace ibcs :: plugin :: external :: robot
7+
{
8+
class DummyRobotProtocol : public IRobotProtocol
9+
{
10+
public:
11+
bool connect(const string &endpoint) override
12+
{
13+
cout <<"Robot proto" << "connect to " << endpoint << endl;
14+
connected = true;
15+
return true;
16+
}
17+
18+
void disconnect() override
19+
{
20+
cout <<"Robot proto" << "disconnect" << endl;
21+
connected = false;
22+
}
23+
24+
bool send_motion(const MotionCommand &cmd) override
25+
{
26+
if (!connected)
27+
return false;
28+
29+
cout <<"Robot proto - send motion: joints ="
30+
<< cmd.joint_positions.size() << "duration = "
31+
<< cmd.duration_seconds << "seconds" << endl;
32+
return true;
33+
}
34+
35+
optional<RobotState> read_state() override
36+
{
37+
if (!connected)
38+
return nullopt;
39+
40+
RobotState state;
41+
state.battery = 95.0;
42+
state.joint_positions = {0.0, 1.0, 0.5};
43+
state.joint_velocities = {0.0, 0.0, 0.0};
44+
state.estop_triggered = false;
45+
state.raw_status = "OK";
46+
47+
return state;
48+
}
49+
50+
bool ping() override
51+
{
52+
return connected;
53+
}
54+
55+
private:
56+
bool connected = false ;
57+
};
58+
59+
IRobotProtocol *make_default_robot_protocol(){
60+
return new DummyRobotProtocol();
61+
}
62+
} // namespace ibcs :: plugin :: external :: robot
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
#pragma once
2+
#include <string>
3+
4+
#include <vector>
5+
#include <optional>
6+
7+
using namespace std;
8+
9+
namespace ibcs :: plugin :: external :: robot
10+
{
11+
// High level state from robot...
12+
struct RobotState
13+
{
14+
double battery = 100.0 ;
15+
vector<double> joint_positions;
16+
vector<double> joint_velocities;
17+
bool estop_triggered = false;
18+
string raw_status;
19+
};
20+
21+
// A generic motion command..
22+
struct MotionCommand
23+
{
24+
vector<double> joint_positions;
25+
vector<double> joint_velocities;
26+
27+
double duration_seconds = 0.0;
28+
};
29+
30+
class IRobotProtocol
31+
{
32+
public:
33+
virtual ~IRobotProtocol() = default;
34+
35+
// establish connection to robot endpoint..
36+
virtual bool connect(const string &endpoint) = 0;
37+
38+
// disconnect cleanly..
39+
virtual void disconnect() = 0;
40+
41+
// send a single motion command..
42+
virtual bool send_motion(const MotionCommand &cmd) = 0 ;
43+
44+
// query low level state..
45+
virtual optional<RobotState> read_state() = 0;
46+
47+
// small health check. .
48+
virtual bool ping() = 0;
49+
};
50+
51+
// factory for default protocol (dummy stab)
52+
IRobotProtocol *make_default_robot_protocol();
53+
} // namespace ibcs :: plugin :: external :: robot

0 commit comments

Comments
 (0)