Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
111 changes: 68 additions & 43 deletions C/RAD0_actuator/main.c
Original file line number Diff line number Diff line change
@@ -1,66 +1,91 @@
// Copyright 2018 Proyectos y Sistemas de Mantenimiento SL (eProsima).
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

#include <rclc/rclc.h>
#include <std_msgs/msg/string.h>
#include <std_msgs/msg/int32.h>
#include <std_msgs/msg/u_int32.h>

#include <stdio.h>

#define ASSERT(ptr) if (ptr == NULL) return -1;
#define CUSTOM_ASSERT(ptr) if ((ptr) == NULL) {return -1;}

static rclc_publisher_t* engine_pub;
static rclc_publisher_t * engine_pub;
static uint32_t engine_power = 100;

void engine_on_message(const void* msgin)
void engine_on_message(const void * msgin)
{

const std_msgs__msg__Int32* msg = (const std_msgs__msg__Int32*)msgin;

if (msg->data + engine_power > 0)
{
engine_power += msg->data;
}
else
{
engine_power = 0;
}

// Publish new altitude
std_msgs__msg__UInt32 msg_out;
msg_out.data = engine_power;
rclc_publish(engine_pub, (const void*)&msg_out);
const std_msgs__msg__Int32 * msg = (const std_msgs__msg__Int32 *)msgin;

if (msg->data + engine_power > 0) {
engine_power += msg->data;
} else {
engine_power = 0;
}

// Publish new altitude
rclc_ret_t ret;
std_msgs__msg__UInt32 msg_out;
msg_out.data = engine_power;
ret = rclc_publish(engine_pub, (const void *)&msg_out);
}

int main(int argc, char* argv[])
int main(int argc, char * argv[])
{
(void)argc;
(void)argv;
(void)argc;
(void)argv;

rclc_node_t * node = NULL;
rclc_subscription_t * engine_sub = NULL;

rclc_node_t* node = NULL;
rclc_subscription_t* engine_sub = NULL;

rclc_ret_t ret;

ret = rclc_init(0, NULL);
if (ret != RCL_RET_OK) {
return -1;
}

rclc_init(0, NULL);
node = rclc_create_node("rad0_actuator_c", "");
ASSERT(node);
engine_sub = rclc_create_subscription(node, RCLC_GET_MSG_TYPE_SUPPORT(std_msgs, msg, Int32), "std_msgs_msg_Int32", engine_on_message, 1, false);
ASSERT(engine_sub);
engine_pub = rclc_create_publisher(node, RCLC_GET_MSG_TYPE_SUPPORT(std_msgs, msg, UInt32), "std_msgs_msg_UInt32", 1);
ASSERT(engine_pub);
node = rclc_create_node("rad0_actuator_c", "");
CUSTOM_ASSERT(node);
engine_sub = rclc_create_subscription(node, RCLC_GET_MSG_TYPE_SUPPORT(std_msgs, msg,
Int32), "std_msgs_msg_Int32", engine_on_message, 1,
false);
CUSTOM_ASSERT(engine_sub);
engine_pub = rclc_create_publisher(node, RCLC_GET_MSG_TYPE_SUPPORT(std_msgs, msg,
UInt32), "std_msgs_msg_UInt32",
1);
CUSTOM_ASSERT(engine_pub);

// Publish new altitude
std_msgs__msg__UInt32 msg_out;
msg_out.data = engine_power;
rclc_publish(engine_pub, (const void*)&msg_out);
// Publish new altitude
std_msgs__msg__UInt32 msg_out;
msg_out.data = engine_power;
ret = rclc_publish(engine_pub, (const void *)&msg_out);

rclc_spin_node(node);
rclc_spin_node(node);

//if (altitude_sub) rclc_destroy_subscription(altitude_sub);
if (engine_sub) rclc_destroy_subscription(engine_sub);
if (engine_pub) rclc_destroy_publisher(engine_pub);
if (node) rclc_destroy_node(node);
// if (altitude_sub) rclc_destroy_subscription(altitude_sub);
if (engine_sub) {
ret = rclc_destroy_subscription(engine_sub);
}
if (engine_pub) {
ret = rclc_destroy_publisher(engine_pub);
}
if (node) {
ret = rclc_destroy_node(node);
}

printf("Actuator node closed.\n");
printf("Actuator node closed.\n");

return 0;
return 0;
}
88 changes: 54 additions & 34 deletions C/RAD0_altitude_sensor/main.c
Original file line number Diff line number Diff line change
@@ -1,3 +1,17 @@
// Copyright 2018 Proyectos y Sistemas de Mantenimiento SL (eProsima).
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

#include <rclc/rclc.h>
#include <std_msgs/msg/float64.h>
#include <std_msgs/msg/int32.h>
Expand All @@ -6,50 +20,56 @@
#include <math.h>
#include <time.h>

#define ASSERT(ptr) if (ptr == NULL) return -1;
#define CUSTOM_ASSERT(ptr) if ((ptr) == NULL) {return -1;}

/**
* @brief
*
* @param argc
* @param argv
* @return int
* @brief
*
* @param argc
* @param argv
* @return int
*/
int main(int argc, char *argv[])
int main(int argc, char * argv[])
{
(void)argc;
(void)argv;
rclc_init(0, NULL);
(void)argc;
(void)argv;

rclc_ret_t ret;

ret = rclc_init(0, NULL);
if (ret != RCL_RET_OK) {
return -1;
}

rclc_node_t * node = NULL;
rclc_publisher_t * publisher = NULL;

rclc_node_t* node = NULL;
rclc_publisher_t* publisher = NULL;
node = rclc_create_node("rad0_altitude_sensor_c", "");
CUSTOM_ASSERT(node);
publisher = rclc_create_publisher(node, RCLC_GET_MSG_TYPE_SUPPORT(std_msgs, msg,
Float64), "std_msgs_msg_Float64",
1);
CUSTOM_ASSERT(publisher);

node = rclc_create_node("rad0_altitude_sensor_c", "");
ASSERT(node);
publisher = rclc_create_publisher(node, RCLC_GET_MSG_TYPE_SUPPORT(std_msgs, msg, Float64), "std_msgs_msg_Float64", 1);
ASSERT(publisher);

double A = 0;

double A = 0;

while (rclc_ok()) {
A += 0.0001;

while (rclc_ok())
{
A += 0.0001;
// Publish new altitude
std_msgs__msg__Float64 msg;
msg.data = 500 * sin(A) + 950;
ret = rclc_publish(publisher, (const void *)&msg);

// Publish new altitude
std_msgs__msg__Float64 msg;
msg.data = 500 * sin(A) + 950;
rclc_publish(publisher, (const void*)&msg);

// Spin node
rclc_spin_node_once(node, 0);
}

// Spin node
rclc_spin_node_once(node, 0);
}
if (publisher) {ret = rclc_destroy_publisher(publisher);}
if (node) {ret = rclc_destroy_node(node);}

if (publisher) rclc_destroy_publisher(publisher);
if (node) rclc_destroy_node(node);

printf("altitude sendor closed.\n");
return 0;
}
printf("altitude sensor closed.\n");
return 0;
}
Loading