Skip to content

Commit ff376b5

Browse files
committed
created bare power board teensy pub and sub
1 parent a44621d commit ff376b5

File tree

1 file changed

+61
-57
lines changed

1 file changed

+61
-57
lines changed

pio_workspace/src/power_main.cpp

Lines changed: 61 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -420,6 +420,8 @@ void power_loop() {
420420

421421
#include <std_msgs/msg/int32.h>
422422
#include <std_msgs/msg/int16_multi_array.h>
423+
#include <std_msgs/msg/float32.h>
424+
#include <std_msgs/msg/float32_multi_array.h>
423425

424426
#if !defined(MICRO_ROS_TRANSPORT_ARDUINO_SERIAL)
425427
#error This example is only avaliable for Arduino framework with serial transport.
@@ -429,14 +431,20 @@ void power_loop() {
429431

430432
bool micro_ros_init_successful;
431433

432-
rcl_subscription_t array_subscriber;
433-
std_msgs__msg__Int16MultiArray array_msg_subscription;
434+
rcl_subscription_t propulsion_microseconds_subscriber;
435+
std_msgs__msg__Int16MultiArray propulsion_microseconds_msg;
434436

435-
rcl_publisher_t publisher;
436-
std_msgs__msg__Int32 msg;
437+
rcl_publisher_t power_batteries_voltage_publisher;
438+
std_msgs__msg__Float32MultiArray power_batteries_voltage_msg;
437439

438-
rcl_publisher_t array_publisher;
439-
std_msgs__msg__Int16MultiArray array_msg_publishing;
440+
rcl_publisher_t power_thrusters_current_publisher;
441+
std_msgs__msg__Float32MultiArray power_thrusters_current_msg;
442+
443+
rcl_publisher_t power_board_temperature_publisher;
444+
std_msgs__msg__Float32 power_board_temperature_msg;
445+
446+
rcl_publisher_t power_teensy_temperature_publisher;
447+
std_msgs__msg__Float32 power_teensy_temperature_msg;
440448

441449
rclc_executor_t executor;
442450
rclc_support_t support;
@@ -471,13 +479,7 @@ enum states {
471479
AGENT_DISCONNECTED
472480
} state;
473481

474-
void subscription_callback(const void * msgin)
475-
{
476-
const std_msgs__msg__Int32 * msg = (const std_msgs__msg__Int32 *)msgin;
477-
digitalWrite(LED_PIN, (msg->data == 0) ? LOW : HIGH);
478-
}
479-
480-
void array_subscription_callback(const void * msgin)
482+
void propulsion_microseconds_callback(const void * msgin)
481483
{
482484
const std_msgs__msg__Int16MultiArray * msg = (const std_msgs__msg__Int16MultiArray *)msgin;
483485

@@ -496,18 +498,12 @@ void array_subscription_callback(const void * msgin)
496498
void timer_callback(rcl_timer_t * timer, int64_t last_call_time) {
497499
RCLC_UNUSED(last_call_time);
498500
if (timer != NULL) {
499-
RCSOFTCHECK(rcl_publish(&publisher, &msg, NULL));
500-
RCSOFTCHECK(rcl_publish(&array_publisher, &array_msg_publishing, NULL));
501-
msg.data++;
501+
RCSOFTCHECK(rcl_publish(&power_batteries_voltage_publisher, &power_batteries_voltage_publisher, NULL));
502+
RCSOFTCHECK(rcl_publish(&power_thrusters_current_publisher, &power_thrusters_current_msg, NULL));
503+
RCSOFTCHECK(rcl_publish(&power_board_temperature_publisher, &power_board_temperature_msg, NULL));
504+
RCSOFTCHECK(rcl_publish(&power_teensy_temperature_publisher, &power_teensy_temperature_msg, NULL));
502505

503-
array_msg_publishing.data.data[0]++;
504-
array_msg_publishing.data.data[1]++;
505-
array_msg_publishing.data.data[2]++;
506-
array_msg_publishing.data.data[3]++;
507-
array_msg_publishing.data.data[4]++;
508-
array_msg_publishing.data.data[5]++;
509-
array_msg_publishing.data.data[6]++;
510-
array_msg_publishing.data.data[7]++;
506+
// can add logic to messages here
511507
}
512508
}
513509

@@ -519,29 +515,41 @@ bool create_entities()
519515
RCCHECK(rclc_support_init(&support, 0, NULL, &allocator));
520516

521517
// create node
522-
RCCHECK(rclc_node_init_default(&node, "int32_publisher_rclc", "", &support));
518+
RCCHECK(rclc_node_init_default(&node, "power_node", "", &support));
519+
520+
RCCHECK(rclc_subscription_init_default(
521+
&propulsion_microseconds_subscriber,
522+
&node,
523+
ROSIDL_GET_MSG_TYPE_SUPPORT(std_msgs, msg, Int16MultiArray),
524+
"/propulsion/microseconds"));
523525

524526
/// create publisher
525527
RCCHECK(rclc_publisher_init_default(
526-
&publisher,
528+
&power_batteries_voltage_publisher,
527529
&node,
528-
ROSIDL_GET_MSG_TYPE_SUPPORT(std_msgs, msg, Int32),
529-
"micro_ros_platformio_node_publisher"));
530+
ROSIDL_GET_MSG_TYPE_SUPPORT(std_msgs, msg, Float32MultiArray),
531+
"/power/batteries/voltage"));
530532

531533
RCCHECK(rclc_publisher_init_default(
532-
&array_publisher,
534+
&power_thrusters_current_publisher,
533535
&node,
534-
ROSIDL_GET_MSG_TYPE_SUPPORT(std_msgs, msg, Int16MultiArray),
535-
"array_publisher"));
536+
ROSIDL_GET_MSG_TYPE_SUPPORT(std_msgs, msg, Float32MultiArray),
537+
"/power/thrusters/current"));
538+
539+
RCCHECK(rclc_publisher_init_default(
540+
&power_board_temperature_publisher,
541+
&node,
542+
ROSIDL_GET_MSG_TYPE_SUPPORT(std_msgs, msg, Float32),
543+
"/power/board/temperature"));
536544

537-
RCCHECK(rclc_subscription_init_default(
538-
&array_subscriber,
545+
RCCHECK(rclc_publisher_init_default(
546+
&power_teensy_temperature_publisher,
539547
&node,
540-
ROSIDL_GET_MSG_TYPE_SUPPORT(std_msgs, msg, Int16MultiArray),
541-
"array_subscriber"));
548+
ROSIDL_GET_MSG_TYPE_SUPPORT(std_msgs, msg, Float32),
549+
"/power/teensy/temperature"));
542550

543551
// create timer,
544-
const unsigned int timer_timeout = 1000;
552+
const unsigned int timer_timeout = 100;
545553
RCCHECK(rclc_timer_init_default(
546554
&timer,
547555
&support,
@@ -550,9 +558,9 @@ bool create_entities()
550558

551559
// create executor
552560
executor = rclc_executor_get_zero_initialized_executor();
553-
RCCHECK(rclc_executor_init(&executor, &support.context, 10, &allocator)); // number arbitrarily set, idk what is the correct on yet, trial and error later on
561+
RCCHECK(rclc_executor_init(&executor, &support.context, 100, &allocator)); // number arbitrarily set, idk what is the correct on yet, trial and error later on
554562
RCCHECK(rclc_executor_add_timer(&executor, &timer));
555-
RCCHECK(rclc_executor_add_subscription(&executor, &array_subscriber, &array_msg_subscription, &array_subscription_callback, ON_NEW_DATA));
563+
RCCHECK(rclc_executor_add_subscription(&executor, &propulsion_microseconds_subscriber, &propulsion_microseconds_msg, &propulsion_microseconds_callback, ON_NEW_DATA));
556564

557565

558566
return true;
@@ -574,9 +582,11 @@ void destroy_entities()
574582
rmw_context_t * rmw_context = rcl_context_get_rmw_context(&support.context);
575583
(void) rmw_uros_set_context_entity_destroy_session_timeout(rmw_context, 0);
576584

577-
rcl_publisher_fini(&publisher, &node);
578-
rcl_publisher_fini(&array_publisher, &node);
579-
rcl_subscription_fini(&array_subscriber, &node);
585+
rcl_publisher_fini(&power_batteries_voltage_publisher, &node);
586+
rcl_publisher_fini(&power_thrusters_current_publisher, &node);
587+
rcl_publisher_fini(&power_board_temperature_publisher, &node);
588+
rcl_publisher_fini(&power_teensy_temperature_publisher, &node);
589+
rcl_subscription_fini(&propulsion_microseconds_subscriber, &node);
580590
rcl_timer_fini(&timer);
581591
rclc_executor_fini(&executor);
582592
rcl_node_fini(&node);
@@ -605,25 +615,19 @@ void power_setup() {
605615
set_microros_serial_transports(Serial);
606616
delay(2000);
607617

608-
array_msg_publishing.data.size = 8;
609-
array_msg_publishing.data.capacity = 8;
610-
array_msg_publishing.data.data = (int16_t*)malloc(array_msg_publishing.data.capacity * sizeof(int16_t));
618+
propulsion_microseconds_msg.data.size = 8;
619+
propulsion_microseconds_msg.data.capacity = 8;
620+
propulsion_microseconds_msg.data.data = (int16_t*)malloc(propulsion_microseconds_msg.data.capacity * sizeof(int16_t));
611621

612-
array_msg_subscription.data.size = 8;
613-
array_msg_subscription.data.capacity = 8;
614-
array_msg_subscription.data.data = (int16_t*)malloc(array_msg_subscription.data.capacity * sizeof(int16_t));
622+
power_batteries_voltage_msg.data.size = 2;
623+
power_batteries_voltage_msg.data.capacity = 2;
624+
power_batteries_voltage_msg.data.data = (float*)malloc(power_batteries_voltage_msg.data.capacity * sizeof(float));
615625

616-
state = WAITING_AGENT;
626+
power_board_temperature_msg.data = 0.0;
617627

618-
msg.data = 0;
619-
array_msg_publishing.data.data[0] = 0;
620-
array_msg_publishing.data.data[1] = 1;
621-
array_msg_publishing.data.data[2] = 2;
622-
array_msg_publishing.data.data[3] = 3;
623-
array_msg_publishing.data.data[4] = 4;
624-
array_msg_publishing.data.data[5] = 5;
625-
array_msg_publishing.data.data[6] = 6;
626-
array_msg_publishing.data.data[7] = 7;
628+
power_teensy_temperature_msg.data = 0.0;
629+
630+
state = WAITING_AGENT;
627631
}
628632

629633
void power_loop() {

0 commit comments

Comments
 (0)