Arduino + processing
processing吧
全部回复
仅看楼主
level 1
Alex赖33 楼主
我在做超声波雷达
怎么连接啊???
找大神!!
2018年06月22日 12点06分 1
level 1
Alex赖33 楼主
Arduino:
#include<stdio.h>
#include <Servo.h>
Servo mServo; //创建一个舵机控制对象
//当前角度
int mAngleNum = 0;
//当前是正向旋转还是反向旋转
char mFront = 0;
//超声波测距引脚
const int mTrigPin = 10;
const int mEchoPin = 11;
//当前距离
int mDistance = 0;
//像串口发送数据 发送到processing
void sendStatusToSerial();
//测距
void ranging();
void setup() {
// put your setup code here, to run once:
Serial.begin(9600);
mServo.attach(9); // 该舵机由arduino第九脚控制
pinMode(mTrigPin, OUTPUT);
// 要检测引脚上输入的脉冲宽度,需要先设置为输入状态
pinMode(mEchoPin, INPUT);
}
void loop() {
// put your main code here, to run repeatedly:
//多角度设置
mServo.write(180 - mAngleNum);
//超声波测距
ranging();
//发数据
sendStatusToSerial();
delay(60);
if ( mFront == 0 )
{
mAngleNum ++;
if ( mAngleNum > 180 )
{
mFront = 1;
}
}
else
{
mAngleNum --;
if ( mAngleNum < 0 )
{
mFront = 0;
}
}
}
//发送当前状态到串口
void sendStatusToSerial()
{
char mAngleStr[6];
char mDistanceStr[6];
sprintf( mAngleStr, "%d", mAngleNum);
sprintf( mDistanceStr, "%d", mDistance);
delayMicroseconds(2);
Serial.print( "<" );
Serial.print(mAngleStr);
Serial.print("|");
Serial.print(mDistanceStr);
Serial.print( ">" );
Serial.println();
delayMicroseconds(2);
}
//测距
void ranging()
{
// 产生一个10us的高脉冲去触发TrigPin
digitalWrite(mTrigPin, LOW); //低高低电平发一个短时间脉冲去TrigPin
delayMicroseconds(2);
digitalWrite(mTrigPin, HIGH);
delayMicroseconds(10);
digitalWrite(mTrigPin, LOW);
mDistance = pulseIn(mEchoPin, HIGH) / 58.0; //将回波时间换算成cm
Serial.println(mDistance);
mDistance = (int(mDistance * 100.0)) / 100.0; //保留两位小数
Serial.print(mDistance);
Serial.print("cm");
Serial.println();
digitalWrite(mTrigPin, LOW);
delayMicroseconds(2);
digitalWrite(mTrigPin, HIGH);
delayMicroseconds(10);
digitalWrite(mTrigPin, LOW);
mDistance = pulseIn(mEchoPin, HIGH) / 58.00;
Serial.print(mDistance);
Serial.print("cm");
Serial.println();
}
2018年06月22日 12点06分 2
level 1
Alex赖33 楼主
processing:
import processing.serial.*;
Serial myPort; // Create object from Serial class
int val; // Data received from the serial port
//base config
int mWidth = 600;
int mHeight = 300;
int mWidthHalf = mWidth / 2;
int mHeightHalf = mHeight / 2;
int mBlock = 20;
int mBlockOffset = mWidth / mBlock;
float mMaxDistance = mWidth / 6.0f;
//current angle
int mAngleNum = 0;
//current distance
int mDistanceNum = 0;
//all distance
int[] mDistanceArr = new int[181];
void setup()
{
size(600, 300);
frameRate(60);
String portName = Serial.list()[0];
myPort = new Serial(this, portName, 9600);
background(0);
}
void draw()
{
if ( myPort.available() > 0) {
val = myPort.read();
if( val == '<')
{
mAngleNum = 0;
mDistanceNum = 0;
while( ( val = myPort.read() ) != '|' )
{
mAngleNum = 10 * mAngleNum + val - '0';
}
while( ( val = myPort.read() ) != '>' )
{
mDistanceNum = 10 * mDistanceNum + val - '0';
}
if( mDistanceNum > 150 )
{
mDistanceArr[mAngleNum] = -1;
}
else
{
mDistanceArr[mAngleNum] = mDistanceNum;
}
}
}
//draw background line
drawBg();
//scan angle
drawScan(mAngleNum);
//distance
drawBarrier();
}
void drawBg()
{
background(0);
//print("draw bg");
stroke(21,113,10, 51);
//line(0, 0, 300, 300);
for( int x = 0; x < mBlockOffset; ++x )
{
//print("line:" + x * mBlock);
line(0, x * mBlock, mWidth, x * mBlock );
line(x*mBlock, 0, x * mBlock, mHeight );
if( x % 5 == 0 )
{
stroke(21,113,10, 250);
line(0, x * mBlock, mWidth, x * mBlock );
line(x*mBlock, 0, x * mBlock, mHeight );
stroke(21,113,10, 51);
}
}
stroke(21,113,10, 200);
fill(21,113,10, 200);
arc( mWidthHalf, mHeight, mBlock, mBlock, PI, PI * 2, PIE);
stroke(21,113,10, 254);
noFill();
arc( mWidthHalf, mHeight, mWidth, mWidth, PI, PI * 2, PIE);
}
void drawScan( int num )
{
println( "angle:" + num );
noStroke();
fill(21,113,10, 50);
arc( mWidthHalf, mHeight, mWidth, mWidth, PI + ( num / 180.0f - 0.1f ) * PI, PI + ( num / 180.0f + 0.1f ) * PI, PIE);
}
void drawBarrier()
{
//println( "distance:" + num);
noStroke();
fill(21,113,10, 254);
for( int i = 0; i < 181; i ++ )
{
int a, b, distance;
distance = a = b = mDistanceArr[i];
if( i - 1 >= 0 )
a = mDistanceArr[ i - 1 ];
if( i + 1 <= 180 )
b = mDistanceArr[ i + 1 ];
if( distance < 5 || distance > mMaxDistance ) continue;
if( a < 5 || a > mMaxDistance ) a = distance;
if( b < 5 || b > mMaxDistance ) b = distance;
distance = (a + b + distance) / 3;
if( distance < 5 || distance >= mMaxDistance ) continue;
//distance = 60 angle = 45;
//mWidth / 2;
float scale = mWidthHalf / mMaxDistance;
int tmpDistance = (int)(distance * scale);
int px = (int)(mWidthHalf - tmpDistance * cos(i * PI/180));
int yy = (int)(tmpDistance * sin(i * PI/180));
int py = 300 - yy;
println("scale:" + scale + " angle:" + i + " sin():" + sin(i * PI/180) + " distance:" + distance + " tmpDistance:" + tmpDistance + " x:" + px + " yy:" + yy + " py:" + py);
ellipse(px, py, 30, 30);
}
}
2018年06月22日 12点06分 3
level 1
哥们,你的做好了没,
2018年06月24日 02点06分 4
1