本帖最后由 liuxing4585 于 2016-8-4 00:56 编辑
前言:
之所以选择这个题材,是因为在试用Landzo C1两个月后,刚子对Landzo C1的基本模块都进行了了解,在这期间正好试用了一款智能拖地机器人,突然想到Landzo C1智能小车所配的模块,不就是智能拖地机上应该有的模块吗?因此萌生了这个实验的念头。
原理:
拖地机器人在原理上讲并不复杂,说白了就是一个带拖布的智能小车,在屋子里行驶时能够自动判断路况,进行避障行走。那我们需要用到Landzo C1的哪些模块呢?
需要用到的模块:
1.电机驱动模块。有了它才能实现行走。通过两轮的转速和方向改变实现前进、转向、后退。
2.红外遥控模块,可实现遥控。
3.超声波模块,实现前方避障。
4.红外避障模块(水平),检测后方障碍实现避障。
5.红外避障模块(垂直),检测地板有无,实现跌落检测。
6.按键模块,实现状态设定。
7.LED模块,实现状态显示。
接线:
1.电机驱动模块-D6 D9 GND VBAT D3 D5。
2.红外遥控模块- GND 5V D11。
3.超声波模块-GND D8 D7 5V。
4.红外避障模块(水平),- A4 A5。
5.红外避障模块(垂直),-D4 D2 5V GND。
6.按键模块,-GND 5V A0。
7.LED模块,-GND 5V D13。
8.舵机 -GND 5V D10
红外遥控器使用华为iptv机顶盒的遥控器,先测定键值:开机: 1303526340
前:1303532460
后:1303530420
左:1303550310
右;1303544700
音量+ 1303511550
音量- 1303544190
1 1303529910
2 1303562550
3 1303524300
程序:
- #define DEBUG 0 // set to 1 to print to serial monitor, 0 to disable
- #include
- #include
- Servo servo_pin_10;
- int findsomebodyL ;
- int findsomebodyR ;
- int findsomebodyVL ;
- int findsomebodyVR ;
- // Constants
- const int EchoPin = 8; //超声波信号输入
- const int TrigPin = 7; //超声波控制信号输出
- int maxspeed = 255;
- const int leftmotorpin1 = 3; // 直流电机信号输出
- const int leftmotorpin2 = 5;
- const int rightmotorpin1 = 6;
- const int rightmotorpin2 = 9;
- const int HeadServopin = 10; // 舵机信号输出 只有9或10接口可利用
- const int IRLpin = 18; // 红外输入
- const int IRRpin = 19; // 红外输入
- const int IRVRpin = 4; // 红外输入
- const int IRVLpin = 2; // 红外输入
- const int keypin = 14; // 按键输入
- const int remotepin = 11; // 按键输入
- int pos = 0; // 该变量用与存储舵机角度位置
- int Status;
- // Variables
- long currDist = 0; // 距离
- IRrecv irrecv(11);
- decode_results results;
- long unsigned int ReceiveCode;
- long unsigned int return_decode()
- {
- if (irrecv.decode(&results)) {
- irrecv.resume(); // 接收下一个值
- return results.value;
- }
- else
- {
- return 0;
- irrecv.resume(); // 接收下一个值
- }
- }
- void servopulse(int val1)//定义一个脉冲函数
- { int myangle1 = map(val1, 0, 180, 500, 2480);
- digitalWrite(HeadServopin, HIGH); //将舵机接口电平至高
- delayMicroseconds(myangle1);//延时脉宽值的微秒数
- digitalWrite(HeadServopin, LOW); //将舵机接口电平至低
- delay(20 - val1 / 1000);
- }
- void setup() {
- Serial.begin(9600); // 开始串行监测
- //信号输入接口
- pinMode(keypin, INPUT);
- pinMode(IRLpin, INPUT);
- pinMode(IRRpin, INPUT);
- pinMode(IRVRpin, INPUT);
- pinMode(IRVLpin, INPUT);
- pinMode( leftmotorpin1, OUTPUT);
- pinMode( leftmotorpin2, OUTPUT);
- pinMode( rightmotorpin1 , OUTPUT);
- pinMode( rightmotorpin2 , OUTPUT);
- pinMode(HeadServopin , OUTPUT); //设定舵机接口为输出接口
- pinMode(TrigPin, OUTPUT);
- pinMode(EchoPin, INPUT);
- pinMode(11, INPUT); //设置红外接收引脚为输入
- irrecv.enableIRIn(); //初始化红外遥控
- // servo_pin_10.write(10);delay(1000);
- // servo_pin_10.write( 90);delay(1000);
- // servo_pin_10.write( 170);delay(1000);
- totalhalt(); // stop!
- Status = 0;
- }
- void loop() {
- ReceiveCode = 0;
- ReceiveCode = return_decode();
- // if ( ReceiveCode>0)
- // {
- // Serial.print("message");
- // Serial.print(" ");
- // Serial.print( ReceiveCode );
- // Serial.print(" ");
- // Serial.println();
- // delay( 500 );
- // }
- currDist = MeasuringDistance(); //读取前端距离
- findsomebodyL = digitalRead(IRLpin);
- findsomebodyR = digitalRead(IRRpin);
- findsomebodyVL = digitalRead(IRVLpin);
- findsomebodyVR = digitalRead(IRVRpin);
- // if (DEBUG) {
- // Serial.print("Current Distance: ");
- // Serial.println(currDist);
- // Serial.print("findsomebodyL:");
- // Serial.println(findsomebodyL);
- // Serial.print("findsomebodyR:");
- // Serial.println(findsomebodyR);
- // Serial.print("findsomebodyVL:");
- // Serial.println(findsomebodyVL);
- // Serial.print("findsomebodyVR:");
- // Serial.println(findsomebodyVR);
- // Serial.print("Status:");
- // Serial.println(Status);
- // }
- if (Status == 0)
- {
- totalhalt(); // stop!
- if ( ReceiveCode == 1303526340)
- {
- Status = 1;
- }
- else if ( ReceiveCode == 1303532460 )
- {
- nodanger();
- }
- else if ( ReceiveCode == 1303530420 )
- {
- backup();
- }
- else if ( ReceiveCode ==1303550310 )
- {
- body_lturn();
- }
- else if ( ReceiveCode == 1303544700)
- {
- body_rturn();
- }
- }
- else if (Status == 1)
- {
- if ( ReceiveCode == 1303529910)
- {
- Status = 0;
- }
- if (currDist >= 7000 && findsomebodyVR == 1 && findsomebodyVL == 1 ) {
- nodanger();
- }
- else if (currDist < 7000 || findsomebodyVR == 0 || findsomebodyVL == 0 ) {
- backup();
- totalhalt(); // stop!
- body_lturn();
- totalhalt(); // stop!
- nodanger();
- totalhalt(); // stop!
- body_lturn();
- totalhalt(); // stop!
- }
- }
- }
- //测量距离 单位厘米
- long MeasuringDistance() {
- digitalWrite(TrigPin, LOW);
- delayMicroseconds(2);
- digitalWrite(TrigPin, HIGH);
- delayMicroseconds(5);
- digitalWrite(TrigPin, LOW);
- long duration = pulseIn(EchoPin, HIGH);
- return duration * 17;
- }
- // 前进
- void nodanger() {
- analogWrite(leftmotorpin1, 0);
- analogWrite(leftmotorpin2, maxspeed);
- analogWrite(rightmotorpin2, 0);
- analogWrite(rightmotorpin1, maxspeed);
- delay(100);
- // totalhalt(); // first stop!
- return;
- }
- //后退
- void backup() {
- analogWrite(leftmotorpin1, maxspeed);
- analogWrite(leftmotorpin2, 0);
- analogWrite(rightmotorpin2, maxspeed);
- analogWrite(rightmotorpin1, 0);
- delay(200);
- // totalhalt(); // first stop!
- return;
- }
- //选择路线
- void whichway() {
- totalhalt(); // first stop!
- servo_pin_10.write(60);
- delay(500);
- int lDist = MeasuringDistance(); // check left distance
- totalhalt(); // 恢复探测头
- servo_pin_10.write(120);
- delay(500);
- int rDist = MeasuringDistance(); // check right distance
- totalhalt(); // 恢复探测头
- if (lDist >= 7000 && rDist < 7000)
- {
- body_lturn();
- }
- else if ( lDist < 7000 && rDist >= 7000)
- {
- body_rturn();
- }
- else if ( lDist < 7000 && rDist <= 7000)
- {
- if (lDist < rDist) {
- body_lturn();
- }
- else {
- body_rturn();
- }
- }
- return;
- }
- //重新机械调整到初始状态
- void totalhalt() {
- analogWrite(leftmotorpin1, 255);
- analogWrite(leftmotorpin2, 255);
- analogWrite(rightmotorpin1, 255);
- analogWrite(rightmotorpin2, 255);
- servo_pin_10.write( 90);
- delay(100);
- }
- //左转
- void body_lturn() {
- analogWrite(leftmotorpin1, 0);
- analogWrite(leftmotorpin2, maxspeed);
- analogWrite(rightmotorpin2, maxspeed);
- analogWrite(rightmotorpin1, 0);
- delay(100);
- // totalhalt();
- }
- //右转
- void body_rturn() {
- analogWrite(leftmotorpin1, maxspeed);
- analogWrite(leftmotorpin2,0);
- analogWrite(rightmotorpin2, 0);
- analogWrite(rightmotorpin1, maxspeed);
- delay(100);
- // totalhalt();
- }
- void randTrun() {
- long randNumber;
- randomSeed(analogRead(0));
- randNumber = random(0, 10);
- if (randNumber > 5) {
- body_rturn();
- }
- else
- {
- body_lturn();
- }
- }
复制代码
视频:
|