Skip to content

Commit d267dc0

Browse files
committed
Issue #16: Get rid of hysteresis in MotorController::SetMotorRPM
1 parent f613668 commit d267dc0

File tree

5 files changed

+26
-35
lines changed

5 files changed

+26
-35
lines changed

src/kpi_rover_ecu/include/protocolHandler.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ class ProtocolHanlder {
2222
std::vector<uint8_t> MotorsStopMessage();
2323

2424
private:
25-
MotorController* main_controller_;
25+
MotorController* motors_controller_;
2626
std::vector<uint8_t> HandleSetMotorSpeed(const std::vector<uint8_t>& message);
2727
std::vector<uint8_t> HandleGetApiVersion(const std::vector<uint8_t>& message);
2828
std::vector<uint8_t> HandleSetAllMotorsSpeed(const std::vector<uint8_t>& message);

src/kpi_rover_ecu/src/main.cpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@
1010
#include <iostream>
1111
#include <string>
1212
#include <vector>
13+
#include <thread>
14+
#include <chrono>
1315

1416
#include "KPIRoverECU.h"
1517
#include "TCPTransport.h"
@@ -86,6 +88,7 @@ int main(int argc, char* argv[]) {
8688
}
8789

8890
while (running_program) {
91+
std::this_thread::sleep_for(std::chrono::milliseconds(100));
8992
}
9093

9194
kpi_rover_ecu.Stop();

src/kpi_rover_ecu/src/motor.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,9 @@ int Motor::MotorGo(int newRPM) {
1414
newRPM = kMaxRpm;
1515
}
1616

17-
if (newRPM < kMinRpm) {
17+
if (newRPM < -kMaxRpm) {
1818
std::cout << "[Warning] RPM out of range\n";
19-
newRPM = kMinRpm;
19+
newRPM = -kMaxRpm;
2020
}
2121

2222
currentDutyCycle_ = GetDC(newRPM);

src/kpi_rover_ecu/src/motorsController.cpp

Lines changed: 10 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -47,29 +47,17 @@ int MotorController::Init(const std::vector<MotorConfig>& _motors, uint8_t _moto
4747
}
4848

4949
int MotorController::SetMotorRPM(int channel, int newRPM) {
50-
if (std::abs(newRPM) > Motor::kMinRpm) {
51-
if (newRPM >= Motor::kMaxRpm) {
52-
std::cout << "[INFO][RC] Set RPM to max value" << '\n';
53-
if (motors_[channel].MotorGo(Motor::kMaxRpm) != 0) {
54-
std::cout << "[ERROR][RC] Error while set new RPM" << '\n';
55-
return -1;
56-
}
57-
} else {
58-
if (motors_[channel].MotorGo(newRPM) != 0) {
59-
std::cout << "[ERROR][RC] Error while set new RPM" << '\n';
60-
return -1;
61-
}
62-
}
63-
} else {
64-
if (std::abs(newRPM) != 0) {
65-
std::cout << "[INFO][RC] Set RPM to stop because RPM less than MIN_RPM" << '\n';
66-
}
67-
68-
if (motors_[channel].MotorStop() != 0) {
69-
std::cout << "[ERROR][RC] Error while set new RPM" << '\n';
70-
return -1;
71-
}
50+
if (channel >= motor_number_) {
51+
std::cout << "[ERROR][RC] Channel out of range" << '\n';
52+
return -1;
7253
}
54+
55+
int res = motors_[channel].MotorGo(newRPM);
56+
if (res != 0) {
57+
std::cout << "[ERROR][RC] Error while set new RPM" << '\n';
58+
return -1;
59+
}
60+
7361
return 0;
7462
}
7563

src/kpi_rover_ecu/src/protocolHandler.cpp

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ constexpr uint8_t ProtocolHanlder::kIdSetAllMotorsSpeed;
1818
constexpr uint8_t ProtocolHanlder::kIdGetEncoder;
1919
constexpr uint8_t ProtocolHanlder::kIdGetAllEncoders;
2020

21-
ProtocolHanlder::ProtocolHanlder(MotorController* motorDriver) : main_controller_(motorDriver) {}
21+
ProtocolHanlder::ProtocolHanlder(MotorController* motorDriver) : motors_controller_(motorDriver) {}
2222

2323
vector<uint8_t> ProtocolHanlder::HandleSetMotorSpeed(const vector<uint8_t>& message) {
2424
const uint8_t kMotorId = message[1];
@@ -32,7 +32,7 @@ vector<uint8_t> ProtocolHanlder::HandleSetMotorSpeed(const vector<uint8_t>& mess
3232
<< '\n';
3333
std::cout << "[INFO] Motor set run" << '\n';
3434

35-
if (main_controller_->SetMotorRPM(static_cast<int>(kMotorId), static_cast<int>(k_motor_rpm)) != 0) {
35+
if (motors_controller_->SetMotorRPM(static_cast<int>(kMotorId), static_cast<int>(k_motor_rpm)) != 0) {
3636
std::cout << "[ERROR] Error setMotorRPM, retry connection" << '\n';
3737
return {};
3838
}
@@ -58,20 +58,20 @@ vector<uint8_t> ProtocolHanlder::HandleGetApiVersion(const vector<uint8_t>& mess
5858
}
5959

6060
vector<uint8_t> ProtocolHanlder::HandleSetAllMotorsSpeed(const vector<uint8_t>& message) {
61-
std::vector<int32_t> motors_rpm_arr(main_controller_->GetMotorsNumber(), 0);
61+
std::vector<int32_t> motors_rpm_arr(motors_controller_->GetMotorsNumber(), 0);
6262
vector<uint8_t> ret_val;
6363

64-
for (int i = 0; i < main_controller_->GetMotorsNumber(); i++) {
64+
for (int i = 0; i < motors_controller_->GetMotorsNumber(); i++) {
6565
memcpy(&motors_rpm_arr[i], &message[1 + i * sizeof(int32_t)], sizeof(int32_t));
6666
motors_rpm_arr[i] = static_cast<int32_t>(ntohl(motors_rpm_arr[i]));
6767
}
6868

69-
for (int i = 0; i < main_controller_->GetMotorsNumber(); i++) {
69+
for (int i = 0; i < motors_controller_->GetMotorsNumber(); i++) {
7070
if (motors_rpm_arr[i] != 0) {
7171
std::cout << "[COMMAND] motor " << i << " new rpm " << static_cast<int>(motors_rpm_arr[i]) << '\n';
7272
}
7373

74-
if (main_controller_->SetMotorRPM(i, static_cast<int>(motors_rpm_arr[i])) != 0) {
74+
if (motors_controller_->SetMotorRPM(i, static_cast<int>(motors_rpm_arr[i])) != 0) {
7575
std::cout << "[ERROR] Error setMotorRPM, retry connection" << '\n';
7676
return {};
7777
}
@@ -86,7 +86,7 @@ vector<uint8_t> ProtocolHanlder::HandleGetEncoder(const vector<uint8_t>& message
8686
vector<uint8_t> ret_val;
8787
std::cout << "[COMMAND] get motor " << static_cast<int>(kMotorId) << " encoder " << '\n';
8888

89-
const int32_t kMotorRpm = main_controller_->GetEncoderCounter(kMotorId);
89+
const int32_t kMotorRpm = motors_controller_->GetEncoderCounter(kMotorId);
9090
ret_val.push_back(ProtocolHanlder::kIdGetEncoder);
9191

9292
auto k_motor_rpm_order = static_cast<int32_t>(htonl(kMotorRpm));
@@ -103,8 +103,8 @@ vector<uint8_t> ProtocolHanlder::HandleGetAllEncoders(const vector<uint8_t>& mes
103103
ret_val.push_back(ProtocolHanlder::kIdGetAllEncoders);
104104

105105
uint8_t buffer[sizeof(int32_t)];
106-
for (int i = 0; i < main_controller_->GetMotorsNumber(); i++) {
107-
auto encoder_rpm = static_cast<int32_t>(htonl(main_controller_->GetEncoderCounter(i)));
106+
for (int i = 0; i < motors_controller_->GetMotorsNumber(); i++) {
107+
auto encoder_rpm = static_cast<int32_t>(htonl(motors_controller_->GetEncoderCounter(i)));
108108
std::memcpy(buffer, &encoder_rpm, sizeof(int32_t));
109109
ret_val.insert(ret_val.end(), buffer, buffer + sizeof(int32_t));
110110
}
@@ -144,7 +144,7 @@ vector<uint8_t> ProtocolHanlder::MotorsStopMessage() {
144144
int32_t stop_value = 0;
145145
ret_val.push_back(ProtocolHanlder::kIdSetAllMotorsSpeed);
146146
uint8_t buffer[sizeof(int32_t)];
147-
for (int i = 0; i < main_controller_->GetMotorsNumber(); i++) {
147+
for (int i = 0; i < motors_controller_->GetMotorsNumber(); i++) {
148148
std::memcpy(buffer, &stop_value, sizeof(int32_t));
149149
ret_val.insert(ret_val.end(), buffer, buffer + sizeof(int32_t));
150150
}

0 commit comments

Comments
 (0)