用 Arduino DIY 镭射激光逗猫神器

喵星人对于激光发射器发出的光点有着天生、难以抗拒的执着。它们就像一个个职业杀手一样直接的扑向那个小小的红色激光点。对于猫咪们来说狩猎是一项很好的运动。看着萌宠们发起攻击的样子,作为铲屎官的你会不会觉得是一件饶有兴趣的事情呢!通常你只要拿起你的激光发射器就可以做到,但是你总会预先知道激光点的方向。让我们给这个游戏制造一些混乱元素——可以平面移动或改变仰角的激光炮塔,并让它可以周期性地随机移动。先看看下面的视频~

第一步:准备项目组件清单

它需要用你惯用的任何舵机来配合驱动塔楼。这里有两种方案:
RobotGeek 云台:
如果你喜好添加一个摄像头或者附加一个的自带钓鱼杆的逗猫玩具,这款将是一个好的选择。

  • RobotGeek云台和舵机
  • 镭射激光发射器和固定配件
  • Geekduino或者其他兼容Arduino UNO 开发板
  • 传感器模块 (推荐,可简化接线)
  • 7V 5A 电源

Mini 云台:
不错的低成本选择, 但是9G 的舵机输出的力量有限。

  • Mini 云台套件
  • 9G 舵机 x 2
  • 镭射激光发射器和固定配件
  • Geekduino或者其他兼容Arduino UNO 开发板
  • 传感器模块 (推荐,可使简化接线)
  • 6V 2A电源

第二步:组装炮塔


如果你使用的是RobotGeek云台,安装说明可以在这找到
如果你使用的是Mini云台, 安装说明可以在这找到
拿上激光发射器,让我们把它连接起来。

第三步:接线

设备 引脚类型 引脚编号
平移舵机 数字 10
仰角舵机 数字 11
激光发射器 数字 2

确保设定的输入电压能供更大的伺服7v运行。对于9G的伺服系统,你可以用5V或6V的电源。真的是超级简单的接线。赶紧编码起来吧!

第四步:编程

上传以下代码到你的 Arduino 主板上。

//https://github.com/robotgeek/desktopRoboTurretV3/tree/master/roboTurret3_lazerDazer
//Includes
#include <Servo.h>          //include the servo library for working with servo objects

//Defines
const int PAN = 10;         //Pan Servo Digital Pin
const int TILT = 11;        //Tilt Servo Digital Pin
const int LASER = 2;        //Laser Digital Pin
const int PAN_MIN = 40;     // minimum pan servo range, value should be between 0 and 180 and lower than PAN_MAX
const int PAN_MAX = 140;    // maximum pan servo range, value should be between 0 and 180 and more than PAN_MIN
const int TILT_MIN = 60;    // minimum tilt servo range, value should be between 0 and 180 and lower than TILT_MAX
const int TILT_MAX = 100;   // maximum tilt servo range, value should be between 0 and 180 and more than TILT_MIN
const int SPEED_MIN = 10;   // minimum interpolation speed (less than 5 not recommended)
const int SPEED_MAX = 10;   // maximum interpolation speed (Lower SPEED_MAX values mean that the program will cycle more quickly, leading to an increased occurance of pausing. Adjust ROLL_MAX accordingly)
const int ROLL_MAX = 40;    // Roll the dice for random pause. Lower value = more frequent pauses, higher value = less frequent pauses
const int PAUSE_MIN = 500;  // define minimum pause length in mS. 1000 = 1 second
const int PAUSE_MAX = 1500; // define maximum pause length in mS.

Servo panServo, tiltServo;  // create servo objects to control the pan and tilt servos

long dazer_speed;
int dazer_pause_roll;
int pause_length = 1;

int panValue = 90;   //current positional value being sent to the pan servo. 
int tiltValue = 90;  //current positional value being sent to the tilt servo. 
byte pan_goal;
byte tilt_goal;

//State Variables
int laserState = LOW;         //The current state of the laser module

//Timing variables
long lastDebounceTime = 0;  // the last time the output pin was toggled. This variable is a 'long' because it may need to hold many milliseconds, and a 'long' will afford more space than an 'int'
int debounceDelay = 50;    // the amount of time that that a button must be held, for a reading to register (in milliseconds)

void setup() 
{
  //initialize servos
  panServo.attach(PAN);  // attaches/activates the pan servo on pin PAN 
  tiltServo.attach(TILT);  // attaches/activates the tilt servo on pin TILT 

  //initalize digital pins
  pinMode(LASER, OUTPUT);      //set the LASER Pin to an output
  
  //write initial servo positions to set the servos to 'home'
  panServo.write(panValue);  //sets the pan servo position to the default 'home' value
  tiltServo.write(tiltValue);//sets the tilt servo position to the default 'home' value
  
  randomSeed(analogRead(0));  //set a basis for the random() engine, based off of analog0 which will be a random number because nothing is attached

  digitalWrite(LASER, HIGH);
} 

void loop() 
{ 
  dazer_pause_roll = random(1, ROLL_MAX);      //pick a random number between 1 and ROLL_MAX
  pause_length = random(PAUSE_MIN, PAUSE_MAX); //pick a random number between PAUSE_MIN and PAUSE_MAX
  delay(pause_length);                         //use the random number to pause for pause_length milliseconds

  pan_goal = random(PAN_MIN, PAN_MAX);
  tilt_goal = random(TILT_MIN, TILT_MAX);
  dazer_speed = random(SPEED_MIN, SPEED_MAX);
  //perfom the loop while the current positions are different from the goal positions 

  //if the pan_goal is larger than the pan position, move the pan position up
  if (pan_goal > panValue)
  {
    panValue = panValue++;
  }
  
  //if the pan_goal is smaller than the pan position, move the pan position down
  else if (pan_goal < panValue)
  {
    panValue = panValue--;
  }               
  
  //if the tilt_goal is larger than the tilt position, move the pan position up
  if (tilt_goal > tiltValue)
  {
    tiltValue = tiltValue++;
  }
  //if the tilt_goal is smaller than the tilt position, move the pan position down
  else if (tilt_goal < tiltValue)
  {
    tiltValue = tiltValue--;
  }    
  
  panServo.write(pan_goal);                    // sets the servo position according to the scaled value 
  tiltServo.write(tilt_goal);                  // sets the servo position according to the scaled value 
  delay(dazer_speed);                          // waits for a random delay before the next loop
} //end loop()

把你的猫咪萌宠抓过来,开启它吧!(当然是开启你的塔楼啦,而不是你的猫咪啦!)你的萌宠可是自带电源的哦!可不要尝试给你的猫咪充电哦!

第五步:Done!就是这么完美!

现在铲屎的你只需坐下来,看着你的猫咪追逐和攻击那个“邪恶”的小红点啦!

via

坐沙发

发表评论