2016/12/07

好玩的簡易機器人

好玩的簡易機器人

 Robot SCRU-FE (2016/10/30)
來源:http://www.thingiverse.com/thing:780050 , 3D 組件
又是連續假期,找一個專案來做做,手邊剛好有的材料,半天就完成他。




















Arduino UNO 加控制板使用 Arduino MotorShield V1.0 板,由Adafruit下載 Motor Shield 函式程式,配合 Arduino IDE 1.0.4 版本,程式如下:


 
#include <Servo.h>
#include <AccelStepper.h>
#include <AFMotor.h>

Servo myservo;  // create servo object to control a servo
             
AF_DCMotor motor1(1, MOTOR12_64KHZ);
AF_DCMotor motor2(2, MOTOR12_64KHZ);


#define trigPin A0
#define echoPin A1

int pos = 0;    // variable to store the servo position
String result,StringR;

void setup()
{
   Serial.begin(9600);           // set up Serial library at 9600 bps
   StringR = String("R");
   pinMode(trigPin, OUTPUT);
   pinMode(echoPin, INPUT);
  
    Serial.println("Motor test!");
    motor1.setSpeed(200);
    motor2.setSpeed(200);
    motor1.run(RELEASE);      // stopped
    motor2.run(RELEASE);
  
    Serial.println("Servo test!");
    myservo.attach(10);  // attaches the servo on pin 10 to the servo object
    for(pos = 0; pos < 180; pos += 1)  // goes from 0 degrees to 180 degrees
  {                                  // in steps of 1 degree
    myservo.write(pos);              // tell servo to go to position in variable 'pos'
    delay(15);                       // waits 15ms for the servo to reach the position
  }
  for(pos = 180; pos>=1; pos-=1)     // goes from 180 degrees to 0 degrees
  {                              
    myservo.write(pos);              // tell servo to go to position in variable 'pos'
    delay(15);                       // waits 15ms for the servo to reach the position
  }
   myservo.write(90);
}


void loop()
{
  long distance;
  distance = getDist();
  if(distance < 60) {
    motor1.setSpeed(100);
    motor2.setSpeed(100);
  } else {
    motor1.setSpeed(200);
    motor2.setSpeed(200);
  }
  if (distance < 10) { 
    motor1.run(RELEASE);    
    motor2.run(RELEASE);
    delay(1000);
    motor1.run(BACKWARD);    
    motor2.run(BACKWARD);
    delay(500);
    motor1.run(RELEASE);     // the other way
    motor2.run(RELEASE);
    result = checkturnLR();
    if(StringR.equals(result))
    {
      motor1.run(FORWARD);     
      motor2.run(BACKWARD);
      delay(1000);
      motor1.run(FORWARD);     
      motor2.run(FORWARD);
    }
    else
    {
      motor1.run(BACKWARD);     
      motor2.run(FORWARD);
      delay(1000);
      motor1.run(FORWARD);     
      motor2.run(FORWARD);
    }
  }
  else {
    motor1.run(FORWARD);      // turn it on going forward
    motor2.run(FORWARD);
  }
  myservo.write(90);
}

String checkturnLR()
{
  long distanceR, distanceL;
  
  // check Right distance
  myservo.write(150);             
  delay(1000);                     
  distanceR = getDist();
 
  // check Left distance
  myservo.write(30);            
  delay(1000);                
  distanceL = getDist();
  myservo.write(90);
  if(distanceR > distanceL) {
    return "R";
  }
  else {
    return "L";
  }
}

long getDist()
{
  long duration, distance;
  digitalWrite(trigPin, LOW);
  delayMicroseconds(2);
  digitalWrite(trigPin, HIGH);
  delayMicroseconds(10);
  digitalWrite(trigPin, LOW);
  duration = pulseIn(echoPin, HIGH);
  distance = (duration/2) / 29.1;
  return distance;
}


馬達接 MotorShield 的 M1, M2
伺服器接 MotorShield 的 Servo1 的引出座。
UltraSonic, Trig 接 MotorShield 的 A0 pin,Echo 接 A1 pin。另接 5VDC。
電池使用兩顆 18650 電池。
動作,前進,如距離接近60cm減速,10cm停車,倒退,左右測距,朝距離遠的方向轉向,前進,距離大於60cm加速。
簡單完成,好玩。
    
 

呼叫 Moom Base Alpha

Space 1999 影集中的 Eagle 太空船

因為有了一台可以印製較細緻的3D印表機,於是完成他。