[Dragon master learning programming] Arduino hands-on (167)---MG996R metal gear steering gear

The idea of ​​37 sensors and modules has been widely circulated on the Internet. In fact, there must be more than 37 sensor modules compatible with Arduino. In view of the fact that I have accumulated some sensor and actuator modules on hand, according to the concept of true knowledge (must be hands-on), for the purpose of learning and communication, I am going to try and do more experiments one by one here, and I will record them whether I succeed or not.

[Arduino] 168 kinds of sensor module series experiments (data code + simulation programming + graphics programming)
Experiment 167: 180-degree single-axis steering gear MG996R servo motor 13KG smart car robot arm DIY gimbal accessories

insert image description here
insert image description here
Knowledge points: What is a steering gear?
Servo motors are often called servos, which are small devices with an output shaft. When we send a control signal to the servo, the output shaft can be turned to a specific position. The servo maintains the angular position of the shaft as long as the control signal remains constant. If the control signal changes, the position of the output shaft will change accordingly.

The steering gear is a position servo driver, which is mainly composed of a casing, a circuit board, a coreless motor, a gear and a position detector. Its working principle is that the receiver or single-chip microcomputer sends a signal to the steering gear, and there is a reference circuit inside, which generates a reference signal with a period of 20ms and a width of 1.5ms, and compares the obtained DC bias voltage with the voltage of the potentiometer to obtain a voltage difference output. The IC on the circuit board determines the direction of rotation, and then drives the coreless motor to start rotating. The power is transmitted to the swing arm through the reduction gear, and the position detector sends back a signal to determine whether it has reached the position. It is suitable for those control systems that require constant changes in angle and can be maintained. When the motor speed is constant, the potentiometer is driven to rotate through the cascaded reduction gear, so that the voltage difference is 0, and the motor stops rotating. Generally, the rotation angle range of the steering gear is 0° to 180°.

In daily life, steering gear is often used in remote control aircraft, remote control cars, robots and other fields. Servos are very useful in robotics. Because the servos have built-in control circuits, they are small in size but powerful enough. A standard servo like the Futaba S-148 provides 0.3 N/m of torque, which is plenty powerful for its size. At the same time, the energy consumed by the steering gear is proportional to the mechanical load. Therefore, a lightly loaded steering gear system will not consume too much power.

insert image description here
Working principle
The steering gear is equipped with a potentiometer (or other angle sensor) to detect the rotation angle of the output shaft, and the control board can control and maintain the angle of the output shaft more accurately according to the information of the potentiometer. Such a DC motor control method is called closed-loop control, so the steering gear is more accurately called a servo motor, English servo. The steering gear consists of: steering wheel, reduction gear, position feedback potentiometer, DC motor, control circuit board, etc. The control circuit board receives the control signal from the signal line to control the rotation of the motor, and the motor drives a series of gear sets, which are decelerated and then transmitted to the output steering wheel. The output shaft of the steering gear is connected to the position feedback potentiometer. When the steering wheel rotates, the position feedback potentiometer is driven. The potentiometer will output a voltage signal to the control circuit board for feedback. Then the control circuit board determines the direction and speed of the motor rotation according to the position, so as to achieve the target stop. Its working process is: control signal→control circuit board→motor rotation→gear set deceleration→rudder wheel rotation→position feedback potentiometer→control circuit board feedback.

insert image description here
The closed-loop detection mechanism of the steering gear
Regarding the precise position control of the steering gear, there is a closed-loop control mechanism as shown in the figure below. That is: the position detector (angle sensor) is its input sensor, and the position of the steering gear changes, and the resistance value of the position detector will change accordingly. By reading the resistance value through the control circuit, the speed and direction of the motor can be adjusted appropriately according to the resistance value, so that the motor can rotate to a specified angle. Thereby the precise rotation control of the steering gear is realized.

insert image description here
The control signal of the steering gear
is a pulse width modulation (PWM) signal with a period of 20ms, in which the pulse width is from 0.5ms to 2.5ms, corresponding to the position of the steering wheel from 0 to 180 degrees, and it changes linearly. That is to say, if it is provided with a certain pulse width, its output shaft will remain at a corresponding angle. No matter how the external torque changes, it will not change the output angle to a new corresponding position until it is provided with a pulse signal of another width. There is a reference circuit inside the steering gear, which generates a reference signal with a period of 20ms and a width of 1.5ms. There is a comparator that compares the external signal with the reference signal to determine the direction and magnitude, thereby generating the rotation signal of the motor. It can be seen that the steering gear is a position servo drive, and the rotation range cannot exceed 180 degrees. It is suitable for drives that require constant changes in angle and can be maintained. For example, the joints of robots, the rudder surfaces of aircraft, etc.

insert image description here
insert image description here
insert image description here
insert image description here
insert image description here
insert image description here
insert image description here
insert image description here
insert image description here
insert image description here
insert image description here
insert image description here
insert image description here
Wiring diagram of Arduino experiment

insert image description here
[Arduino] 168 sensor module series experiments (data code + simulation programming + graphics programming)

Experiment 167: 180-degree single-axis steering gear MG996R servo motor 13KG smart car robot mechanical arm DIY pan-tilt accessories

Project: Simple control steering gear MG966R

Arduino experiment open source code

/*

 【Arduino】168种传感器模块系列实验(资料代码+仿真编程+图形编程)

  项目:简单控制舵机MG966R

*/

#include <Servo.h>

Servo myservo;  // create servo object to control a servo

void setup() {
    
    

  myservo.attach(9, 600, 2300);  // (pin, min, max)

}

void loop() {
    
    

  myservo.write(0);  // tell servo to go to a particular angle

  delay(500);

  myservo.write(90);

  delay(500);

  myservo.write(180);

  delay(500);

  myservo.write(90);

  delay(500);

}


Arduino experiment scene diagram

insert image description here
How the Code Works
The first step is to include the required Arduino libraries. You can also find this library under "File > Examples > Servo".

insert image description here

// Include the servo library:
#include <Servo.h>

Next, you need to create a new object of the Servo class. In this case, the servo is called "myservo", but you can use other names. Note that you will also have to change the name of the server in the rest of the code.

// Create a new servo object:
Servo myservo;

After that, it is defined which Arduino pin the servo motor is connected to.

// Define the servo pin:
#define servoPin 9

This statement is used to name constant values. The compiler will replace any references to this constant with the defined value when compiling the program. So everywhere you mention it, the compiler will replace it with the value 9 when compiling the program.

#define
servoPin

This variable is used to store the current position of the servo in degrees.

angle
// Create a variable to store the servo position:
int angle = 0;

In the setup section of the code, we link the servo object we created to the pins that will control the servos. The function also has two optional parameters,

attach()
void setup() {
    
    
  // Attach the Servo variable to a pin:
  myservo.attach(servoPin);
}

Control Angle/Position:

In the first part of the loop we simply tell the servo to move to a specific angle using the function. Note that there needs to be a delay between commands to give the servo motor time to move to the set position.

write()
// Tell the servo to go to a particular angle:
  myservo.write(90);
  delay(1000);
  myservo.write(180);
  delay(1000);
  myservo.write(0);
  delay(1000);

Control speed:

In the last part of the code, I use two [for loops] to sweep the servo back and forth. This code is also useful if you want to control the speed of the servo motor. By changing the delay value at the end of the for loop, the speed of the servo arm can be adjusted.

// Sweep from 0 to 180 degrees:
  for (angle = 0; angle <= 180; angle += 1) {
    
    
    myservo.write(angle);
    delay(15);
  }
  // And back from 180 to 0 degrees:
  for (angle = 180; angle >= 0; angle -= 1) {
    
    
    myservo.write(angle);
    delay(30);
  }

The Arduino Servo library makes it very easy to adjust the minimum and maximum angle of the servo motor by specifying two optional parameters in the function. In this function, the first parameter is the pin number the servo is connected to. The second parameter is the pulse width, in microseconds (μs), corresponding to the minimum (0 degree) angle of the servo motor. The third parameter is the pulse width, in microseconds, corresponding to the maximum (180 degrees) angle of the servo motor.

attach()

By default, the minimum and maximum pulse width settings are 544 and 2400 microseconds. These values ​​work for the most common servos, but sometimes you have to tweak the values ​​a bit.

I recommend adjusting the min and max values ​​in small increments (10-20 microseconds) to avoid damaging the servo. If the servo arm reaches the physical limits of the motor, increase the minimum value, and decrease the maximum value.

#define servoPin 9
int min = 480;
int max = 2500;
Servo myservo;

void setup() {
    
    
  myservo.attach(servoPin, min, max);
}

Guess you like

Origin blog.csdn.net/weixin_41659040/article/details/131869885