forked from IEEE-NITK/SLAMBot
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdifferential_drive.ino
101 lines (78 loc) · 2.25 KB
/
differential_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
#include <ros.h>
#include <geometry_msgs/Twist.h>
#include <nav_msgs/Odometry.h>
#include <sensor_msgs/Imu.h>
#include <sensor_msgs/JointState.h>
#include <tf2_msgs/TFMessage.h>
//Declare global variables
nav_msgs::Odometry odometry;
sensor_msgs::Imu imu;
sensor_msgs::JointState joint_states;
tf2_msgs::TFMessage transform;
geometry_msgs::Twist cmd_vel;
int motor_right_pin1 = 2, motor_right_pin1 = 3, motor_left_pin1 = 4, motor_left_pin1 = 5;
//Calculate motor velocities inside this
void velocity_callback(const geometry_msgs::Twist& vel_msg)
{
}
//Declare global objects
ros::NodeHandle node;
ros::Publisher odometry_publisher("odom", &odometry);
ros::Publisher imu_publisher("imu", &imu);
ros::Publisher joint_state_publisher("joint_states", &joint_states);
ros::Publisher transform_publisher("tf", &transform);
ros::Subscriber<geometry_msgs::Twist> twist_subscriber("cmd_vel", velocity_callback);
//Calculate transform for base_footprint
void calculate_transform()
{
}
//Calculate odometry from encoder values
void calculate_odometry()
{
}
//Calculate joint states from encoder values
void calculate_joint_states()
{
}
//Calculate acceleration from IMU values
void calculate_imu()
{
}
//Initialise nodes, publishers, subscribers and serial monitor
void setup()
{
Serial.begin(115200);
Serial.println("Starting...");
pinMode(motor_right_pin1, OUTPUT);
pinMode(motor_right_pin2, OUTPUT);
pinMode(motor_left_pin1, OUTPUT);
pinMode(motor_left_pin2, OUTPUT);
pinMode(9, OUTPUT);
pinMode(10, OUTPUT);
node.initNode();
node.advertise(odometry_publisher);
node.advertise(imu_publisher);
node.advertise(transform_publisher);
node.advertise(joint_state_publisher);
node.subscribe(twist_subscriber);
}
//Write your program logic
void loop()
{
//Controlling speed (0 = off and 255 = max speed):
analogWrite(9, 100); //ENA pin
analogWrite(10, 200); //ENB pin
//Controlling spin direction of motors:
digitalWrite(motor1pin1, HIGH);
digitalWrite(motor1pin2, LOW);
digitalWrite(motor2pin1, HIGH);
digitalWrite(motor2pin2, LOW);
delay(1000);
digitalWrite(motor1pin1, LOW);
digitalWrite(motor1pin2, HIGH);
digitalWrite(motor2pin1, LOW);
digitalWrite(motor2pin2, HIGH);
delay(1000);
node.spinOnce();
delay(100);
}