Skip to content

Commit ff940fa

Browse files
authored
Merge pull request #4 from arshPratap/ArmingServiceBin
Added Arming Services with Binary Creation Example
2 parents 730a691 + 9585798 commit ff940fa

File tree

4 files changed

+253
-0
lines changed

4 files changed

+253
-0
lines changed
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
# AP_Services_Demo
2+
## Prerequisites
3+
- Install [ROS-2 Humble](https://docs.ros.org/en/humble/Installation.html)
4+
- Install the necessary ROS-2 packages in [ardupilot_ros2](https://github.com/arshPratap/ardupilot_ros2)
5+
- switch to the **ArmingClient** branch
6+
- build the [ap_custom_services](https://github.com/arshPratap/ardupilot_ros2/tree/ArmingClient/ap_custom_services) package
7+
- build the [ap_service_clients](https://github.com/arshPratap/ardupilot_ros2/tree/ArmingClient/ap_service_clients) package
8+
- Install [Micro-XRCE-Agent](https://micro-xrce-dds.docs.eprosima.com/en/latest/installation.html#installing-the-agent-standalone)
9+
- Install [Micro-XRCE-Client library](https://micro-xrce-dds.docs.eprosima.com/en/latest/installation.html#installing-the-client-standalone)
10+
- Install [Integration Services](https://integration-service.docs.eprosima.com/en/latest/installation_manual/installation.html) :
11+
- Get system handles for [Fast-DDS](https://github.com/eProsima/FastDDS-SH)
12+
- Get system handles for [ROS 2](https://github.com/eProsima/ROS2-SH)
13+
- Install the integration service by running the following command : `colcon build --cmake-args -DMIX_ROS_PACKAGES="ap_custom_services"`
14+
## Arming Motors Example (DDS Server)
15+
### Terminal 1 (XRCE-Agent)
16+
- Run XRCE Agent using the following command `cd /usr/local/bin && MicroXRCEAgent udp4 -p 2019`
17+
### Terminal 2 (XRCE-Client)
18+
- In the **Arming_DDS_Server** folder, compile the Server file as `g++ Arm_Motors_DDS_Server.cpp -lmicrocdr -lmicroxrcedds_client`
19+
- Run the Server script as `./a.out 127.0.0.1 2019`
20+
- There is a third flag : `armable` , which has the following values :
21+
- **1 (default)** : to mimic conditions when arming motors is allowed
22+
- **0** : to mimic conditions when arming is not allowed
23+
### Terminal 3 (Integration Service)
24+
- Source your ROS 2 installation
25+
- Source the installation of the ROS-2 packages present in **ardupilot_ros2** from your ROS 2 workspace
26+
- Source Integration Services from your workspace
27+
- Run the service using the following command `Arm_Motors_DDS_IS_config.yaml` from the **Arming_DDS_Server** folder
28+
### Terminal 4 (ROS-2)
29+
- Source ROS-2 installation
30+
- Source the installation of the ROS-2 packages present in **ardupilot_ros2** from your ROS 2 workspace
31+
- Run the ROS-2 client as `ros2 run ap_service_clients arm_motors_client sample_count`
32+
- _Note : **sample_count** above refers to the total number of requests the user wants to make to the DDS server_
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
types:
2+
idls:
3+
- >
4+
struct ArmMotors_Request
5+
{
6+
boolean arm;
7+
};
8+
9+
struct ArmMotors_Response
10+
{
11+
boolean result;
12+
};
13+
systems:
14+
dds: { type: fastdds }
15+
ros2: { type: ros2 }
16+
17+
routes:
18+
dds_server:
19+
server: dds
20+
clients: ros2
21+
22+
services:
23+
arm_motors: {
24+
request_type: ArmMotors_Request,
25+
reply_type: ArmMotors_Response,
26+
route: dds_server,
27+
remap: {
28+
dds: {
29+
topic: ArmMotorsService,
30+
},
31+
ros2: {
32+
request_type: "ap_custom_services/ArmMotors:request",
33+
reply_type: "ap_custom_services/ArmMotors:response"
34+
}
35+
}
36+
}
Lines changed: 153 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,153 @@
1+
#include <uxr/client/client.h>
2+
3+
#include <stdio.h>
4+
#include <string.h>
5+
#include <stdlib.h>
6+
#include <inttypes.h>
7+
8+
#define STREAM_HISTORY 8
9+
#define BUFFER_SIZE UXR_CONFIG_UDP_TRANSPORT_MTU* STREAM_HISTORY
10+
11+
static uxrStreamId reliable_out;
12+
static uxrStreamId reliable_in;
13+
14+
static uxrObjectId participant_id;
15+
static uxrObjectId replier_id;
16+
17+
bool isArmable = true;
18+
19+
void on_request(
20+
uxrSession* session,
21+
uxrObjectId object_id,
22+
uint16_t request_id,
23+
SampleIdentity* sample_id,
24+
ucdrBuffer* ub,
25+
uint16_t length,
26+
void* args)
27+
{
28+
(void) object_id;
29+
(void) request_id;
30+
(void) length;
31+
(void) args;
32+
33+
bool arm;
34+
ucdr_deserialize_bool(ub,&arm);
35+
36+
if (arm){
37+
printf("Request recieved for Arming \n");
38+
}else{
39+
printf("Request recieved for Disarming \n");
40+
}
41+
uint8_t reply_buffer[8] = {
42+
0
43+
};
44+
45+
ucdrBuffer reply_ub;
46+
47+
bool result = false;
48+
if(isArmable){
49+
result = arm;
50+
}else{
51+
result = false;
52+
}
53+
54+
ucdr_init_buffer(&reply_ub, reply_buffer, sizeof(reply_buffer));
55+
ucdr_serialize_bool(&reply_ub,result);
56+
57+
uxr_buffer_reply(session, reliable_out, replier_id, sample_id, reply_buffer, sizeof(reply_buffer));
58+
59+
if(result){
60+
printf("Reply : Armed \n");
61+
}else{
62+
printf("Reply : Disarmed \n");
63+
}
64+
65+
}
66+
67+
int main(
68+
int args,
69+
char** argv)
70+
{
71+
if (3 > args || 0 == atoi(argv[2]))
72+
{
73+
printf("usage: program [-h | --help] | ip port [armable]\n");
74+
printf("usage: armable: 0 to set arming condition to false \n");
75+
printf("usage: armable: 1 to set arming condition to true \n");
76+
return 0;
77+
}
78+
79+
char* ip = argv[1];
80+
char* port = argv[2];
81+
isArmable = (args == 4) ? atoi(argv[3]) : 1;
82+
uint32_t key = 0xCCCCDDDD;
83+
84+
// Transport
85+
uxrUDPTransport transport;
86+
if (!uxr_init_udp_transport(&transport, UXR_IPv4, ip, port))
87+
{
88+
printf("Error at init transport.\n");
89+
return 1;
90+
}
91+
92+
// Session
93+
uxrSession session;
94+
uxr_init_session(&session, &transport.comm, key);
95+
uxr_set_request_callback(&session, on_request, 0);
96+
if (!uxr_create_session(&session))
97+
{
98+
printf("Error at init session.\n");
99+
return 1;
100+
}
101+
102+
// Streams
103+
uint8_t output_reliable_stream_buffer[BUFFER_SIZE];
104+
reliable_out = uxr_create_output_reliable_stream(&session, output_reliable_stream_buffer, BUFFER_SIZE,
105+
STREAM_HISTORY);
106+
107+
uint8_t input_reliable_stream_buffer[BUFFER_SIZE];
108+
reliable_in = uxr_create_input_reliable_stream(&session, input_reliable_stream_buffer, BUFFER_SIZE, STREAM_HISTORY);
109+
110+
// Create entities
111+
participant_id = uxr_object_id(0x01, UXR_PARTICIPANT_ID);
112+
uint16_t participant_req = uxr_buffer_create_participant_bin(&session,reliable_out,participant_id,0,"ArmMotors_Server",UXR_REPLACE);
113+
uxrQoS_t qos = {
114+
.durability = UXR_DURABILITY_TRANSIENT_LOCAL,
115+
.reliability = UXR_RELIABILITY_RELIABLE,
116+
.history = UXR_HISTORY_KEEP_ALL,
117+
.depth = 1
118+
};
119+
120+
replier_id = uxr_object_id(0x01, UXR_REPLIER_ID);
121+
uint16_t replier_req = uxr_buffer_create_replier_bin(&session,reliable_out,replier_id,participant_id,"ArmMotorsService","ArmMotors_Request","ArmMotors_Response","ArmMotorsService_Request","ArmMotorsService_Reply",qos,UXR_REPLACE);
122+
123+
// Send create entities message and wait its status
124+
uint8_t status[2];
125+
uint16_t requests[2] = {
126+
participant_req, replier_req
127+
};
128+
if (!uxr_run_session_until_all_status(&session, 1000, requests, status, 2))
129+
{
130+
printf("Error at create entities: participant: %i requester: %i\n", status[0], status[1]);
131+
return 1;
132+
}
133+
134+
printf("Entities Created Successfully \n");
135+
136+
// Request requests
137+
uxrDeliveryControl delivery_control = {
138+
0
139+
};
140+
delivery_control.max_samples = UXR_MAX_SAMPLES_UNLIMITED;
141+
uint16_t read_data_req =
142+
uxr_buffer_request_data(&session, reliable_out, replier_id, reliable_in, &delivery_control);
143+
(void) read_data_req;
144+
145+
// Read request
146+
bool connected = true;
147+
while (connected)
148+
{
149+
connected = uxr_run_session_time(&session, 1000);
150+
}
151+
152+
return 0;
153+
}
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
# AP_Services_Demo
2+
## Prerequisites
3+
- Install [ROS-2 Humble](https://docs.ros.org/en/humble/Installation.html)
4+
- Install the necessary ROS-2 packages in [ardupilot_ros2](https://github.com/arshPratap/ardupilot_ros2)
5+
- switch to the **ArmingClient** branch
6+
- build the [ap_custom_services](https://github.com/arshPratap/ardupilot_ros2/tree/ArmingClient/ap_custom_services) package
7+
- build the [ap_service_clients](https://github.com/arshPratap/ardupilot_ros2/tree/ArmingClient/ap_service_clients) package
8+
- Install [Micro-XRCE-Agent](https://micro-xrce-dds.docs.eprosima.com/en/latest/installation.html#installing-the-agent-standalone)
9+
- Install [Micro-XRCE-Client library](https://micro-xrce-dds.docs.eprosima.com/en/latest/installation.html#installing-the-client-standalone)
10+
- Install [Integration Services](https://integration-service.docs.eprosima.com/en/latest/installation_manual/installation.html) :
11+
- Get system handles for [Fast-DDS](https://github.com/eProsima/FastDDS-SH)
12+
- Get system handles for [ROS 2](https://github.com/eProsima/ROS2-SH)
13+
- Install the integration service by running the following command : `colcon build --cmake-args -DMIX_ROS_PACKAGES="ap_custom_services"`
14+
## Arming Motors Example (DDS Server)
15+
### Terminal 1 (XRCE-Agent)
16+
- Run XRCE Agent using the following command `cd /usr/local/bin && MicroXRCEAgent udp4 -p 2019`
17+
### Terminal 2 (XRCE-Client)
18+
- In the **Arming_DDS_Server** folder, compile the Server file as `g++ Arm_Motors_DDS_Server_Bin.cpp -lmicrocdr -lmicroxrcedds_client`
19+
- Run the Server script as `./a.out 127.0.0.1 2019`
20+
- There is a third flag : `armable` , which has the following values :
21+
- **1 (default)** : to mimic conditions when arming motors is allowed
22+
- **0** : to mimic conditions when arming is not allowed
23+
### Terminal 3 (Integration Service)
24+
- Source your ROS 2 installation
25+
- Source the installation of the ROS-2 packages present in **ardupilot_ros2** from your ROS 2 workspace
26+
- Source Integration Services from your workspace
27+
- Run the service using the following command `Arm_Motors_DDS_Bin_IS_config.yaml` from the **Arming_DDS_Server** folder
28+
### Terminal 4 (ROS-2)
29+
- Source ROS-2 installation
30+
- Source the installation of the ROS-2 packages present in **ardupilot_ros2** from your ROS 2 workspace
31+
- Run the ROS-2 client as `ros2 run ap_service_clients arm_motors_client sample_count`
32+
- _Note : **sample_count** above refers to the total number of requests the user wants to make to the DDS server_

0 commit comments

Comments
 (0)