40
Lesson 4 Controlling the Servo Motor In this lesson, we will learn how to control the Servo Motor. 4.1 Components used in this course Components Quantity Picture AdeeptPixie Drive Board 1 Micro USB Cable 1 Servo 1 4.2 The introduction of the Servo Motor 4.2.1 Servo Motor Servo motor refers to the engine that controls mechanical component operation in the servo system. It is a kind of auxiliary motor indirect transmission device. The servo motor is a gear motor that can rotate only 180 degrees. It is controlled by sending pulses from the microcontroller. These pulses tell the server where to move. The servo motor system includes housing, circuit board, non-core motor, gearing and position detection. Servo motor is shown in the figure:

Lesson 4 Controlling the Servo Motor

  • Upload
    others

  • View
    1

  • Download
    0

Embed Size (px)

Citation preview

Page 1: Lesson 4 Controlling the Servo Motor

Lesson 4 Controlling the Servo Motor

In this lesson, we will learn how to control the Servo Motor.

4.1 Components used in this course

Components Quantity Picture

AdeeptPixie Drive Board 1

Micro USB Cable 1

Servo 1

4.2 The introduction of the Servo Motor

4.2.1 Servo Motor

Servo motor refers to the engine that controls mechanical component operation in

the servo system. It is a kind of auxiliary motor indirect transmission device. The

servo motor is a gear motor that can rotate only 180 degrees. It is controlled by

sending pulses from the microcontroller. These pulses tell the server where to move.

The servo motor system includes housing, circuit board, non-core motor, gearing and

position detection. Servo motor is shown in the figure:

Page 2: Lesson 4 Controlling the Servo Motor

4.2.2 The working principle of the Servo Motor

The servo mechanism is an automatic control system that enables the object's

position, orientation, state and other output controlled quantities to follow arbitrary

changes in the input target (or given value). The servo mainly depends on Pulsefor

location. Basically, it can be understood that the servo motor receives an impulse and

rotates the angle corresponding to the impulse to realize displacement. Because the

servo motor itself has the function of sending out pulses, the servo motor rotates every

time at an angle, and a corresponding number of pulses will be sent out. In this way,

the pulses received by the servo motor form a response, or a closed loop. In this way,

the system will know how many pulses are sent to the servo motor and how many

pulses are received. In this way, it is possible to precisely control the rotation of the

motor, thereby achieving precise positioning

The Drive Board sends the PWM signal to Servo, and then the IC on the circuit

board processes the signal to calculate the rotation direction of the drive motor, and

then transmits the drive force to the swing arm with the reduction gear. At the same

time, the position detector returns a position signal to determine whether it has

reached the set position.

Page 3: Lesson 4 Controlling the Servo Motor

2.2.3 Principle of write() function

In the program, we use the write() function to control the rotation of the servo.

For standard servos, the write() function will rotate the servo axis to the corresponding

angular position. For the continuous rotation type of servo, the write() function can set

the rotation speed of the servo (0 indicates that the servo rotates at full speed in one

direction, 180 indicates that the servo rotates at full speed in the other direction, and

90 indicates that the servo is stationary. The servo used this time is a standard servo)

attach() is the port for setting the servo.

Page 4: Lesson 4 Controlling the Servo Motor

4.3 Wiring diagram (Circuit diagram)

In this lesson, we use AD002 servo to connect the AdeeptPixie driver board, only

need to connect the AD002 servo cable to the Servo interface of the AdeeptPixie

driver board as shown below:

4.4 How to control Servo

4.4.1Compile and run the code program of this course

1.Open the Arduino IDE software, as shown below:

Page 5: Lesson 4 Controlling the Servo Motor

2. In the Tools toolbar, find Board and select Arduino Uno, as shown below:

3. In the Tools toolbar, find Port, select the port number of the AdeeptPixie driver

board, as shown below:

4. Click Open under the File drop-down menu:

Page 6: Lesson 4 Controlling the Servo Motor

5. Find the folder Hexapod 6 Legs Spider Robot Kit for Arduino\03Course code

we provide for users, open the Lesson4_Servo folder, select Servo.ino, this file is the

code program we need to use in this lesson, and then click Open.

6. After opening, click to Upload the code program to the driver board, the

following error message may appear:

Page 7: Lesson 4 Controlling the Servo Motor

【Solution】

Find the directory where you installed the Arduino IDE, open the hardware folder,

you need to delete the "hardware" inside.

Then you click again to upload the code program

the upload is successful, a text prompt appears in the

uploading.

to the driver board. After

lower left corner: Done

7. After successfully running the program, you will observe the movement of the

servo.

Page 8: Lesson 4 Controlling the Servo Motor

Lesson 5 Using WS2812 to Make Lights

In this lesson, we will learn how to use the WS2812 RGB LED module to make

colorful lights that can change various colors.

5.1 Components used in this course

Components Quantity Picture

AdeeptPixie Drive Board 1

Micro USB Cable 1

3 pin jumper wire 1

WS2812 1

5.2 About WS2812 RGB LED module

5.2.1 WS2812 RGB LED module

WS2812 RGB LED module is a low-power RGB tri-color lamp integrated with a

current control chip. Its appearance is the same as a 5050LED lamp bead, and each

element is a pixel. The pixel contains an intelligent digital interface data latch signal

shaping and amplifying drive circuit, as well as a high-precision internal oscillator and

a 12V high-voltage programmable constant current control part, which effectively

ensures that the color of the pixel is highly consistent.

Page 9: Lesson 4 Controlling the Servo Motor

5.2.2 Working principle of WS2812 RGB LED module

WS2812 RGB LED module is a low-power RGB tri-color lamp integrated with a

current control chip. Its appearance is the same as a 5050LED lamp bead, and each

element is a pixel. The pixel contains an intelligent digital interface data latch signal

shaping and amplifying drive circuit, as well as a high-precision internal oscillator and

a 12V high-voltage programmable constant current control part, which effectively

ensures that the color of the pixel is highly consistent. The data protocol adopts a

single-wire return-to-zero code communication method. After the pixel is power-on

reset, the DIN terminal accepts the data transmitted from the controller. The first 24bit

data sent is extracted by the first pixel and sent to the pixel. After the remaining data is

reshaped and amplified by the internal shaping processing circuit, the remaining data

is forwarded and output to the next cascaded pixel with the DO port. After each pixel

is transmitted, the signal is reduced by 24bit. The pixel adopts automatic shaping and

forwarding technology, so that the number of cascaded pixels is not limited by signal

transmission, only limited by the signal transmission speed requirement.

5.2.3 setPixelColor (i, color) method

The first parameter of setPixelColor(i,color) is the number of the LED in

WS2812, the number closest to the MCU pin is 0; color describes the color of the

pixel, representing the brightness levels of red, green and blue respectively, and 0 is

the darkest. 255 is the maximum brightness; we need to use strip.Color(R,G,B) to

Page 10: Lesson 4 Controlling the Servo Motor

convert the color into an integer value.

5.3 Wiring diagram (circuit diagram)

Before the experiment, we connected the components used in this lesson to the

circuit as shown in the figure below. Connect the WS2812 RGB module to the

WS2812 port on the AdeeptPixie Drive Board, as shown in the figure below:

5.4 How to control WS2812 RGB LED module

We use C language to program and control the WS2812 RGB LED module on the

AdeeptPixie Drive Board with the Arduino IDE. You need to master the C language.

Let’s learn how to control the WS2812 RGB LED module.

5.4.1 Running the code program of this lesson

1. You need to use Micro USB Cable to connect AdeeptPixie Drive Board to your

Page 11: Lesson 4 Controlling the Servo Motor

computer, and then open the Arduino IDE, as shown below:

2. In the Tools toolbar, find Board and select Arduino Uno, as shown below:

4. Click "Tools" and select the port number of the connected AdeeptPixie Drive

Board in "Port": COM5, as shown in the figure below:

Page 12: Lesson 4 Controlling the Servo Motor

6. You need to open the code program of this lesson. In the File in the upper left

corner, click Open, as shown below:

7. Find the file information provided by Adeept: Hexapod 6 Legs Spider Robot Kit

for Arduino\03Course code, open the Lesson5_WS2812 folder, select WS2812.ino, this

file is the code program we need to use in this lesson, and then click Open.

Page 13: Lesson 4 Controlling the Servo Motor

6. After successfully opening the file, you need to click the button to upload

the code program to the AdeeptPixie Drive Board. After the upload is successful, the

console will not appear a red warning, and the prompt text "Done uploading" appears

in the upper left corner, as shown in the figure below:

Page 14: Lesson 4 Controlling the Servo Motor

8. After successfully uploading the program, pay attention to observe whether the

color of the WS2812RGB LED module changes and flashes with different colors,

indicating that the experimental test is successful.

5.4.2 Main code program

After the above practical operation, you must be very curious to know how we

use the C language to program and control the WS2812 RGB LED module on the

AdeeptPixie Drive Board. Below we will introduce how the main code program is

implemented.

In the setup() function, initialize the WS2812 RGB LED module with

strip.begin(), and set the brightness of the WS2812 RGB LED module with the

strip.setBrightness(50) function.

In the loop() function, use the random() function to randomly set the R, G, B

values of the WS2812 RGB LED module, and use the for loop to light up the WS2812

RGB LED module, which has 6 LEDs. The first parameter of the

setPixelColor(i,color) function is the number of the LED in the WS2812, the number

closest to the MCU pin is 0; color represents the color of the pixel, which represents

the brightness level of red, green and blue respectively, and 0 is the darkest , 255 is the

maximum brightness; use strip.Color(R,G,B) to convert the color into a value.

Page 15: Lesson 4 Controlling the Servo Motor

Lesson 6 Reading the Value of the Ultrasonic

Distance Sensor

In this lesson, we will learn how to read the distance of an ultrasonic distance

sensor.

6.1 Components used in this course

Components Quantity Picture

AdeeptPixie Drive Board 1

Micro USB Cable 1

3 pin jumper wire 1

Ultrasonic Distance Sensor 1

6.2 The application of the Ultrasonic Distance Sensor

6.2.1 Ultrasonic Distance Sensor

The model of the ultrasonic distance sensor we use is hc-sr04, it is mainly

composed of two left and right probes, looking like our human eyes. One probe is

responsible for transmitting sound waves for detection, and the other probe is

responsible for receiving sound waves for return. It has 4 pins, which are VCC; Trig

(control end - trigger signal input); Echo (receiver - recovery signal output);

Gnd(ground).

Page 16: Lesson 4 Controlling the Servo Motor

6.2.2 The working principle of the Ultrasonic Distance Sensor

The method of detecting distance of ultrasonic wave is called echo detection

method,which ultrasonic emitter emits to a certain direction, in the moment of timer

timing starts at the same time, the ultrasonic wave in air, run into obstacles on the way

your face (objects) block was reflected immediately, ultrasonic receiver received the

ultrasonic reflected back to immediately stop timing. The propagation speed of

ultrasonic wave in the air is 340m/s. According to the time t recorded by the timer, the

distance s from the launch point to the obstacle surface can be calculated, that is,

s=340t/2. Under this principle of ultrasound, ultrasonic ranging module is widely used

in practical applications, such as car reversing radar, uav, and intelligent car.

Page 17: Lesson 4 Controlling the Servo Motor

6.3 Wiring diagram (Circuit diagram)

Before the experiment, we connected the components used in this lesson to the

circuit as shown in the figure below. The Ultrasonic Distance Sensor module was

connected to the Ultrasonic port on the AdeeptPixie Drive Board, as shown in the

figure below:

6.4 How to read the value of the ultrasonic distance

sensor

We use the Arduino IDE to use the C language to program and read the value of

the ultrasonic distance sensor on the AdeeptPixie Drive Board. You need to master the

C language. Let's learn how to read the value of the ultrasonic distance sensor.。

Page 18: Lesson 4 Controlling the Servo Motor

6.4.1 Running the code program of this lesson

5. You need to use Micro USB Cable to connect AdeeptPixie Drive Board to your

computer, and then open the Arduino IDE, as shown below:

2. In the Tools toolbar, find Board and select Arduino Uno, as shown below:

7. Click "Tools" and select the port number of the connected AdeeptPixie Drive

Board in "Port": COM5, as shown in the figure below:

Page 19: Lesson 4 Controlling the Servo Motor

8. You need to open the code program of this lesson. In the File in the upper left

corner, click Open, as shown below:

9. Find the file information provided by Adeept: Hexapod 6 Legs Spider Robot

Kit for Arduino\03Course code, open the Lesson6_ultrasonic folder, select

ultrasonic.ino, this file is the code program we need to use in this lesson, and then

click Open.

Page 20: Lesson 4 Controlling the Servo Motor

6. After successfully opening the file, you need to click the button to upload

the code program to the AdeeptPixie Drive Board. After the upload is successful, the

console will not appear a red warning, and the prompt text "Done uploading" appears

in the upper left corner, as shown in the figure below:

Page 21: Lesson 4 Controlling the Servo Motor

7. After successfully running the program, we need to open the serial monitor on

the Arduino IDE and observe the acquired data with the serial monitor. How to open

the serial monitor? You need to click the "Serial Monitor" button in the upper

right corner, as shown below:

Page 22: Lesson 4 Controlling the Servo Motor

8. After clicking , the serial monitor window will pop up, and the obstacle

distance data detected by Ultrasonic Sensor will be observed. Note that 9600 baud is

selected, as shown below:

6.4.2 Main code program

After the above practical operation, you must be very curious to know how we

use C language to program and read Ultrasonic Sensor module data on AdeeptPixie

Drive Board. Below we will introduce how the main code program is implemented.

In the setup() function, initialize the serial monitor with Serial.begin(9600).

In the loop() function, the ping(pingPin) function reads the data of the Ultrasonic

Sensor module, and prints out the acquired data to the serial monitor with

Serial.println(). The detailed calculation process can be viewed in the ping (pingPin)

function in the source code.

Lesson 7 Reading the Data of MPU6050

Page 23: Lesson 4 Controlling the Servo Motor

In this lesson, we will learn how to read the data of MPU6050.

7.1 Components used in this course

Components Quantity Picture

AdeeptPixie Drive Board 1

Micro USB Cable 1

3 pin jumper wire 1

MPU6050 1

7.2 About MPU6050

7.2.1 Introduction of MPU6050

MPU-6050 is the world's first integrated 6-axis MotionTracking device. It

integrates a 3-axis MEMS gyroscope, a 3-axis MEMS accelerometer, and an

expandable digital motion processor DMP (Digital Motion Processor), which can be

connected to a third-party digital sensor, such as a magnetometer, with an I2C

interface. After expansion, it can output a 9-axis signal through its I2C or SPI

interface (SPI interface is only available in MPU-6000). MPU-60X0 can also be

connected to non-inertial digital sensors, such as pressure sensors, by its I2C interface.

MPU-6050 uses three 16-bit ADCs for the gyroscope and accelerometer,

respectively, to convert the measured analog quantity into an output digital quantity.

Page 24: Lesson 4 Controlling the Servo Motor

The gyroscope can measure angular velocity, and the accelerometer can measure

acceleration. In order to accurately track fast and slow motion, the measuring range of

the sensor is controllable, the gyroscope can measure the range of ±250, ±500, ±1000,

±2000°/sec (dps), and the accelerometer can measure the range of ±2 , ±4, ±8, ±16g

(gravitational acceleration).

An on-chip 1024-byte FIFO helps reduce system power consumption. The

communication with all device registers uses a 400kHz I2C interface. In addition, a

temperature sensor and an oscillator with a ±1% variation in the working environment

are embedded on the chip. And there is a programmable low-pass filter.

Regarding the power supply, the MPU-6050 module can support the working

voltage range of VCC: 3~5VDC.

7.3 Wiring diagram (circuit diagram)

Connect the components used in this lesson to the circuit as shown in the figure

below. Connect the MPU6050 module to the MPU6050 port on the AdeeptPixie Drive

Board. Pay attention to the corresponding pin numbers, as shown in the figure below:

Page 25: Lesson 4 Controlling the Servo Motor

7.4 How to read the data of MPU6050

We use the Arduino IDE to use the C language to program and read the value of

the super MPU6050 sensor on the AdeeptPixie Drive Board. You need to master the

C language. Let's learn how to read the value of the MPU6050 sensor.

7.4.1 Running the code program of this lesson

6. You need to use Micro USB Cable to connect AdeeptPixie Drive Board to your

computer, and then open the Arduino IDE, as shown below:

Page 26: Lesson 4 Controlling the Servo Motor

2. In the Tools toolbar, find Board and select Arduino Uno, as shown below:

8. Click "Tools" and select the port number of the connected AdeeptPixie Drive

Board in "Port": COM5, as shown in the figure below:

Page 27: Lesson 4 Controlling the Servo Motor

9. You need to open the code program of this lesson. In the File in the upper left

corner, click Open, as shown below:

10. Find the file information provided by Adeept: Hexapod 6 Legs Spider

Robot Kit for Arduino\03Course code, open the Lesson7_MPU6050 folder, select

MPU6050.ino, this file is the code program we need to use in this lesson, and then

click Open.

Page 28: Lesson 4 Controlling the Servo Motor

6. After successfully opening the file, you need to click the button to upload

the code program to the AdeeptPixie Drive Board. After the upload is successful, the

console will not appear a red warning, and the prompt text "Done uploading" appears

in the upper left corner, as shown in the figure below:

Page 29: Lesson 4 Controlling the Servo Motor

8. After successfully running the program, we need to open the serial monitor on

the Arduino IDE and observe the acquired data with the serial monitor. How to open

the serial monitor? You need to click the "Serial Monitor" button in the upper

right corner, as shown below:

8. After clicking , the serial monitor window will pop up, and you will

observe the data of MPU6050. The printout is the value of X, Y, Z axis. You can

swing the MPU6050 sensor and these data will change. Pay attention to choose

115200 baud ,As shown below:

Page 30: Lesson 4 Controlling the Servo Motor

7.4.2 Main code program

After the above practical operation, you must be wondering how we use C

language to program and read the data of MPU6050 sensor on the AdeeptPixie Drive

Board. Below we will introduce how the main code program is implemented.

In the setup() function, use Wire.beginTransmission(MPU_addr) to turn on the

data transmission of MPU6050, use Serial.begin(115200) to turn on the serial monitor,

use the getMotion6() function in the for loop to read the six-axis raw data of

MPU6050, and perform a cumulative calculation on the data. After the For loop,

average the data to obtain a standard value.

Lesson 8 Remotely controlling the Servo with

Page 31: Lesson 4 Controlling the Servo Motor

ESP8226

In this lesson, we will learn how to use the ESP8266 module to establish a

wireless connection and remotely control the servo.

8.1 Components used in this course

Components Quantity Picture

AdeeptPixie Drive Board 1

Micro USB Cable 1

AD002 Servo 1

ESP8266 1

8.2 Introduction of ESP8266 Module

8.2.1 Introduction to ESP8266

ESP8266 is an ultra-low-power UART-WiFi transparent transmission module. It

has a very competitive package size and ultra-low energy consumption technology in

the industry. It is specially designed for mobile devices and IoT applications. The

user's physical device can be connected to the Wi-Fi wireless network for Internet or

local area network communication to achieve networking functions. ESP8266 can be

widely used in smart grid, smart transportation, smart furniture, handheld devices,

industrial control and other fields.

Page 32: Lesson 4 Controlling the Servo Motor

8.2.2 Main functions of ESP82668.

The main functions that ESP8266 can implement include: serial port transparent

transmission, PWM control, and GPIO control. Serial port transparent transmission:

data transmission, transmission reliability is good, the maximum transmission rate is:

460800bps.

PWM control: light control, three-color LED control, motor speed control, etc.

GPIO control: controlling switches, relays, etc.

8.2.3 ESP8266 working mode

The ESP8266 module supports three working modes: STA/AP/STA+AP.

7. STA mode: The ESP8266 module connects to the Internet by a router, and the

mobile phone or computer realizes remote control of the device with the Internet.

8. AP mode: The ESP8266 module is used as a hotspot to realize the direct

communication between the mobile phone or computer and the module, and realize

the wireless control of the LAN.

9. STA+AP mode: the coexistence mode of the two modes, which can realize

seamless switching with Internet control, which is convenient for operation.

8.3 Wiring diagram (circuit diagram)

Connect the components used in this lesson to the circuit as shown in the figure

below. Connect the ESP8266 module to the wifi port on the AdeeptPixie Drive Board.

Pay attention to the corresponding pin numbers, as shown in the figure below

Page 33: Lesson 4 Controlling the Servo Motor

8.4 Learn the code program of ESP8266.ino

Let's first learn how to establish a wireless connection based on ESP8266 in the

sample program provided in this course.

9. You need to use Micro USB Cable to connect AdeeptPixie Drive Board to your

computer, and then open the Arduino IDE, as shown below:

Page 34: Lesson 4 Controlling the Servo Motor

2. In the Tools toolbar, find Board and select Arduino Uno, as shown below:

10. Click "Tools" and select the port number of the connected AdeeptPixie

Drive Board in "Port": COM5, as shown in the figure below:

Page 35: Lesson 4 Controlling the Servo Motor

11. You need to open the code program of this lesson. In the File in the upper

left corner, click Open, as shown below:

9. Find the file information provided by Adeept: Hexapod 6 Legs Spider Robot

Kit for Arduino\03Course code, open the Lesson8_ESP8266 folder, select

ESP8266.ino, this file is the code program we need to use in this lesson, and then

click Open.

Page 36: Lesson 4 Controlling the Servo Motor

6. After opening, you will see the code inside. The following is an explanation of

the code:

The most core code program is in the loop() function.

Use the endsWith() function in the judgement() function to judge whether the

received string is followed by the keywords we need, because the string sent through

TCP communication is in this format: "\r\n+IPD, 0,1:forwardStart\n", the final string

forwardStart\n is what we want. So we use endsWith() to get and make a judgment,

judge which button is clicked, and then execute the program to make the servo move.

Page 37: Lesson 4 Controlling the Servo Motor

8.5 Remotely controlling servo

1. In the Arduino IDE of the opened ESP8266.ino, you need to turn the "Upload 0

RUN 1" switch on the AdeeptPixie Drive Board to the 0 position. Then upload this

program to AdeeptPixie Drive Board, as shown below:

2. Click the button to upload the ESP8266.ino program to the AdeeptPixie

Drive Board. If the upload is successful, there will be no red warning on the console,

and the prompt text "Done uploading" appears in the upper left corner, as shown

below:

Page 38: Lesson 4 Controlling the Servo Motor

3. After the download is complete, you also need to turn the "Upload 0 RUN 1"

switch to the 1 position.

4. After the upload is successful, the ESP8266 module will generate a Wifi

hotspot by default. You can check the name with "ESP" in the WIFI list. This hotspot

is generated by ESP8266.

5. Now you need to connect to the WIFI hotspot generated by the ESP8266

module with your computer. The initial password of this hotspot is 12345678.

【Pay attention】

Page 39: Lesson 4 Controlling the Servo Motor

1. If you are using a desktop computer, you need to purchase an additional USB

wireless network card device. With this device, the desktop computer can be

connected to the WIFI hotspot of the ESP8266 module.

6. Find the file information provided by Adeept for users: Hexapod 6 Legs Spider

Robot Kit for Arduino\03Course code, open the Lesson8_ESP8266 folder, double-

click to open GUI info v1.0.py (the computer needs to be installed with Python

runtime environment), as shown in the figure below:

7. You need to enter the default IP address of the ESP8266 module in the "IP

Address" input field: 192.168.4.1, this IP address is universal in the world, and it is

generated by the ESP8266 module by default. Click the Connect button, after the

connection is successful, the interface will display a green "Connect button".

Page 40: Lesson 4 Controlling the Servo Motor

8. By clicking the "Forward" and "Backward" buttons in the GUI interface, the

servo can be controlled. In Lesson 12, we will teach you in detail how to use this GUI

application to control the robot.