Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to use Raspberry Pi hardware and Python language to complete the manufacture of a robot

2025-02-27 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)06/01 Report--

In this issue, the editor will bring you about how to use Raspberry Pi hardware and Python language to complete a robot production. The article is rich in content and analyzes and describes for you from a professional point of view. I hope you can get something after reading this article.

The theme of my production is to use Raspberry Pi hardware and Python language to complete the production of a robot, then listen to me come here!

Hardware building

In this production, I chose an AS-4WD aluminum alloy car platform, based on the car platform, adding 7-inch HD LCD, wireless keyboard, Bluetooth module and motor drivers and other accessories, such as figure 1 is a list of the materials used to make the robot. The whole raspberry pie car system is built in two steps. The first is to build a raspberry pie computer system. Although it is a pocket computer, "although the sparrow is small, it has all the internal organs." in addition to the raspberry pie motherboard, it is also necessary to prepare a set of standard general-purpose USB keyboard and mouse, a display (described in this article is a 7-inch display for vehicle monitoring equipment, connected through the RCA interface). A battery is used for the power supply of the whole system, and finally, the most important thing is to prepare an SD card pre-installed with the Debian system (for SD cards, it is better to read and write above 4MB/S, the capacity is greater than 2GB, of course, the larger the capacity, the faster the better. After the completion of the construction of the computer system, the next step is to complete the construction of the robot system. In principle, the GPIO function of the two rows of external pins of the raspberry pie is mainly used to control the external raspberry pie driver (Raspi Driver) to realize the power and forward and reverse control of the motor, as well as the use of UART function to realize data communication with the Bluetooth data transmission module, so that the car can be controlled through the Bluetooth remote control of the mobile phone. Figure 2 shows a beautiful picture of the overall hardware, and the hardware wiring diagram of the raspberry pie robot is shown in figure 3.

Fig. 1 Materials used by robots

Figure 2-1 walking raspberry pie computer

Fig. 3 hardware wiring diagram of raspberry pie robot

The configuration of Python Library of Raspberry Pie

When using raspberry pie, it was also the first time for me to come into contact with the language Python. Through the study of relevant materials, I found that Python is an easy language to learn. If you have C or other computer language foundation, you can basically start writing programs in half a day. Before you start writing car control programs, you need to install and set up the relevant Python library files of our raspberry pie computer. First, GPIO, open the LX terminal (LXTerminal). Update the apt-get software installation package list (note that the network connection must be normal), and then execute the installation command to install the raspberry-gpio-python package, as follows:

Pi@raspberrypi ~ $sudo apt-get update

Pi@raspberrypi ~ $sudo apt-get install python-rpi.gpio

After installing the GPIO library of Python, the next step is to install the UART library of Python. Similar to the previous steps, update the list of apt-get software installation packages, and then install the serial communication module of Python. The specific instructions are as follows:

Pi@raspberrypi ~ $sudo apt-get update

Pi@raspberrypi ~ $sudo apt-get install python-serial

Through the above two steps, Python and raspberry external hardware GPIO and UART library files have been installed, and the code can be called directly in the next car control program. Before writing the control program, some parameters of the default serial port need to be changed. Because the system default serial port function is used to output kernel logs, the related parameters are different from our external serial port devices. So you need to make changes to its startup configuration file, enter / boot/cmdline.txt by typing "sudo nano / boot/cmdline.txt" in LXTerminal, open the cmdline.txt file with a vi editor, and set the

Dwc_otg.lpm_enable=0 console=ttyAMA0,115200 kgdboc=ttyAMA0,115200 console=tty1 root=/dev/mmcblk0p2 rootfstype=ext4 elevator=deadline rootwait

Remove

Console=ttyAMA0,115200 kgdboc=ttyAMA0,115200

When you exit the vi editor, be careful to save the file and edit the system initialization file. In LXTerminal, type "sudo nano / etc/inittab", and then find the following snippet

# Spawn a getty on Raspberry Pi serial line

T0:23:respawn:/sbin/getty-L ttyAMA0 115200 vt100

Change it to the following, comment out the parameters for the "ttyAMA0" port. When you exit the vi editor, you also need to pay attention to saving the file.

# Spawn a getty on Raspberry Pi serial line

# T0:23:respawn:/sbin/getty-L ttyAMA0 115200 vt100

Restart the raspberry pie, and the configuration will take effect. After completing the above steps, you can move on to the next chapter, the robot debugging process.

Python GPIO debug of ready-made raspberry pie

As the first time to use raspberry pie external IO may not be confused, how to use the language on the computer to make your computer's IO beat! In fact, it is not complicated, just open the IDLE3 editor on the desktop of the system and go in four steps

Step1: import the GPIO library, type "import RPi.GPIO as GPIO" in the edit line, and press "enter" to execute.

Step2: set the GPIO pin to use the label mode. If you select the label on the board, type "GPIO.setmode (GPIO.BOARD)" in the editor. If you use the label mode of the chip itself, just type "GPIO.setmode (GPIO.BCM)".

Step3: set the mode of the corresponding GPIO. If you use its output function "GPIO.setup (pin_number,GPIO.OUT)", you can use the input function by changing GPIO.OUT to GPIO.IN.

Step4: in the output mode, make the level of the corresponding pin high or low, and in the input mode, just read the level of the corresponding pin. If you have an understanding of the above four steps, then give it a try. I will take the operation of extinguishing LED1 and lighting LED2 on RasPi Driver as an example, giving the test code and the actual photo of the experiment (figure 4). If you can also achieve the above steps, congratulations, you have mastered the use of GPIO on raspberry pie.

Import RPi.GPIO as GPIO

# gpio init

GPIO.setmode (GPIO.BCM)

GPIO.setup (7core GPIO. Out) # LED2

GPIO.setup (8core GPIO. Out) # LED1

GPIO.output (7core GPIO. Low) # LED2 ON

GPIO.output (8th GPIO. High) # LED1 OFF

Fig. 4 Experimental screenshot

Python UART debug of ready-made raspberry pie

In fact, the method and steps for implementing the UART function of raspberry pie are similar to the use of GPIO above, which is divided into four steps:

Step1: import serial port library, type "import serial"

Step2: initialize the serial port, which is set here to the external Bluetooth matching parameters, BUAD=9600,timeout = 0.5. type "ser = serial.Serial ('/ dev/ttyAMA0', 9600 timeout = 0.5)" accordingly.

Step3: open the enable serial port, "if ser.isOpen () = = False:ser.open ()"

Import serial

Import time

Ser = serial.Serial ('/ dev/ttyAMA0', 9600, timeout = 0.5)

While True:

If ser.isOpen () = = False:

Ser.open ()

Print ser.read ()

Ser.write ('A')

Time.sleep (1)

Step4: use "ser.read ()" when reading data and "ser.write" when sending data. Here I edit a Python program Serial_test.py through IDLE3, and then type "sudo python Serial_test.py" directly in LXTerminal (note that because the account name: pi is used by default, the file needs to be placed in the / home/pi directory before it can be executed directly), and then the Bluetooth remote control (figure 5) is connected to the Bluetooth transparent transmission module, and after successful communication The corresponding characters can be sent through the key buttons of the mobile phone remote controller and printed to the screen on the serial port. Here I give the source program of the test (as follows). The Bluetooth virtual output serial port on the computer side is connected with the Bluetooth transparent serial port module connected to the raspberry pie for data transmission, and the serial port assistant on the computer side sends the letter "B". At the same time, the letter "A" sent by the raspberry pie is received and displayed in the debugging receiving window, and the raspberry terminal receives the letter "B" sent by the computer and prints it out. Through this phenomenon, it can be proved that the UART function test of raspberry pie is normal, as shown in screenshot 6 of the experiment.

Fig. 5 Bluetooth remote control for mobile phone

Figure 6 Test photos

Robot Control of Raspberry Pie

The control of the AS-4WD car is relatively simple. In this production, the special motor drive board for RasPi, the motor drive circuit with L293 as the core, and the positive and reverse rotation and enabling of the motor are realized through two groups of 2 IO.

Through the above pin arrangement, it can be clearly seen that the connection between the raspberry external hardware and RasPi Driver, the steering and enabling of one of the motors are controlled by GPIO4 and GPIO17 (high-level effective), and GPIO8 is used to indicate the status of forward and reverse rotation. Similarly, it can be seen that GPIO25 is used for forward and reverse control, GPIO10 is enabled, and GPIO7 status indication. At the same time, the external UART interface on the board is used to connect with the Bluetooth serial port module.

The whole program framework is relatively simple compared with the previous single-chip microcomputer version of the remote control car, which is mainly divided into functional modules initialization setting, circular judgment of remote control values and output corresponding functional motion values, as detailed in system control principle diagram 8. Import library files, GPIO and serial port configuration, the specific parameters are the same as the above, do not repeat After completing the above settings, it is the whole program to control the car, read the value of the serial buffer, and then cycle to judge the character data "A", "B", "C" and "D" sent by the Bluetooth remote control of the mobile phone. corresponding to the corresponding movement (note: in the corresponding action, the high and low level may not correspond to the preset action due to the wiring of the drive board motor. The wiring can be adjusted flexibly or the level can be modified by software.

Fig. 8 block diagram of system control principle

The above is the editor for you to share how to use Raspberry Pi hardware and Python language to complete a robot production, if you happen to have similar doubts, please refer to the above analysis to understand. If you want to know more about it, you are welcome to follow the industry information channel.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report