-
Notifications
You must be signed in to change notification settings - Fork 5
/
ros_drive.ino
162 lines (112 loc) · 3.26 KB
/
ros_drive.ino
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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
#include <ros.h>
#include <geometry_msgs/Twist.h>
#include <math.h>
#include <std_msgs/String.h>
#include <SoftwareSerial.h>
#include <SabertoothSimplified.h>
SoftwareSerial SWSerial(NOT_A_PIN, 11); // RX on no pin (unused), TX on pin 11 (to S1).
SabertoothSimplified ST(SWSerial); // Use SWSerial as the serial port.
SoftwareSerial portROS(0, 1);
int pulPin = 3; // Pulse Pin for steering motor controller
int dirPin = 5; // Direction Pin for steering motor controller
int enblPin = 6; // Enbl Pin for steering motor controller
int ledPin = 13; // LED
const long CONTROL_TIMEOUT = 1000; //ms to wait before killing motors
void cmdVelCallback(const geometry_msgs::Twist&);
ros::NodeHandle handle;
ros::Subscriber<geometry_msgs::Twist> subscriber("cmd_vel", &cmdVelCallback);
geometry_msgs::Twist msg;
ros::Publisher chatter_pub("cmd_vel", &msg);
unsigned long lastData = 0;
int cnt=0;
void cmdVelCallback(const geometry_msgs::Twist &twist)
{
if (cnt%2==0)
digitalWrite(2, HIGH);
else
digitalWrite(2, LOW);
cnt=cnt+1;
// this function is called as soon as there is any cmd_vel command in the ros network
lastData = millis();
// this variable is given forth/back velocity in range of [-0.5 +0.5]
const float linear = twist.linear.x;
// this variable is given left/right velocity in range of [-1 +1]
const float spin = twist.angular.z;
// give movement to forth/back direction
vorwaerts(int(66*linear));
// if given command is right means spin is negative
if (spin<0)
{
rechtsLenken(int(-100*spin),10);
}
else if (spin>0)
{
linksLenken(int(100*spin),10);
}
}
void setup() {
pinMode(2, OUTPUT);
SWSerial.begin(9600);
portROS.begin(57600);
pinMode(pulPin, OUTPUT);
pinMode(dirPin, OUTPUT);
pinMode(enblPin, OUTPUT);
pinMode(ledPin, OUTPUT);
digitalWrite(pulPin, LOW);
digitalWrite(ledPin, LOW);
digitalWrite(enblPin, LOW);
digitalWrite(dirPin, LOW);
digitalWrite(enblPin, HIGH);
delay(100);
digitalWrite(enblPin, LOW);
handle.initNode();
handle.subscribe(subscriber);
handle.advertise(chatter_pub);
}
void loop()
{
handle.spinOnce();
if(millis() - lastData >= CONTROL_TIMEOUT)
{
lastData=millis();
msg.linear.x = 0;
msg.angular.z = 0;
chatter_pub.publish(&msg);
}
}
// Alle eigenen Funktionen
int rechtsSpeed(int speed) {
digitalWrite(pulPin, HIGH);
digitalWrite(ledPin, HIGH); //schauen ob dirPin a no nötig ist
digitalWrite(pulPin, LOW);
digitalWrite(ledPin, LOW);
delay(speed); // LESS DELAY = FASTER MOTOR SPEED (more pulses per timeframe)
}
int linksSpeed(int speed) {
digitalWrite(pulPin, HIGH);
digitalWrite(ledPin, HIGH);
digitalWrite(dirPin, HIGH);
digitalWrite(pulPin, LOW);
digitalWrite(ledPin, LOW);
digitalWrite(dirPin, LOW);
delay(speed); // LESS DELAY = FASTER MOTOR SPEED (more pulses per timeframe)
}
int linksLenken(int dauer, int geschw) {
for(int i=0; i <= dauer; i++){
linksSpeed(geschw);
}
}
int rechtsLenken(int dauer, int geschw) {
for(int i=0; i <= dauer; i++){
rechtsSpeed(geschw);
}
}
int vorwaerts(int gas) {
ST.motor(1,-gas);
}
int rueckwaerts(int rueck) {
ST.motor(1,rueck);
}
void standgas() {
ST.motor(1,0);
}