encoder_diffbot.cpp
Go to the documentation of this file.
1 #include "encoder_diffbot.h"
2 
3 
4 diffbot::Encoder::Encoder(ros::NodeHandle& nh, uint8_t pin1, uint8_t pin2, int encoder_resolution)
5  : nh_(nh)
6  , encoder(pin1, pin2)
7  , encoder_resolution_(encoder_resolution)
8  , prev_update_time_(0, 0)
9  , prev_encoder_ticks_(0)
10 {
11 
12 }
13 
15 {
16  long encoder_ticks = encoder.read();
17  // This function calculates the motor's rotational (angular) velocity based on encoder ticks and delta time
18  ros::Time current_time = nh_.now();
19  ros::Duration dt = current_time - prev_update_time_;
20 
21  // Convert the delta time to seconds
22  double dts = dt.toSec();
23 
24  //calculate wheel's speed (RPM)
25  double delta_ticks = encoder_ticks - prev_encoder_ticks_;
26  double delta_angle = ticksToAngle(delta_ticks);
27 
28  joint_state_.angular_position_ += delta_angle;
29 
30 
31  joint_state_.angular_velocity_ = delta_angle / dts;
32 
33  prev_update_time_ = current_time;
34  prev_encoder_ticks_ = encoder_ticks;
35 
36  return joint_state_;
37 }
38 
40 {
41  return joint_state_.angular_position_;
42 }
43 
44 
46 {
47  return joint_state_.angular_velocity_;
48 }
49 
50 double diffbot::Encoder::ticksToAngle(const int &ticks) const
51 {
52  // Convert number of encoder ticks to angle in radians
53  double angle = (double)ticks * (2.0*M_PI / encoder_resolution_);
54  return angle;
55 }
56 
57 
59 {
60  long encoder_ticks = encoder.read();
61  //this function calculates the motor's RPM based on encoder ticks and delta time
62  ros::Time current_time = nh_.now();
63  ros::Duration dt = current_time - prev_update_time_;
64 
65  //convert the time from milliseconds to minutes
66  double dtm = dt.toSec() / 60;
67  double delta_ticks = encoder_ticks - prev_encoder_ticks_;
68 
69  //calculate wheel's speed (RPM)
70 
71  prev_update_time_ = current_time;
72  prev_encoder_ticks_ = encoder_ticks;
73 
74  return (delta_ticks / encoder_resolution_) / dtm;
75 }
diffbot::Encoder::angularPosition
double angularPosition()
Definition: encoder_diffbot.cpp:39
angle
TFSIMD_FORCE_INLINE tfScalar angle(const Quaternion &q1, const Quaternion &q2)
diffbot::JointState
Definition: encoder_diffbot.h:15
diffbot::Encoder::jointState
JointState jointState()
Definition: encoder_diffbot.cpp:14
nh
ros::NodeHandle nh
Definition: main.cpp:8
ros::Time
diffbot::Encoder::getRPM
int getRPM()
get revolutions per minute
Definition: encoder_diffbot.cpp:58
diffbot::Encoder::angularVelocity
double angularVelocity()
Get the measure the angular joint velocity.
Definition: encoder_diffbot.cpp:45
DurationBase< Duration >::toSec
double toSec() const
diffbot::Encoder::ticksToAngle
double ticksToAngle(const int &ticks) const
Convert number of encoder ticks to angle in radians.
Definition: encoder_diffbot.cpp:50
diffbot::Encoder::Encoder
Encoder(ros::NodeHandle &nh, uint8_t pin1, uint8_t pin2, int encoder_resolution)
Construct a diffbot::Encoder providing access to quadrature encoder ticks and angular joint velocity.
Definition: encoder_diffbot.cpp:4
ros::Duration
encoder_diffbot.h
ros::NodeHandle
ros::Time::now
static Time now()


diffbot_base
Author(s):
autogenerated on Thu Sep 7 2023 02:35:23