forked from fellipeac2/ieee-open-stm32F103C8
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.cpp
70 lines (55 loc) · 1.6 KB
/
main.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
#include "stm32f103c8t6.h"
#include <mbed.h>
#include "Crawler.h"
#include <ros.h>
#include <std_msgs/Float32.h>
#include <std_msgs/Int16.h>
#define ENC1_A PB_12
#define ENC1_B PB_13
DigitalOut led(LED1);
AnalogIn battery(PA_0);
Crawler cr_left(Crawler::Type::LEFT);
Crawler cr_right(Crawler::Type::RIGHT);
ros::NodeHandle nh;
std_msgs::Int16 encoder_left_msg;
std_msgs::Int16 encoder_right_msg;
ros::Publisher pub_encoder_left("encoder_left", &encoder_left_msg);
ros::Publisher pub_encoder_right("encoder_right", &encoder_right_msg);
void velocity_left_cb(const std_msgs::Float32& msg) {
cr_left.set_velocity(msg.data);
}
void velocity_right_cb(const std_msgs::Float32& msg) {
cr_right.set_velocity(msg.data);
}
ros::Subscriber<std_msgs::Float32> sub_vel_left("cmd_vel_left", &velocity_left_cb);
ros::Subscriber<std_msgs::Float32> sub_vel_right("cmd_vel_right", &velocity_right_cb);
void wait_for_battery() {
float battery_status;
do {
battery_status = battery.read();
wait_ms(1000);
} while(battery_status < 0.33);
wait_ms(1000);
}
int main() {
// setup begin
confSysClock();
wait_for_battery();
cr_left.set_velocity(0);
cr_right.set_velocity(0);
nh.initNode();
nh.subscribe(sub_vel_left);
nh.subscribe(sub_vel_right);
led = 0;
nh.advertise(pub_encoder_left);
nh.advertise(pub_encoder_right);
// setup end
while (true) {
encoder_left_msg.data= cr_left.getMotor()->getEncoder().get_pulses();
encoder_right_msg.data = cr_right.getMotor()->getEncoder().get_pulses();
pub_encoder_left.publish(&encoder_left_msg);
pub_encoder_right.publish(&encoder_right_msg);
wait_ms(10);
nh.spinOnce();
}
}