国外课栈 - 国外电子信息技术视频教程、电子书和博文栈

MeArm机器人操纵杆控制器 - 记录坐标

 二维码 73
发表时间:2019-01-09 16:51



使用2个模拟操纵杆控制4个伺服系统,无需使用额外的板。有3种模式:手动,坐标记录和自动运行。


此项目使用的东西


硬件组件


2,409Arduino UNO&Genuino UNO x 1

模拟操纵杆x 2

电阻器10k ohm x 2

电阻器680 ohm x 1

5 mm LED:红色x 1

微型伺服SG90 x 4


软件应用程序和在线服务

Arduino IDE


故事介绍

Arduino Uno操纵杆控制器,用于机器人手臂(MeArm®),带坐标记录。允许在按钮单击或重复时重复记录坐标。使用Arduino Uno基板而无需额外的板。需要USB连接(2A)或6V / 2A电源。使用四个伺服系统SG90进行测试。

按下操纵杆1上的按钮开始记录坐标。按下操纵杆1上的按钮结束坐标记录。

按下操纵杆2上的按钮开始重放记录的坐标。按下操纵杆2上的按钮停止重放记录的坐标。按住操纵杆2上的按钮,以循环方式自动开始重放记录的坐标。

原理图


代码

/* meArm analog joysticks version 1.3.1 - UtilStudio.com Dec 2018
   Uses two analogue joysticks and four servos.

   In version 1.3 was improved recording of coordinates.
   Some bugs was removed.

   First joystick moves gripper forwards, backwards, left and right,
   button start/stop recording positions.

   Second joystick moves gripper up, down, and closes and opens,
   button start/stop playing recorded positions.
   Press button for 2 seconds to autoplay.

   Pins:
   Arduino    Stick1    Stick2    Base   Shoulder   Elbow    Gripper   Record/
      GND       GND       GND    Brown     Brown   Brown     Brown    Auto play
       5V       VCC       VCC      Red       Red     Red       Red    LED
       A0       HOR
       A1       VER
       PD2      BUTT
       A2                 HOR
       A3                 VER
       PD3                BUTT
       11                       Yellow
       10                                 Yellow
        9                                         Yellow
        6                                                   Yellow
       PD4                                                            X
*/
#include<Servo.h>

boolrepeatePlaying=false; /* Repeatedly is running recorded cycle */
intdelayBetweenCycles=2000; /* Delay between cycles */

intbasePin=11;       /* Base servo */
intshoulderPin=10;   /* Shoulder servo */
intelbowPin=9;       /* Elbow servo */
intgripperPin=6;     /* Gripper servo */

intxdirPin=0;        /* Base - joystick1*/
intydirPin=1;        /* Shoulder - joystick1 */
intzdirPin=3;        /* Elbow - joystick2 */
intgdirPin=2;        /* Gripper - joystick2 */

//int pinRecord = A4;     /* Button record - backward compatibility */
//int pinPlay = A5;       /* Button play   - backward compatibility */
intpinRecord=PD2;     /* Button record - recommended (A4 is deprecated, will by used for additional joystick) */
intpinPlay=PD3;       /* Button play   - recommended (A5 is deprecated, will by used for additional joystick) */
intpinLedRecord=PD4;   /* LED - indicates recording (light) or auto play mode (blink one) */

booluseInternalPullUpResistors=false;

constintbuffSize=512; /* Size of recording buffer */

intstartBase=90;
intstartShoulder=90;
intstartElbow=90;
intstartGripper=0;

intposBase=90;
intposShoulder=90;
intposElbow=90;
intposGripper=0;

intlastBase=90;
intlastShoulder=90;
intlastElbow=90;
intlastGripper=90;

intminBase=0;
intmaxBase=150;
intminShoulder=0;
intmaxShoulder=150;
intminElbow=0;
intmaxElbow=150;
intminGripper=0;
intmaxGripper=150;

constintcountServo=4;
intbuff[buffSize];
intbuffAdd[countServo];
intrecPos=0;
intplayPos=0;

intbuttonRecord=HIGH;
intbuttonPlay=HIGH;

intbuttonRecordLast=LOW;
intbuttonPlayLast=LOW;

boolrecord=false;
boolplay=false;
booldebug=false;

Stringcommand="Manual";
intprintPos=0;

intbuttonPlayDelay=20;
intbuttonPlayCount=0;

boolledLight=false;

ServoservoBase;
ServoservoShoulder;
ServoservoElbow;
ServoservoGripper;

voidsetup() {
  Serial.begin(9600);

  if (useInternalPullUpResistors) {
    pinMode(pinRecord, INPUT_PULLUP);
    pinMode(pinPlay, INPUT_PULLUP);
  }
  else
  {
    pinMode(pinRecord, INPUT);
    pinMode(pinPlay, INPUT);
  }

  pinMode(xdirPin, INPUT);
  pinMode(ydirPin, INPUT);
  pinMode(zdirPin, INPUT);
  pinMode(gdirPin, INPUT);

  pinMode(pinLedRecord, OUTPUT);

  servoBase.attach(basePin);
  servoShoulder.attach(shoulderPin);
  servoElbow.attach(elbowPin);
  servoGripper.attach(gripperPin);

  StartPosition();

  digitalWrite(pinLedRecord, HIGH);
  delay(1000);
  digitalWrite(pinLedRecord, LOW);
}

voidloop() {

  buttonRecord=digitalRead(pinRecord);
  buttonPlay=digitalRead(pinPlay);

  //   Serial.print(buttonRecord);
  //   Serial.print("\t");
  //   Serial.println(buttonPlay);
  //   for testing purposes

  if (buttonPlay==LOW)
  {
    buttonPlayCount++;

    if (buttonPlayCount>=buttonPlayDelay)
    {
      repeatePlaying=true;
    }
  }
  elsebuttonPlayCount=0;

  if (buttonPlay!=buttonPlayLast)
  {
    if (record)
    {
      record=false;
    }

    if (buttonPlay==LOW)
    {
      play=!play;
      repeatePlaying=false;

      if (play)
      {
        StartPosition();
      }
    }
  }

  if (buttonRecord!=buttonRecordLast)
  {
    if (buttonRecord==LOW)
    {
      record=!record;

      if (record)
      {
        play=false;
        repeatePlaying=false;
        recPos=0;
      }
      else
      {
        if (debug) PrintBuffer();
      }
    }
  }

  buttonPlayLast=buttonPlay;
  buttonRecordLast=buttonRecord;

  floatdx=map(analogRead(xdirPin), 0, 1023, -5.0, 5.0);
  floatdy=map(analogRead(ydirPin), 0, 1023, 5.0, -5.0);
  floatdz=map(analogRead(zdirPin), 0, 1023, 5.0, -5.0);
  floatdg=map(analogRead(gdirPin), 0, 1023, 5.0, -5.0);

  if (abs(dx) <1.5) dx=0;
  if (abs(dy) <1.5) dy=0;
  if (abs(dz) <1.5) dz=0;
  if (abs(dg) <1.5) dg=0;

  posBase+=dx;
  posShoulder+=dy;
  posElbow+=dz;
  posGripper+=dg;

  if (play)
  {
    if (playPos>=recPos) {
      playPos=0;

      if (repeatePlaying)
      {
        delay(delayBetweenCycles);
        StartPosition();
      }
      else
      {
        play=false;
      }
    }

    boolendOfData=false;

    while (!endOfData)
    {
      if (playPos>=buffSize-1) break;
      if (playPos>=recPos) break;

      intdata=buff[playPos];
      intangle=data&0xFFF;
      intservoNumber=data&0x3000;
      endOfData=data&0x4000;

      switch (servoNumber)
      {
        case0x0000:
          posBase=angle;
          break;

        case0x1000:
          posShoulder=angle;
          break;

        case0x2000:
          posElbow=angle;
          break;

        case0x3000:
          posGripper=angle;
          dg=posGripper-lastGripper;
          break;
      }

      playPos++;
    }
  }

  if (posBase>maxBase) posBase=maxBase;
  if (posShoulder>maxShoulder) posShoulder=maxShoulder;
  if (posElbow>maxElbow) posElbow=maxElbow;
  if (posGripper>maxGripper) posGripper=maxGripper;

  if (posBase<minBase) posBase=minBase;
  if (posShoulder<minShoulder) posShoulder=minShoulder;
  if (posElbow<minElbow) posElbow=minElbow;
  if (posGripper<minGripper) posGripper=minGripper;

  servoBase.write(posBase);
  servoShoulder.write(posShoulder);
  servoElbow.write(posElbow);

  boolwaitGripper=false;
  if (dg<0) {
    posGripper=minGripper;
    waitGripper=true;
  }
  elseif (dg>0) {
    posGripper=maxGripper;
    waitGripper=true;
  }

  servoGripper.write(posGripper);
  if (play&&waitGripper)
  {
    delay(1000);
  }

  if ((lastBase!=posBase) | (lastShoulder!=posShoulder) | (lastElbow!=posElbow) | (lastGripper!=posGripper))
  {
    if (record)
    {
      if (recPos<buffSize-countServo)
      {
        intbuffPos=0;

        if (lastBase!=posBase)
        {
          buffAdd[buffPos] =posBase;
          buffPos++;
        }

        if (lastShoulder!=posShoulder)
        {
          buffAdd[buffPos] =posShoulder|0x1000;
          buffPos++;
        }

        if (lastElbow!=posElbow)
        {
          buffAdd[buffPos] =posElbow|0x2000;
          buffPos++;
        }

        if (lastGripper!=posGripper)
        {
          buffAdd[buffPos] =posGripper|0x3000;
          buffPos++;
        }

        buffAdd[buffPos-1] =buffAdd[buffPos-1] |0x4000;

        for (inti=0; i<buffPos; i++)
        {
          buff[recPos+i] =buffAdd[i];
        }

        recPos+=buffPos;
      }
    }

    command="Manual";
    printPos=0;

    if (play)
    {
      command="Play";
      printPos=playPos;
    }
    elseif (record)
    {
      command="Record";
      printPos=recPos;
    }

    Serial.print(command);
    Serial.print("\t");
    Serial.print(printPos);
    Serial.print("\t");
    Serial.print(posBase);
    Serial.print("\t");
    Serial.print(posShoulder);
    Serial.print("\t");
    Serial.print(posElbow);
    Serial.print("\t");
    Serial.print(posGripper);
    Serial.print("\t");
    Serial.print(record);
    Serial.print("\t");
    Serial.print(play);
    Serial.println();
  }

  lastBase=posBase;
  lastShoulder=posShoulder;
  lastElbow=posElbow;
  lastGripper=posGripper;

  if ( repeatePlaying)
  {
    ledLight=!ledLight;
  }
  else
  {
    if (ledLight)
    {
      ledLight=false;
    }

    if (record)
    {
      ledLight=true;
    }
  };

  digitalWrite(pinLedRecord, ledLight);
  delay(50);
}

voidPrintBuffer()
{
  for (inti=0; i<recPos; i++)
  {
    intdata=buff[i];
    intangle=data&0xFFF;
    intservoNumber=data&0x3000;
    boolendOfData=data&0x4000;

    Serial.print("Servo=");
    Serial.print(servoNumber);
    Serial.print("\tAngle=");
    Serial.print(angle);
    Serial.print("\tEnd=");
    Serial.print(endOfData);
    Serial.print("\tData=");
    Serial.print(data, BIN);
    Serial.println();
  }
}

voidStartPosition()
{
  intangleBase=servoBase.read();
  intangleShoulder=servoShoulder.read();
  intangleElbow=servoElbow.read();
  intangleGripper=servoGripper.read();

  Serial.print(angleBase);
  Serial.print("\t");
  Serial.print(angleShoulder);
  Serial.print("\t");
  Serial.print(angleElbow);
  Serial.print("\t");
  Serial.print(angleGripper);
  Serial.println("\t");

  posBase=startBase;
  posShoulder=startShoulder;
  posElbow=startElbow;
  posGripper=startGripper;

  servoBase.write(posBase);
  servoShoulder.write(posShoulder);
  servoElbow.write(posElbow);
  servoGripper.write(posGripper);
}









文章分类: 平台创客Arduino
分享到:
会员登录
登录
我的资料
留言
回到顶部