Arduino and the steering gear

Position servo is a servo drive, it is mainly constituted by a housing, a circuit board, a coreless motor, a gear position detector. Its working principle is sent by the receiver to the servo signal or the microcontroller, which has an internal reference circuit, the generation period of 20ms, a width of 1.5ms signal, the DC bias voltage potentiometer voltage obtained in Comparative obtain a voltage difference output. Determining the rotational direction via the IC circuit board, and then drive coreless motor starts to rotate, the power transmitted through the reduction gear arm, while the return signal from the position detector, judges whether the positioning has been reached. For those who need to constantly change the angle of the control system and can be maintained. When the motor speed is constant, a reduction gear driven by cascading rotary potentiometer, such that the voltage difference is zero, the motor stops rotating. Steering angular range of rotation is generally 0 to 180 degrees.

Servo There are many specifications, but all the servos have three external lines are distinguished by brown, red, orange three colors, due to the different steering gear brands, colors will vary, brown ground wire, red for the positive power supply line, a signal line orange.
Here Insert Picture Description
Steering angle of rotation is achieved by adjusting the duty ratio of PWM (pulse width modulation) signal, the standard period of PWM (pulse width modulation) signal is fixed to 20ms (50Hz), in theory, to be distributed in 1ms pulse width between 2ms, but in fact it may be between 0.5ms to 2.5ms pulse width, the pulse width and the steering angle of 0 ° ~ 180 °, respectively. There are places worth noting, due to the different brands of servos, for the same signal, different brands of steering angle of rotation will be different.
Here Insert Picture Description
Arduino servo controlling method, there are two, one is produced by an ordinary digital sensor interface Arduino different duty cycle square wave, the PWM signal generating analog servo positioning, the second is the direct use Arduino comes Servo servo control functions, the advantages of this control method is that the programming, the disadvantage is only 2-way servo control, as a function of Arduino comes only with a digital interface to 9,10. Arduino limited drive capability, so the need for external power supply when it is necessary to control one or more servos.
Here Insert Picture Description
A method
servos 9 connected to the digital interface.
Servo write a program that lets the user rotated to the position of the input digital number corresponding to the angle, and the angle displayed on the screen printing.

Code:

int servopin=9;//设置舵机驱动脚到数字口9
int myangle;//定义角度变量
int pulsewidth;//定义脉宽变量
int val;
void servopulse(int servopin,int myangle)/*定义一个脉冲函数,用来模拟方式产生PWM值*/
{
  pulsewidth=(myangle*11)+500;//将角度转化为500-2480 的脉宽值
  digitalWrite(servopin,HIGH);//将舵机接口电平置高
  delayMicroseconds(pulsewidth);//延时脉宽值的微秒数
  digitalWrite(servopin,LOW);//将舵机接口电平置低
  delay(20-pulsewidth/1000);//延时周期内剩余时间
}
void setup()
{
  pinMode(servopin,OUTPUT);//设定舵机接口为输出接口
  Serial.begin(9600);//设置波特率为9600
  Serial.println("servo=o_seral_simple ready" ) ;
}
void loop()
{
  val=Serial.read();//读取串口收到的数据
  if(val>'0'&&val<='9')//判断收到数据值是否符合范围
  {
    val=val-'0';//将ASCII码转换成数值,例'9'-'0'=0x39-0x30=9
    val=val*(180/9);//将数字转化为角度,例9*(180/9)=180
    Serial.print("moving servo to ");
    Serial.print(val,DEC);
    Serial.println();
    for(int i=0;i<=50;i++) //产生PWM个数,等效延时以保证能转到响应角度
    {
      servopulse(servopin,val);//模拟产生PWM
    }
  }
}

Effect as the above code: 1 digital input, steering angle is 20 degrees, input 2, the servo 40 degrees, and so on, until the input 9, 180 until the servo output.

Code two

int servopin=9;//设置舵机驱动脚到数字口9
int myangle;//定义角度变量
int pulsewidth;//定义脉宽变量
int val;
void servopulse(int servopin,int myangle)/*定义一个脉冲函数,用来模拟方式产生PWM值*/
{
  pulsewidth=(myangle*11)+500;//将角度转化为500-2480 的脉宽值
  digitalWrite(servopin,HIGH);//将舵机接口电平置高
  delayMicroseconds(pulsewidth);//延时脉宽值的微秒数
  digitalWrite(servopin,LOW);//将舵机接口电平置低
  delay(20-pulsewidth/1000);//延时周期内剩余时间
}
void setup()
{
  pinMode(servopin,OUTPUT);//设定舵机接口为输出接口
  Serial.begin(9600);//设置波特率为9600
  Serial.println("servo=o_seral_simple ready" ) ;
}
void loop()
{
  val=Serial.read();//读取串口收到的数据
if(val>='0'&&val<='1') //判断收到数据值是否符合范围
{
val=val-'0'; //将ASCII码转换成数值,例'9'-'0'=0x39-0x30
val=val+1;
val=val*10*val*val;//将数字转化为角度
Serial.print("moving servo to ");
Serial.print(val,DEC);
Serial.println();
for(int i=0;i<=50;i++) //给予舵机足够的时间让它转到指定角度
{
servopulse(servopin,val);//引用脉冲函数
}
}
}

At this time, if the steering gear mounted to the gripper, the effect of the input opening claws 1, 0 input is closed claws.

Method two
first specific analysis Arduino Servo function and its own statement, to introduce a few common statement servo function of it.
1, attach (Interface) - set servo interface, only the number 9 or 10 interfaces can be used.
2, write (angle) - for setting the angular range of rotation angle servo statement, that can be set to 0 ° 180 °.
3, read () - used to read the angle servo statements, it is understood as the last read command write ()
values.
4, attached () - determining servo parameters have been sent to the interface where the steering gear.
5, detach () - separated from the steering gear to its interfaces, the interface (9 or 10 digital interface) may continue to be used as a PWM interface.
Note: The format of the above statements are "steering variable name specific statement ()." For example: myservo.attach (9).

Code:

#include <Servo.h>  // 声明调用Servo.h库
Servo myservo;   // 创建一个舵机对象
int pos = 0;    // 变量pos用来存储舵机位置
void setup() {
  // put your setup code here, to run once:
myservo.attach(9);    // 将引脚9上的舵机与声明的舵机对象连接起来
}

void loop() {
  // put your main code here, to run repeatedly:
for(pos = 0;pos < 180;pos +=1)     // 舵机从0°转到180°,每次增加1°
{
  myservo.write(pos);             // 给舵机写入角度
  delay(15); 
}
for (pos =180; pos >= 1;pos-=1)     // 舵机从180°转回到0°,每次减小1°
{
  myservo.write(pos);
  delay(15);
}
}

Guess you like

Origin blog.csdn.net/weixin_42913113/article/details/91958217