-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest1.ino
More file actions
87 lines (82 loc) · 1.7 KB
/
test1.ino
File metadata and controls
87 lines (82 loc) · 1.7 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
#include <stdio.h>
#include <ros.h>
#include <stdlib.h>
#include <std_msgs/Int16.h>
#include <std_msgs/Float32.h>
#include <pub/Num.h>
#include <Servo.h>
Servo myservo;
Servo myservo2;
int pos = 0;
int flag=0;
int upflag=0;
int pos2=0;
ros::NodeHandle n;
pub::Num var;
ros::Publisher chat("chatter",&var);
#define trigPin 13
#define echoPin 12
float theta=-3.14159;
float phi=3.14159/3.0;
void setup()
{
pinMode(trigPin, OUTPUT);
pinMode(echoPin, INPUT);
n.initNode();
myservo.attach(10);
n.advertise(chat);
myservo2.attach(5);
myservo2.write(0);
myservo.write(0);
}
void loop()
{
long duration, distance;
digitalWrite(trigPin, LOW); // Added this line
delayMicroseconds(2); // Added this line
digitalWrite(trigPin, HIGH);
// delayMicroseconds(1000); - Removed this line
delayMicroseconds(10); // Added this line
digitalWrite(trigPin, LOW);
duration = pulseIn(echoPin, HIGH);
distance = (duration/2) / 29.1;
if (distance >= 200 || distance <= 0)
{
var.r=0;
var.theta=theta;
var.phi=phi;
}
else {
int16_t sum=distance;
var.r=sum;
var.theta=theta;
var.phi=phi;
}
if(pos==180)
{
flag=1;
upflag=1;
}
else if(pos==0)
{
flag=0;
upflag=1;
}
if(upflag==1)
{
pos2=pos2+2;
myservo2.write(pos2);
upflag=0;
}
if(flag==1 && upflag==0)
pos=pos-1;
else if(flag==0 && upflag==0)
pos=pos+1;
theta=(((float)(pos)*3.14159)/180.0);
phi=(((float)(pos2)*3.14159)/180.0);
myservo.write(pos);
// tell servo to go to position in variable 'pos'
chat.publish(&var);
delay(100);
n.spinOnce();
}