Robot CPP Programming Basics-01 The first program Hello World

Many courses start with C/C++ or some other programming courses, which are called basic courses. Then go to the senior year of the undergraduate course to study robotics, which is a very big time loss and very low efficiency.

C++/MCU/Embedded/ROS and other programming foundations can be combined into one course for implementation. These materials have been iterated for more than three rounds, and all of them are open. You can refer to them if you need them. If you don’t need them, I will write them for myself^_^ Thank you all for your continued help, support and encouragement.

https://www.lanqiao.cn/courses/854

apply to

  1. C++ 11 14 17 20……
  2. Arduino UNO DUE ESP8266 ESP32…… 
  3. ARM……
  4. ROS1 kinetic melodic noetic……
  5. ROS2 foxy humble……

Only part of the material is disclosed, the main reason is that there are too many contents... 


C++

#include<iostream>
using namespace std;

main()
{
    cout<<"Hello World !";
}

This is a simple program written in C++ language. Let me explain it in detail for you in Chinese:

First, #include<iostream>there is a preprocessing directive that tells the compiler to include the iostream file before the program runs. The iostream file contains the functions and objects we need to perform input/output operations.

Then, using namespace std;tell the compiler that we intend to use the std namespace. This is because the functions and objects in the iostream file are in the std namespace.

Next comes main()the function, which is the entry point to the C++ program. When you run the program, the main function is called.

In the main function, we use coutto output a message, namely "Hello World!". cout is a stream object that represents standard output (usually a monitor). Here, we use the cout object to send a string (i.e. "Hello World!") to standard output.

In general, the function of this program is to output a "Hello World!" message.


Arduino

In the Arduino IDE, using the C++ programming language, you need to use the Serial.print()OR Serial.println()function to output to the serial port instead cout. Also, the Arduino IDE doesn't support that using namespace std;, so you'll need to use the full standard library name.

Here is the modified code:

#include <iostream>  
  
int main()  
{  
    Serial.print("Hello World !");  
    return 0;  
}

Note: This code may not work directly in the Arduino IDE. The Arduino IDE is mainly used for microcontroller programming, which is different from the standard C++ compiler. To print "Hello World!" in the Arduino IDE, you can use the following code:

void setup() {  
  Serial.begin(9600);  
}  
  
void loop() {  
  Serial.println("Hello World!");  
  delay(1000);  
}

The above code will print "Hello World!" as soon as the serial connection is opened, then wait 1 second, and repeat the process. This is the usual way of serial communication in the Arduino IDE. 

M5ATOMS3 basic 01 button


ROS1

In ROS1, you can use ROS_INFOfunctions instead to coutoutput messages. Here's ROS_INFOan example of code rewritten to use:

#include <ros/ros.h>  
  
int main(int argc, char **argv)  
{  
    ros::init(argc, argv, "my_node");  
    ros::NodeHandle nh;  
  
    ROS_INFO("Hello World !");  
  
    return 0;  
}

In the above code, we have included ros/ros.hthe header file, which is the main header file in ROS1. Then, we ros::initinitialize the ROS node using the function and create a node handle ( ros::NodeHandle). Next, we use ROS_INFOthe function to output the "Hello World!" message.

Note that you will need to compile and run this code with a ROS1 environment to ensure proper handling of ROS-related functions and messaging.


ROS2

Can it be done quickly and independently? ? ?

In ROS2, you can use macros rclcppin the library RCL_INFOinstead coutfor message output. Here's an example of rewriting the code to use RCL_INFOmacros:

#include <rclcpp/rclcpp.hpp>  
  
int main(int argc, char **argv)  
{  
    rclcpp::init(argc, argv);  
  
    rclcpp::Node::SharedPtr node = rclcpp::Node::make_shared("my_node");  
    rclcpp::Rate rate(1);  // 设置一个频率为1Hz的定时器  
  
    while (rclcpp::ok()) {  
        node->get_logger().info("Hello World !");  // 使用RCL_INFO宏输出消息  
        rate.sleep();  // 等待一段时间以保持1Hz的频率  
    }  
  
    rclcpp::shutdown();  // 关闭ROS节点  
  
    return 0;  
}

In the above code, we include rclcpp/rclcpp.hppthe header file first. Then, we use rclcpp::initthe function to initialize the ROS node. Next, we create a shared node handle and set a timer with a frequency of 1Hz. In the loop, we use RCL_INFOa macro to output the "Hello World!" message to the log of the ROS system. Then, we rate.sleep()wait for some time by calling to keep the frequency at 1Hz. Finally, we use rclcpp::shutdown()the function to shutdown the ROS node.

Note that you will need to compile and run this code with a ROS2 environment to ensure proper handling of ROS-related functions and messaging.


ROS1 & Arduino

#include <ros.h>
#include <std_msgs/String.h>

ros::NodeHandle nh;

std_msgs::String str_msg;
ros::Publisher chatter("chatter", &str_msg);

char hello[13] = "hello world!";

void setup()
{
  nh.initNode();
  nh.advertise(chatter);
}

void loop()
{
  str_msg.data = hello;
  chatter.publish( &str_msg );
  nh.spinOnce();
  delay(1000);
}

This code is a simple ROS (Robot Operating System) node written in C++. ROS is a framework for writing robot software that supports a variety of programming languages, including C++, Python, and Java.

The main function of the code is to publish a message containing the string "hello world!" to the ROS topic named "chatter". Let's explain each part of the code in detail:

  1. #include <ros.h>: This is the header file that includes the ROS framework.
  2. #include <std_msgs/String.h>: This is a header file containing string messages in Standard Message Types. In ROS, messages are custom data types used to pass data between nodes.
  3. ros::NodeHandle nh;: Create a ROS node handle object. Node handles are the main interface used to interact with the ROS system.
  4. std_msgs::String str_msg;: Create a string message object that will be used to publish messages.
  5. ros::Publisher chatter("chatter", &str_msg);: Create a publisher object that will be used to publish messages to the topic named "chatter".
  6. char hello[13] = "hello world!";: Create a character array to store the string "hello world!".
  7. void setup(): Defines a function called "setup" that will be executed once when the node is initialized.
  8. nh.initNode();: Initialize the node.
  9. nh.advertise(chatter);: Announces the publisher, causing the node to start listening to the topic named "chatter" and prepare to publish messages.
  10. void loop(): Defines a function called "loop" that will be executed continuously while the node is running.
  11. str_msg.data = hello;: Assign the string "hello world!" to the data field of the message object.
  12. chatter.publish( &str_msg );: Post a message to the "chatter" topic.
  13. nh.spinOnce();: Poll the message queue once to receive messages from other nodes.
  14. delay(1000);: Delay for 1 second, then repeat the loop.

Overall, this code creates a simple ROS node that continually publishes messages containing the string "hello world!" to a topic named "chatter".

M5ATOMS3 basics 03 send a greeting to ROS1 (rosserial)


ROS2 & Arduino

M5ATOMS3 basics 04 send a greeting to ROS2 (micro-ROS)


Guess you like

Origin blog.csdn.net/ZhangRelay/article/details/132191872