2 #include "rclcpp/rclcpp.hpp"
3 #include "rclcpp/qos.hpp"
5 #include <sensor_msgs/msg/range.hpp>
21 using std::placeholders::_1;
23 galaxis::ros::create_publisher(
this, m_measurementPubSPtr);
25 rclcpp::QoS qos = galaxis::ros::get_qos_profile(galaxis::ros::QoS_Profile_t::QOS_RELIABLE);
28 galaxis::ros::create_publisher(
this, m_buttonStatePubSPtr);
30 galaxis::ros::create_subscription(
this,
31 m_controlValuesSubSPtr,
34 galaxis::ros::create_subscription(
this,
35 m_settingsValuesSubSPtr,
38 galaxis::ros::create_subscription(
this,
44 m_velocityPubSPtr = this->create_publisher<std_msgs::msg::Float32>(
"/measurements/speed", qos);
49 m_ecuHandlerSPtr = p_ecuHandlerSPtr;
51 log(
"successfully initialised...");
55 log_debug(
"send control values via ECU handler");
59 packet.motor_erpm = p_msgSPtr->set_motor_erpm;
60 packet.motor_current = p_msgSPtr->set_motor_current;
61 log_debug(
"motor current ", p_msgSPtr->set_motor_current);
62 packet.servo_front = std::min(
int(p_msgSPtr->set_servo_front) + 3, 127);
63 packet.servo_rear = p_msgSPtr->set_servo_rear;
64 packet.brake = p_msgSPtr->set_brakes ? 1 : 0;
66 uint8_t headlight = 1;
67 uint8_t blinkerLeft = 0;
68 uint8_t blinkerRight = 0;
69 uint8_t brakelight = packet.brake;
71 if(m_lastLightsSPtr !=
nullptr && this->get_clock()->now() - m_lastLightsSPtr->header.stamp < rclcpp::Duration(2,0)) {
73 headlight = m_lastLightsSPtr->set_headlight;
75 blinkerLeft = m_lastLightsSPtr->set_blinker_left ? 0x2 : 0;
76 blinkerRight = m_lastLightsSPtr->set_blinker_right ? 0x2 : 0;
77 brakelight = m_lastLightsSPtr->set_brake_light;
92 log(
"preamble " , packet.preamble);
93 log(
"packet_id " , packet.packet_id);
94 log(
"motor_erpm " , packet.motor_erpm);
95 log(
"motor_current " , packet.motor_current);
96 log(
"servo_front " , packet.servo_front);
97 log(
"servo_rear " , packet.servo_rear);
98 log(
"lightState " , packet.lightState);
99 log(
"crc " , packet.crc);
101 m_ecuHandlerSPtr->TransmitPacket(packet);
105 log(
"New settings callback");
108 packet.firmware.motor_current_max = p_msgSPtr->firmware.motor_current_max;
109 log(
"motor_current_max: ", std::to_string(p_msgSPtr->firmware.motor_current_max));
110 packet.firmware.erpm_max = p_msgSPtr->firmware.erpm_max;
111 log(
"motor_current_max: ", std::to_string(p_msgSPtr->firmware.erpm_max));
112 packet.firmware.max_brake_current = p_msgSPtr->firmware.max_brake_current;
113 log(
"motor_current_max: ", std::to_string(p_msgSPtr->firmware.max_brake_current));
115 packet.firmware.max_servo_offset.front = p_msgSPtr->firmware.max_servo_offset.front;
116 log(
"motor_current_max: ", std::to_string(p_msgSPtr->firmware.max_servo_offset.front));
117 packet.firmware.max_servo_offset.rear = p_msgSPtr->firmware.max_servo_offset.rear;
118 log(
"motor_current_max: ", std::to_string(p_msgSPtr->firmware.max_servo_offset.rear));
120 packet.firmware.manual_override_difference = p_msgSPtr->firmware.manual_override_difference;
121 log(
"motor_current_max: ", std::to_string(p_msgSPtr->firmware.manual_override_difference));
122 packet.firmware.use_rc_to_steer = p_msgSPtr->firmware.use_rc_to_steer;
123 log(
"motor_current_max: ", std::to_string(p_msgSPtr->firmware.use_rc_to_steer));
124 packet.firmware.rc_use_erpm = p_msgSPtr->firmware.rc_use_erpm;
125 log(
"motor_current_max: ", std::to_string(p_msgSPtr->firmware.rc_use_erpm));
127 packet.firmware.servo_offset.front = p_msgSPtr->firmware.servo_offset.front;
128 log(
"motor_current_max: ", std::to_string(p_msgSPtr->firmware.servo_offset.front));
129 packet.firmware.servo_offset.rear = p_msgSPtr->firmware.servo_offset.rear;
130 log(
"motor_current_max: ", std::to_string(p_msgSPtr->firmware.servo_offset.rear));
132 packet.firmware.motor_current_max_rc = p_msgSPtr->firmware.motor_current_max_rc;
133 log(
"motor_current_max: ", std::to_string(p_msgSPtr->firmware.motor_current_max_rc));
134 packet.firmware.erpm_max_rc = p_msgSPtr->firmware.erpm_max_rc;
135 log(
"motor_current_max: ", std::to_string(p_msgSPtr->firmware.erpm_max_rc));
137 m_ecuHandlerSPtr->TransmitPacket(packet);
142 if(packet ==
nullptr) {
147 std::unique_ptr<MeasurementsType> msgUPtr = std::make_unique<MeasurementsType>();
149 msgUPtr->header.stamp = this->get_clock()->now();
150 msgUPtr->rc_active = packet->rc_active;
151 msgUPtr->motor_erpm = packet->motor_erpm;
152 msgUPtr->motor_duty_cycle = packet->motor_duty_cycle;
153 msgUPtr->linear_acceleration_x = packet->linear_acceleration_x;
154 msgUPtr->linear_acceleration_y = packet->linear_acceleration_y;
155 msgUPtr->linear_acceleration_z = packet->linear_acceleration_z;
156 msgUPtr->gyro_x = packet->gyro_x;
160 if(packet->tof_right == 0){
161 msgUPtr->time_of_flight_right = 2.f;
163 msgUPtr->time_of_flight_right = float(packet->tof_right) / 1000.f;
165 if(packet->tof_rear == 0){
166 msgUPtr->time_of_flight_rear = 2.f;
168 msgUPtr->time_of_flight_rear = float(packet->tof_rear) / 1000.f;
171 msgUPtr->orientation = packet->orientation;
172 msgUPtr->button_state = packet->buttonState;
173 msgUPtr->battery_level = packet->batteryLevel;
174 msgUPtr->applied_servo_front = packet->applied_servo_front;
175 msgUPtr->applied_servo_rear = packet->applied_servo_rear;
176 msgUPtr->applied_motor_current = packet->applied_motor_current;
177 msgUPtr->applied_motor_erpm = packet->applied_motor_erpm;
178 msgUPtr->applied_motor_brake = packet->applied_motor_brake;
179 for(
size_t i = 0; i < 3; i++) {
180 msgUPtr->battery_cell_level[i] = packet->batteryCellLevel[i];
183 static int count = 0;
186 std::array<double, 10> temp_array;
187 for(
size_t i = 0; i < temp_array.size(); ++i) {
188 temp_array[i] = diff_ms.at(i).count();
190 std::sort(temp_array.begin(), temp_array.end());
202 log(
"Max difference: ", temp_array[9]);
203 log(
"Min difference: ", temp_array[0]);
204 uint32_t sum_differences = 0;
205 for(
size_t i = 0; i < 10; i++) {
206 sum_differences += diff_ms.at(i).count();
208 float average_differences = (float)sum_differences / 10.0;
209 log(
"average differences: ", average_differences);
210 log(
"rc_active: ", packet->rc_active);
211 log(
"Motor ERPM: ", packet->motor_erpm);
212 float rpm = packet->motor_erpm / 7;
213 log(
"Motor RPM: ", rpm);
215 log(
"Velocity: ", velocity);
216 log(
"Duty Cycle: ", packet->motor_duty_cycle);
217 if(packet->tof_right == 0){
218 log(
"tof_right: 2.0");
220 log(
"tof_right: ", packet->tof_right / 1000.f);
222 if(packet->tof_rear == 0){
223 log(
"tof_rear: 2.0");
225 log(
"tof_rear: ", packet->tof_rear / 1000.f);
229 log(
"orientation: ", packet->orientation);
230 log(
"linear_acc_x: ", packet->linear_acceleration_x);
231 log(
"linear_acc_y: ", packet->linear_acceleration_y);
232 log(
"linear_acc_z: ", packet->linear_acceleration_z);
233 log(
"gyro_x: ", packet->gyro_x);
234 log(
"buttonState: ", packet->buttonState);
235 log(
"batteryLevel: ", packet->batteryLevel);
236 for(
size_t i = 0; i < 3; i++) {
237 log(
"batteryCellLevel[]: ", i, packet->batteryCellLevel[i]);
239 log(
"applied servo front: ", packet->applied_servo_front);
240 log(
"applied servo rear: ", packet->applied_servo_rear);
241 log(
"Applied motor current: ", packet->applied_motor_current);
242 log(
"Applied motor erpm: ", packet->applied_motor_erpm);
243 log(
"Applied motor brake: ", packet->applied_motor_brake);
258 m_measurementPubSPtr->publish(std::move(msgUPtr));
261 std::unique_ptr<std_msgs::msg::Float32> velocityUPtr = std::make_unique<std_msgs::msg::Float32>();
263 m_velocityPubSPtr->publish(std::move(velocityUPtr));
293 m_lastButtonState = packet->buttonState;
301 std::unique_lock<std::mutex> lock(m_lightsMtx);
302 m_lastLightsSPtr = p_msgSPtr;