自制舵机-教具

作者:hardihuang   hardihuang   

一、概述 #


该装置为舵机演示教具,利用学生常见的普通直流减速电机,配合电机驱动板、arduino主控板和电位器等电子元件,摇身一变,成为了一个可以相对精确控制角度等MINI舵机。从中可以让学生更加彻底的明白舵机的工作原理,以及不同部件的功能。

二、结构细节 #


减速电机轴上通过联轴器,连接着一个电位器,arduino通过不断的读取用户转动的电位器和连接着电机的电位器,来获取差值,然后给电机驱动板信号,控制电机进行移动,缩小误差,从而控制电机上固定的指针运动到指定的位置,

三、视频演示 #


四、源码 #


//variables
short int valRead = 0;
short int motorPWM = 110; //amount of power motor (0-255) were 255 is max.

//constants
const short int wireMT_1 = 9; //for wire motor H bridge
const short int wireMT_2 = 10; //for wire motor H bridge
const short int potMT = A0; //motor potentiometer
const short int potIn = A1; //input potentiometer
const short int diff_error = 3; //max error to potentiometer motor
const short int minPot = 400;  //minimal position potentiometer motor (0-1023)
const short int maxPot = 1010; //max position potentiometer motor (0-1023)
const short int maxValIn = 1023; //max *input value. For this case 1023, the max of pot.
// *here can use something like degress, where max could be 180

//setup
void setup() {
  Serial.begin(9600);
  pinMode (wireMT_1, OUTPUT); //wire motor to H bridge
  pinMode (wireMT_2, OUTPUT); //wire motor to H bridge
  pinMode (potMT, INPUT); //motor potentiometer
  pinMode (potIn, INPUT); //input potentiometer
  digitalWrite (wireMT_1, LOW);
  digitalWrite (wireMT_2, LOW);
}

//run motor function
void runMotor(short int valTarget) {
  //proportionality between input and output value
  while (valRead <= valTarget && abs(valRead - valTarget) >  diff_error) {
    valRead = (((float)analogRead(potMT) - minPot) / (maxPot - minPot)) * maxValIn;
    //run motor
    analogWrite (wireMT_1, motorPWM);
    analogWrite (wireMT_2, 0);
  }
  analogWrite (wireMT_1, 0); //turn off motor
  
  //proportionality between input and output value
  while (valRead >= valTarget && abs(valRead - valTarget) >  diff_error) {
    valRead = (((float)analogRead(potMT) - minPot) / (maxPot - minPot)) * maxValIn;
    //run motor
    analogWrite (wireMT_2, motorPWM);
    analogWrite (wireMT_1, 0);
  }
  analogWrite (wireMT_2, 0); //turn off motor
}

//loop
void loop() {
  short int val = analogRead(potIn); //read input potentiometer
  runMotor(val); //run motor for target value
  //runMotor(1023-val); //use for invert direction motor
  Serial.print("motor: ");
  Serial.print(analogRead(A0));
  Serial.print("   input: ");
  Serial.print(analogRead(A1));
  Serial.println();
}

五、资料下载 #


「1」联轴器3D模型:diy-servo-connector.stl「2」仪表盘面板图纸:speedmeter.psd

发表回复

您的电子邮箱地址不会被公开。 必填项已用 * 标注