Studica - Roscpp Documentation
VMX-pi ROS Library
TitanDriver_ros.h
Go to the documentation of this file.
1#pragma once
2
3#include "VMXPi.h"
4#include <ros/ros.h>
5
6#include "vmxpi_utils.h"
7#include <stdint.h>
8#include <stdio.h>
9#include <thread>
10#include <unistd.h>
11#include <iostream>
12#include <sstream>
13#include <sys/syscall.h>
14#include <sys/types.h>
15
16#include <vmxpi_ros/TitanInfo.h>
17#include <vmxpi_ros/UniqueID.h>
18#include <vmxpi_ros/LimitSwitch.h>
19#include <std_msgs/Int16.h>
20#include <std_msgs/Int32.h>
21#include <std_msgs/UInt16.h>
22#include <std_msgs/Float32.h>
23#include "TitanConstants_ros.h"
24
25class TitanDriver : public Utils {
26 private:
27 VMXCAN *can;
28 VMXCANReceiveStreamHandle canrxhandles[1];
29 double m_speed[4] = {0,0,0,0};
30 int deviceID = 42;
31 bool enabled;
32 int frequency = 15600;
33 int baseID;
34 int autoRepeatPeriodInMilliseconds = 10;
35 std::thread runth;
36
37 double ENCODER_0_DISTANCE_PER_TICK = 0, ENCODER_1_DISTANCE_PER_TICK = 0,
38 ENCODER_2_DISTANCE_PER_TICK = 0, ENCODER_3_DISTANCE_PER_TICK = 0;
39 int32_t enc_count[4], rpm[4];
40 public:
41
42 TitanDriver(int deviceID, int frequency_set, VMXCAN *can_);
43
44 TitanDriver(int deviceID, VMXCAN *can);
45
46 ~TitanDriver();
47
48 // Run loop on separate thread to disable/enable Titan
49 void Run();
50
51 // Configure specified motor's frequency
52 void ConfigureMotor(uint8_t motor);
53
54 // Set specified motor's speed (speed_set = 0 -> 1.0)
55 void SetMotorSpeed(double speed_set, int motor);
56
57 // Stop specified motor
58 void StopMotor(uint8_t motor);
59
60 // Stop all motors (todo: remove hardcoded motors)
61 void StopAllMotors();
62
63 // Reset encoder count for specified motor
64 void ResetEncoder(uint8_t motor);
65
66 // Resets all encoder counts
67 void ResetAllEncoders();
68
69 // Set the stop mode on the specified motor
70 // mode: (0 = coast, 1 = break)
71 void SetMotorStopMode(uint8_t motor, int8_t mode);
72
73 // Get Titan's Unique ID
74 void GetUniqueID(vmxpi_ros::UniqueID *res_msg);
75
76 // Get Titan's firmware and hardware information
77 void GetInfo(vmxpi_ros::TitanInfo *res_msg);
78
79 // Get all motors' frequency
80 void GetMotorFreq(std_msgs::UInt16 res_msg[]);
81
82 void GetCountRPM(int8_t motor);
83
84 /* Get the count of the specified encoder, note this
85 * is not the encoder distance */
86 void GetEncoderCount(std_msgs::Int32 *res_msg, int8_t encoder);
87
88 /* Configure the encoder's distance per tick
89 * For more info on this: https://docs.wsr.studica.com/en/latest/docs/Sensors/encoders.html
90 * encoderPPR = encoder pulse per ratio
91 * gearRatio = rotation speed of motor to rotation speed of wheel */
92 void ConfigureEncoder(int8_t encoder, double radius, double encoderPPR, double gearRatio);
93
94 // Calculate the encoder's distance based on its current count
95 void GetEncoderDist(std_msgs::Float32 *res_msg, int8_t encoder);
96
97 // Get the RPM of the specified encoder
98 void GetRPM(std_msgs::Int16 *res_msg, uint8_t motor);
99
100 /* Get the limit switch values for each motor as a boolean value
101 * {M0 L H, M0 L L, M1 L H, M1 L L, M2 L H, M2 L L, M3 L H, M3 L L}
102 * L = low limit switch, H = high limit switch */
103 void GetLimitSwitch(vmxpi_ros::LimitSwitch *res_msg);
104
105 // Get the current MCU (CPU) temperature
106 void GetMCUTemp(std_msgs::Float32 *res_msg);
107
108 // Get specified motor's speed
109 void GetSpeed(std_msgs::Float32 *res_msg, uint8_t motor);
110
111 // Sets the current flag being sent by the run loop to enabled
112 void Enable();
113
114 // Sets the current flag being sent by the run loop to disabled
115 void Disable();
116
117 private:
118 bool CheckCANStatus(VMXErrorCode vmxerr) {
119 VMXCANBusStatus can_bus_status;
120 if (!can->GetCANBUSStatus(can_bus_status, &vmxerr)) {
121 ROS_INFO("Error getting CAN BUS Status.");
122 return false;
123 DisplayVMXError(vmxerr);
124 } else {
125 if(can_bus_status.busWarning)
126 ROS_INFO("CAN Bus Warning.\n");
127 if(can_bus_status.busPassiveError)
128 ROS_INFO("CAN Bus in Passive mode due to errors.");
129 if(can_bus_status.busOffError)
130 ROS_INFO("CAN Bus Transmitter Off due to errors.");
131 if(can_bus_status.transmitErrorCount > 0)
132 ROS_INFO("CAN Bus Tx Error Count: %d", can_bus_status.transmitErrorCount);
133 if(can_bus_status.receiveErrorCount > 0)
134 ROS_INFO("CAN Bus Rx Error Count: %d", can_bus_status.receiveErrorCount);
135 if(can_bus_status.busOffCount > 0)
136 ROS_INFO("CAN Bus Tx Off Count: %d", can_bus_status.busOffCount);
137 if(can_bus_status.txFullCount > 0)
138 ROS_INFO("CAN Bus Tx Full Count: %d", can_bus_status.txFullCount);
139 if(can_bus_status.hwRxOverflow)
140 ROS_INFO("CAN HW Receive Overflow detected.");
141 if(can_bus_status.swRxOverflow)
142 ROS_INFO("CAN SW Receive Overflow detected.");
143 if(can_bus_status.busError)
144 ROS_INFO("CAN Bus Error detected.");
145 if(can_bus_status.wake)
146 ROS_INFO("CAN Bus Wake occured.");
147 if(can_bus_status.messageError)
148 ROS_INFO("CAN Message Error detected.");
149 else {
150 //ROS_INFO("CAN working");
151 return true;
152 }
153 return false;
154 }
155 }
156
157 // Blackboard read function utilizing VMXPi's blackboard functionality
158 void ReadCANBlackboard(VMXCANReceiveStreamHandle handle, int msgID, VMXCANTimestampedMessage* data, uint32_t length, uint64_t timestamp, VMXErrorCode vmxerr);
159};
160
Definition: vmxpi_utils.h:5
void DisplayVMXError(VMXErrorCode vmxerr)
Displays the VMXError.
Definition: vmxpi_utils.h:14