-
Notifications
You must be signed in to change notification settings - Fork 0
/
arduino_normal_node.cpp
87 lines (75 loc) · 1.56 KB
/
arduino_normal_node.cpp
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
87
#include <ros.h>
#include <Arduino.h>
#include <std_msgs/Float64.h>
#include <math.h>
float angle;
int lSpeed = 3;
int lDirn1 = 4;
int lDirn2 = 7;
int rSpeed = 11;
int rDirn1 = 12;
int rDirn2 = 13;
ros::NodeHandle nh;
void straight()
{
digitalWrite(lDirn1, HIGH);
digitalWrite(lDirn2, LOW);
digitalWrite(rDirn1, HIGH);
digitalWrite(rDirn2, LOW);
analogWrite(lSpeed, 150);
analogWrite(rSpeed, 150);
}
void turn_left_updated()
{
digitalWrite(lDirn1, HIGH);
digitalWrite(lDirn2, LOW);
digitalWrite(rDirn1, HIGH);
digitalWrite(rDirn2, LOW);
analogWrite(lSpeed, 50);
analogWrite(rSpeed, 125);
}
void turn_right_updated()
{
digitalWrite(lDirn1, HIGH);
digitalWrite(lDirn2, LOW);
digitalWrite(rDirn1, HIGH);
digitalWrite(rDirn2, LOW);
analogWrite(lSpeed, 125);
analogWrite(rSpeed, 50);
}
void message_callback(const std_msgs::Float64 &theta)
{
angle = theta.data;
nh.loginfo("Angle: ");
// nh.loginfo(angle);
if (angle < -8)
{
turn_right_updated();
nh.loginfo("turning right...");
}
else if (angle > 8)
{
nh.loginfo("turning left...");
turn_left_updated();
}
else
{
straight();
}
}
ros::Subscriber<std_msgs::Float64> sub("/robocon2018/error", &message_callback);
void setup()
{
pinMode(lDirn1, OUTPUT);
pinMode(lDirn2, OUTPUT);
pinMode(lSpeed, OUTPUT);
pinMode(rDirn1, OUTPUT);
pinMode(rDirn2, OUTPUT);
pinMode(rSpeed, OUTPUT);
nh.initNode();
nh.subscribe(sub);
}
void loop()
{
nh.spinOnce();
}