Hello, everyone, I am a small fish, I wish you a happy Mid-Autumn Festival.

Today, on the topic of happiness, how to use three different programming ideas to put an elephant in a refrigerator.

To be a robot, computer programming is indispensable. After years of development, computer programming has evolved into three different and commonly used programming ideas, which are:

  1. Process oriented programming ideas. Abbreviations: POP
  2. Object-oriented programming ideas. Abbreviations: OOP
  3. The idea of a function. Abbreviations: FP

1.Why

Why do you know these programming ideas? OOP in particular.

When reading open-source code related to robots, such as Nav2, a navigation framework, and Moveit, a motion control framework for mechanical arms, Xiaoyu found that each line of other people’s code seemed to be understandable, but they could not understand it together. They were soon confused when they saw other people’s functions changing and changing. I don’t know how to do it.

This is actually caused by the lack of understanding of other people’s programming ideas, so in this class, Fish will give a mention of three common programming ideas, so that you have a concept in your mind, when you encounter a program that you don’t understand, you know which direction to go to learn.

Programming ideas broad and profound, small fish here three basic concepts.

2. Ideological analysis

First of all, what is programming for?

Is it to make money?

No, we write programs to solve real problems, and programming ideas are problem-solving ideas.

So what’s the difference between these three ideas?

2.1 Put an elephant in a refrigerator with three Thoughts

Let’s say we want to put an elephant in a refrigerator, and we’ll use three different ideas, and we’ll see what the difference is.

Put the elephant in the fridge

2.2 Process oriented thinking

If we take a process-oriented approach, there are three steps:

  1. Open the refrigerator door
  2. Shove the elephant in
  3. Close the refrigerator door

Process-oriented programming is analyzing the steps needed to solve a problem, implementing each step separately, and then executing it step by step.

2.3 Object-oriented thinking

How does object-oriented programming (OOP) work?

You have to know what object orientation is, right? Figure out what an object is?

Object-oriented programming

Is it a girlfriend? The answer is definitely not.

Anything we want to explore can be taken as an object, for example, we can understand your refrigerator as an object, we can study what parts your refrigerator is composed of (command device, etc.), what can your refrigerator do (refrigeration, temperature regulation, etc.)?

And then we started to define it, which is to give it a fancy name

The behavior of an object is actually an operation on its properties, such as refrigeration to cool, electrify the lamp strip to light.

Object = property + behavior

Then we started using OOP to put the elephant in the refrigerator

  1. Call: Refrigerator -> Open door (behavior)
  2. Call: Refrigerator -> Load things (behavior)
  3. Call: Refrigerator -> Close door (behavior)

It doesn’t look very different from process-oriented, but we have a major shift in our thinking. We treat the refrigerator as a separate object, and we do the whole process by interacting with the refrigerator.

2.4 Functional programming

Let’s move on to functional programming

  1. Define the close in (refrigerator, elephant) function
  2. Implementation function: Close the door (put in (open the door (refrigerator), elephant))

You can see layers of nested function calls, which is the beauty of function programming, because FP is not the focus of our robot learning, here is no more than the explanation!

3. Write at the end

Of the three programming ideas, the first two are enough for us robots. However, in the official example given by ROS2, even the third FP is given as an example. That’s a good thing, because as people become more disciplined, we’ll be able to reuse more and more of our code and implement more complex features together.

I am a small fish, I wish you a happy Mid-Autumn Festival ~