(SKU:KIT0090) 六足虫虫机器人高级玩法

来自DFRobot Product Wiki
跳转至: 导航搜索
虫虫机器人模型(300px)

目录

简介

如果有一只“虫”会让你爱不释手,那就是这款虫虫机器人六足版了! 

新版虫虫用3个微型舵机的协调摆动来行走,通过红外测距来感知环境,还能够感知周围的光线亮度。另外,新版虫虫的中枢依旧是一颗强大的Beetle控制器和扩展板,而且是利用简单易用的Arduino来编程。所有这些功能让虫虫的行走更加复杂,互动更加丰富。可以实现前进、后退、避障拐弯、巡光等功能。

简单的组装让你了解最基本的机器人原理和智能控制。喜欢DIY的用户还可以在原有代码的基础上改造出自己的互动模式。所以,其实打造一款机器人并没有那么难啦。

产品规格

  • 工作电压:5-6.4V(4节7号干电池)
  • 控制器:Bluno Beetle (Bootloader: Arduino UNO)
  • 组装后尺寸:180x153X120mm
  • 重量:240g


配送清单

  • 主控模组 (Bluno Beetle+Bluno Beetle shield) X1
  • 9g Micro Servo X3
  • GP2Y0A21 距离传感器 X1
  • 传感器支架 X1
  • 2节7号电池盒 X2
  • 热缩管-黑色-直径6mm-100mm长 X1
  • T型胶塞-规格M4 X8
  • 钢丝200mm x 1mm X3
  • 舵机连接器 X1
  • 白色泡沫双面胶(L/W/H:78x61x3mm) X1
  • 数字传感器线 X2
  • 扎线带1.8x100mm X3
  • 高品质 micro USB数据线 X1
  • 安装说明书 x1


教程

按照我们的步骤,您将会制作出属于您自己的虫虫机器人。


必备工具

  • 螺丝刀
    • 螺丝刀,是机器人制作非常必要的工具。不用多说,用来拧螺丝的。建议你可以买一套完整的螺丝刀套件。面对不同型号的螺丝,使用起来就得心应手了。
  • 尖嘴钳
    • 这里用于弯曲钢丝用的,尽量少用手去折铁丝。尖嘴钳还常用于剪掉一些多余的线。
  • 剪刀
    • 剪刀用来剪贴纸和材料的。


组装步骤

  • 详细组装步骤

虫虫机器人教程.pdf

虫虫代码

自动模式

自动模式代码

#include <Servo.h>
// creating the servo objects for front, rear and mid servo
Servo frontLeg;
Servo rearLeg;
Servo midLeg;
// setting the servo angle to 90° for startup
byte frontAngle = 90;
byte rearAngle = 90;
byte midAngle = 90;
// setting the delay value
byte delayWalk = 2;
byte delayTurn = 3;
// Analog sensor pins
int distanceSensor = A1;
int lightSensorLeft = A2;
int lightSensorRight = A0;
// Analog sensor reading
int sensorValue = 0;
// Values
int sensorValueLeft = 0;
int sensorValueRight = 0;
int left = 0;
int right = 0;
// Change the following value to decrease or increase the sensitivity. 
// Bigger value for lower sensitivity and smaller value for highter sensitivity 
int lightDifference = 60;
// Decrease the danger value when you want to INCREASE the collision trigger distance.
// Increase the danger value when you want to DECREASE the collision trigger distance 
int danger = 450;  // Increase this value when you want to DECREASE the collision trigger distance 
// Arrays for sensor reading average calculation
int leftReadings[11];
int rightReadings[11];
// Booleans for decision
boolean lightLeft = false;
boolean lightRight = false;
// Setup function
void setup(){
  // serial connection for debugging
  Serial.begin(9600);
  // attaching the servos to their pins  
  frontLeg.attach(9);
  rearLeg.attach(10);
  midLeg.attach(11);
  // move servos to center position -> 90°
  frontLeg.write(frontAngle);
  rearLeg.write(rearAngle);
  midLeg.write(midAngle);
  delay(2000);

}
// Light detection and filtering
void scan()
{  
    int i;
        // Take 5 readings on each sensor
	for (i = 0; i < 11; i = i + 1) {
		// read the value from the left and right sensor:
                sensorValueLeft = analogRead(lightSensorLeft);
                sensorValueRight = analogRead(lightSensorRight);
                // add sensor readings of both sides to their respective array
		leftReadings[i] = sensorValueLeft;
                rightReadings[i] = sensorValueRight;
	    }
// calculate an average value for left and right sensor readings
// Sorting the left light sensor values
sort(leftReadings,11);
    left = leftReadings[5];
sort(rightReadings,11);
     right = rightReadings[5];
}
// Sort the array ascending
void sort(int a[], int size) {
    for(int i=0; i<(size-1); i++) {
        for(int o=0; o<(size-(i+1)); o++) {
                if(a[o] > a[o+1]) {
                    int t = a[o];
                    a[o] = a[o+1];
                    a[o+1] = t;
                }
        }
    }
}
// Desision depending on the light values
void decision(){
// Call the scan function to provide the sensor values of the left and right light sensor
  scan();
// Compare the values and make a decision according the difference value
if (left > right){
    // subtract right from left value to get a value to work with
    left = left-right;
    // check if that previous calculated value is greater than 50
    if (left > lightDifference) // I will call this "Inside IF 1" in further comments.
    {
      lightLeft = true;
      lightRight = false;
    }
    // That happens when the left value in "Inside IF 1" is lower than 50
    else{
      /* Is the calculated value for left lower than 50 then go forward. Why? Is that value lower than 50 then the difference 
         between the light on the left and right sensor are not that great. In this case it should be save to go forward.
      */
      lightLeft = true;
      lightRight = true;
    }
  }
  // That happens when left is lower than right 
  else if (left < right){
    // subtract left from right value to get a value to work with
    right = right-left;
    // check if that previous calculated value is greater than 50
    if (right > lightDifference){  // I will call this "Inside IF 2" in further comments.
      lightLeft = false;
      lightRight = true;
    }
    else{ 
      lightLeft = true;
      lightRight = true;
    }
  }
  /* That "else" happens when none of the above conditions occur and the left and right sensor shows the same readings.
     This condition will probably never occur but we need it anyway.
  */
  else{
    // Go forward without questions
      lightLeft = true;
      lightRight = true;
  }
}
// Walk forward //////////////////////////////////////////////////////////
void forward(){

  for (midAngle = 70; midAngle < 100; midAngle +=1){
    midLeg.write(midAngle);
    delay(delayWalk);
  }
  for (frontAngle = 120; frontAngle > 50; frontAngle -= 1){
    frontLeg.write(frontAngle);
    rearLeg.write(frontAngle);
    delay(delayWalk);
  }
  for (midAngle = 100; midAngle > 70; midAngle -=1){
    midLeg.write(midAngle);
    delay(delayWalk);
  }
  for (frontAngle = 50; frontAngle < 120; frontAngle += 1){
    frontLeg.write(frontAngle);
    rearLeg.write(frontAngle);
    delay(delayWalk);
  }
}
// Walk reverse //////////////////////////////////////////////////////////
void reverse(){

  for (midAngle = 70; midAngle < 100; midAngle +=1){
    midLeg.write(midAngle);
    delay(delayWalk);
  }
  for (frontAngle = 50; frontAngle < 120; frontAngle += 1){
    frontLeg.write(frontAngle);
    rearLeg.write(frontAngle);
    delay(delayWalk);
  }

  for (midAngle = 100; midAngle > 70; midAngle -=1){
    midLeg.write(midAngle);
    delay(delayWalk);
  }
  for (frontAngle = 120; frontAngle > 50; frontAngle -= 1){
    frontLeg.write(frontAngle);
    rearLeg.write(frontAngle);
    delay(delayWalk);
  }
}
// Left Turn //////////////////////////////////////////////////////////
void leftTurn(){

  rearLeg.write(90);
  for (midAngle = 70; midAngle < 110; midAngle += 1){
    midLeg.write(midAngle);
    delay(delayTurn); 
  } 
  for (frontAngle = 70;