传感器和小车
我现在在做一个作业,老师要求我们写出指令,然后让小车根据我们写下来的指令去运动。一共是三个步骤,第一步是旋转,第二步是前进五秒,第三步是向右移动五秒。
运动过程都很简单。
但这些不是重点,重点是我们在小车的前面装了一个感测距离的传感器。
老师的要求是,在小车运行的同时,传感器也一直在工作。
当它检测到前方多少厘米有障碍物的时候,它就会停下来,一直停着。
然后等前方的障碍物移开的时候,小车就会继续刚才没有完成的运动。
我现在的问题是,我不知道如何让传感器一直感测距离,我只能在最开始运行的时候感测。
我个人觉得应该是放在紧急停止时应该放在中断里面,但是这个中断该怎么触发呢?
首先我得让感测器一直感测,然后一直得到一个distance.
然后再通过比较这个distance,然后再触发中断。
好吧,这是我个人的想法。
希望有好心人能够帮我解答,其实还是挺急的。
谢谢! 測距的模組是用紅外線還是超音波??模組型號是哪一個??
隨風大俠 发表于 2014-2-7 19:51 static/image/common/back.gif
測距的模組是用紅外線還是超音波??模組型號是哪一個??
用的是 2Y0A02, 教材里面读取距离就是直接 digitalRead. czh007007 发表于 2014-2-7 19:57 static/image/common/back.gif
用的是 2Y0A02, 教材里面读取距离就是直接 digitalRead.
你程式碼可以貼出來讓大家看看嗎??這樣比較好幫。
本帖最后由 czh007007 于 2014-2-7 21:55 编辑
隨風大俠 发表于 2014-2-7 20:04 static/image/common/back.gif
你程式碼可以貼出來讓大家看看嗎??這樣比較好幫。
全部就有点长了,全文如下:
/*
This example shows how to use PWM to control the motors
using the analogWrite() function.
*/
int IRpin = 1;
#define motor1A 3
#define motor1C 5
#define motor2A 6
#define motor2C 9
#define motor3A 10
#define motor3C 11
#define clkwise 0
#define anticlkwise 1
#define M1
#define M2
#define M3
#define cos60 0.5
#define sin60 0.866
float a1_sp;
float a2_sp;
float a3_sp;
char a1_speed;
char a2_speed;
char a3_speed;
int halt = 0; // how bright the LED is
int incAmount = 5; // how many points to fade the LED by
String inputString = ""; // a string to hold incoming data
boolean stringComplete = false;// whether the string is complete
void setup(){
// declare pin 9 to be an output:
pinMode(motor1A, OUTPUT);
pinMode(motor1C, OUTPUT);
pinMode(motor2A, OUTPUT);
pinMode(motor2C, OUTPUT);
pinMode(motor3A, OUTPUT);
pinMode(motor3C, OUTPUT);
// initialize serial:
Serial.begin(9600);
// reserve 200 bytes for the inputString:
inputString.reserve(20);
}
void loop(){
// call motor routine:
float distance = 65*pow(analogRead(IRpin)*0.0048828125, -1.10);
Serial.println(65*pow(analogRead(IRpin)*0.0048828125, -1.10));
while(65*pow(analogRead(IRpin)*0.0048828125, -1.10)>20)
movement();
while(65*pow(analogRead(IRpin)*0.0048828125, -1.10) > 10 && 65*pow(analogRead(IRpin)*0.0048828125, -1.10) < 20)
{
analogWrite(motor1A,0);
analogWrite(motor1C,0);
analogWrite(motor2A,0);
analogWrite(motor2C,0);
analogWrite(motor3A,0);
analogWrite(motor3C,0);
}
}
void movement()
{
spin(100,5000);
forward(100, 5000);
rightward(100,4000);
}
void control(char robospeed, float roboangle, char roborotate ){
// assign the correct output pin for motor_num
// v.v1 + roborotate
a1_sp= - cos60*robospeed*cos(roboangle) + sin60*robospeed*sin(roboangle) + roborotate;
// v.v2+ roborotate
a2_sp= 1*robospeed*cos(roboangle) + roborotate;
// v.v3+ roborotate
a3_sp= - cos60*robospeed*cos(roboangle) - sin60*robospeed*sin(roboangle) + roborotate;
#ifdef M1
if (a1_sp > 0) {
a1_speed = a1_sp;
motor(1, a1_speed, clkwise);
}
else {
a1_sp *=-1;
a1_speed = a1_sp;
motor(1, a1_speed, anticlkwise);
}
#endif
#ifdef M2
if (a2_sp > 0) {
a2_speed = a2_sp;
motor(2, a2_speed, clkwise);
}
else {
a2_sp *=-1;
a2_speed = a2_sp;
motor(2, a2_speed, anticlkwise);
}
#endif
#ifdef M3
if (a3_sp > 0) {
a3_speed = a3_sp;
motor(3, a3_speed, clkwise);
}
else {
a3_sp *=-1;
a3_speed = a3_sp;
motor(3, a3_speed, anticlkwise);
}
#endif
}
/*
Setting the speed and direct control of a specific motor
*/
void motor(char motor_num, char speed_val, boolean direct ){
char motorLA;
char motorLC;
// assign the correct output pin for motor_num
switch(motor_num) {
case 1:
motorLA = motor1A;
motorLC = motor1C;
break;
case 2:
motorLA = motor2A;
motorLC = motor2C;
break;
case 3:
motorLA = motor3A;
motorLC = motor3C;
break;
}
if (direct) { // direct = 0:clockwisedirect = 1:anti-clockwise
analogWrite(motorLA, 0);
analogWrite(motorLC, speed_val);
}
else {
analogWrite(motorLC, 0);
analogWrite(motorLA, speed_val);
}
}
void forward(int speedval, int time)
{
control (speedval, 3.141*0.5, 0);
delay(time);
}
void rightward(int speedval,int time)
{
control (speedval, 0, 0);
delay(time);
}
void spin(int speedval, int time)
{
analogWrite(motor1A,speedval);
analogWrite(motor1C,0);
analogWrite(motor2A,speedval);
analogWrite(motor2C,0);
analogWrite(motor3A,speedval);
analogWrite(motor3C,0);
delay(time);
}
不懂编程,感觉可以设定门限然后触发中断,应该和单片机的中断处理差不多吧。
页:
[1]