ROS Summer School Sharing-2023

Advantages of Cloud Classes

https://gitcode.net/ZhangRelay/cocubesim

Network programming and stand-alone programming

Network programming and stand-alone programming are two different programming methods, and their main difference lies in their application scenarios and implementation technologies.

1 Application Scenarios

Network programming is mainly used to build Internet-based applications, such as web applications, online shopping platforms, online games, etc. These applications need to exchange data with remote servers through the network, so it is necessary to use network programming technology to realize data transmission and communication.

Stand-alone programming is mainly used to build local-based applications, such as desktop applications, local game mods, local data analysis tools, etc. These applications do not need to exchange data with remote servers over the network, but process data directly on the local computer.

2 Implementation Technology

The implementation technology of network programming mainly includes TCP/IP protocol, HTTP protocol, FTP protocol and so on. These protocols stipulate how data is transmitted and communicated in the network, so network programming needs to use corresponding protocols to realize data exchange and communication.

The implementation technology of stand-alone programming includes local database, file system, operating system API, etc. These technologies are used to process and store data on a local computer without the need for data exchange over a network.

3 programming languages

Network programming can use various programming languages, such as Java, Python, C++, JavaScript, etc. These languages ​​have corresponding network programming libraries and frameworks, which can facilitate network programming.

Stand-alone programming can also use various programming languages, such as Java, C++, Python, etc. These languages ​​also have corresponding local programming libraries and frameworks, which can easily realize stand-alone programming.

In general, network programming and stand-alone programming have their own application scenarios and implementation technologies. When choosing a programming method, you need to choose an appropriate programming method according to actual needs and application scenarios.

distributed thinking and focused thinking

Distributed thinking and concentrated thinking are two different ways of thinking. For ROS programming, they have certain differences.

Distributed thinking refers to decomposing a problem into multiple parts, then considering the characteristics and solutions of each part separately, and finally comprehensively obtaining an overall solution. In ROS programming, distributed thinking is expressed as decomposing the robot system into multiple components, such as sensors, controllers, actuators, etc., and then considering the function and implementation of each component separately, and finally combining these components into one through the ROS architecture. Complete system.

Concentrated thinking refers to seeing the problem as a whole and considering the solution to the problem from a holistic perspective. In ROS programming, concentrated thinking is manifested as considering the robot system as a whole, considering the overall function and goals of the system, and then designing and implementing a complete control system architecture, integrating various components to achieve overall control.

Therefore, the difference between distributed thinking and concentrated thinking in ROS programming lies in the different angles and methods of thinking about problems. Distributed thinking pays more attention to decomposing the problem into multiple parts and considering the solution of each part separately, while concentrated thinking pays more attention to considering the solution of the problem from an overall perspective. Both ways of thinking have their advantages and disadvantages, and the specific application needs to choose the appropriate way according to the actual situation.

real case scenario

artificial intelligence

M5ATOMS3 basics 03 send a greeting to ROS1 (rosserial)

Official code:

#!/usr/bin/env python
# license removed for brevity
import rospy
from std_msgs.msg import String

def talker():
    pub = rospy.Publisher('chatter', String, queue_size=10)
    rospy.init_node('talker', anonymous=True)
    rate = rospy.Rate(10) # 10hz
    while not rospy.is_shutdown():
        hello_str = "hello world %s" % rospy.get_time()
        rospy.loginfo(hello_str)
        pub.publish(hello_str)
        rate.sleep()

if __name__ == '__main__':
    try:
        talker()
    except rospy.ROSInterruptException:
        pass

This code is a ROS node written in Python that publishes a "hello world" message to the ROS network.

Here is a functional explanation of the code:

  1. #!/usr/bin/env python: This is a shebang that specifies the interpreter used by the script. Here, it tells the operating system to use the Python interpreter to execute this script.
  2. license removed for brevity: This line is commented out, indicating that no license information is included in the code.
  3. import rospy: Import the ROS library for communicating with the ROS system.
  4. from std_msgs.msg import String: Import the String message type from the ROS standard message library for publishing and subscribing messages.
  5. def talker():: Defines a talkerfunction called that publishes a "hello world" message.
  6. pub = rospy.Publisher('chatter', String, queue_size=10): Create a publisher object pubfor publishing String type messages to the ROS network, the topic name is "chatter", and the queue size is 10.
  7. rospy.init_node('talker', anonymous=True): Initialize the ROS node and name it "talker", anonymous mode (no node name required).
  8. rate = rospy.Rate(10): Create a rate object rateto publish messages at a frequency of 10Hz.
  9. while not rospy.is_shutdown():: Enter an infinite loop until the ROS node shuts down.
  10. hello_str = "hello world %s" % rospy.get_time(): Creates a string variable containing the current time hello_strin the format "hello world <current time>".
  11. rospy.loginfo(hello_str): Use the log system of ROS to record information, and print the current time string to the log.
  12. pub.publish(hello_str): Publish the current time string message to the ROS network.
  13. rate.sleep(): Wait for a period of time to keep the publishing frequency at 10Hz.
  14. if __name__ == '__main__':: Check if the current script is running as the main program.
  15. try:: Start an exception handling block for catching possible interrupt signals.
  16. talker(): Call talkerthe function to start publishing messages.
  17. except rospy.ROSInterruptException:: Catch the ROS interrupt signal exception and execute passthe statement in the exception handling block to ignore the exception and continue execution.

Overall, this code creates a ROS node that uses a publisher to publish a "hello world" message to a topic called "chatter" at 10Hz.

Modify the Python code above to publish floating-point numbers instead of strings.

#!/usr/bin/env python  
# license removed for brevity  
import rospy  
from std_msgs.msg import Float64  
  
def talker():  
    pub = rospy.Publisher('chatter', Float64, queue_size=10)  
    rospy.init_node('talker', anonymous=True)  
    rate = rospy.Rate(10) # 10hz  
    while not rospy.is_shutdown():  
        hello_float = 3.14  # 定义一个浮点数  
        rospy.loginfo(hello_float)  
        pub.publish(hello_float)  
        rate.sleep()  
  
if __name__ == '__main__':  
    try:  
        talker()  
    except rospy.ROSInterruptException:  
        pass

In the above code, the float 3.14 is issued. At the same time, in order to match the data type of the floating point number, Stringchange the message type from Float64to and from std_msgs.msgimport in Float64. Correspondingly, rospy.loginfo()this floating-point number is also recorded in the code.


ros2 run demo_nodes_py talker
ros2 run demo_nodes_py listener


 

 

 

Supongo que te gusta

Origin blog.csdn.net/ZhangRelay/article/details/132004291
Recomendado
Clasificación