-
Notifications
You must be signed in to change notification settings - Fork 0
Issue #22. Implement reading and sending to rpi imu data #24
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
taketook34
merged 11 commits into
main
from
22-implement-reading-and-sending-to-rpi-imu-data
May 7, 2025
Merged
Changes from 9 commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
7a28f20
Issue #22: Creating IMUController, impleenting in KPIRoverECU
taketook34 286d3a9
Issue #22: Implementing UDP CLient for sending read data from IMU.
taketook34 d09bf08
Issue #22: Add new scenario: Stop receiving after no-command second
taketook34 8914015
Issue #22: Formatting IMUController due to code style. Adding IMUCont…
taketook34 a1edff9
Issue #22: Get source IP from TCP packages
taketook34 7561421
Issue #22: Fixed code style
taketook34 13cb3ba
Issue #22: Fixed cli arguments
taketook34 fa9722a
Issue #22: All motors GetEncoderCounter() return values with same sign
taketook34 b93bf65
Issue #22. Encoder reading test was fixed
taketook34 22acfd3
Issue #22. Fix code style and comments in tests
taketook34 81ecd83
Issue #22. Fixing code style warnings
taketook34 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,25 @@ | ||
| #ifndef ICLIENT_H | ||
| #define ICLIENT_H | ||
|
|
||
| #include <cstdint> | ||
| #include <string> | ||
| #include <vector> | ||
|
|
||
| class IClient { | ||
| protected: | ||
| IClient() = default; | ||
|
|
||
| public: | ||
| virtual bool Send(const std::vector<std::uint8_t>& data) = 0; | ||
| virtual bool Receive(std::vector<std::uint8_t>& data) = 0; | ||
| virtual int Init(std::string ip_address, int port) = 0; | ||
| virtual void Destroy() = 0; | ||
| virtual ~IClient() = default; | ||
|
|
||
| IClient(const IClient&) = delete; | ||
| IClient& operator=(const IClient&) = delete; | ||
| IClient(IClient&&) = delete; | ||
| IClient& operator=(IClient&&) = delete; | ||
| }; | ||
|
|
||
| #endif | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,41 @@ | ||
| #ifndef IMUCONTROLLER_H | ||
| #define IMUCONTROLLER_H | ||
|
|
||
| #include <rc/mpu.h> | ||
|
|
||
| #include <atomic> | ||
| #include <cstddef> | ||
| #include <cstdint> | ||
| #include <vector> | ||
|
|
||
| constexpr size_t kActualDataSize = 10; | ||
|
|
||
| class IMUController { | ||
| public: | ||
| IMUController(); | ||
| int Init(); | ||
| void SetEnable(); | ||
| void SetDisable(); | ||
| void Stop(); | ||
| std::vector<float> GetData(); | ||
| uint8_t GetId(); | ||
|
|
||
| private: | ||
| const int kIdGetCommand = 0x06; | ||
| const int kI2cBus = 2; | ||
| const int kGpioIntPinChip = 3; | ||
| const int kGpioIntPinPin = 21; | ||
| const int kDmpSampleRate = 100; | ||
| const int kEnableMagnetometer = 1; | ||
|
|
||
| std::atomic<bool> isStarted_; | ||
| rc_mpu_config_t configuration_; | ||
| std::vector<float> actualData_; | ||
| rc_mpu_data_t data_; | ||
|
|
||
| std::vector<float> GetAccel(); | ||
| std::vector<float> GetGyro(); | ||
| std::vector<float> GetQaternion(); | ||
| }; | ||
|
|
||
| #endif |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,6 +1,8 @@ | ||
| #ifndef TCPTRANSPORT_H | ||
| #define TCPTRANSPORT_H | ||
|
|
||
| #include <arpa/inet.h> | ||
|
|
||
| #include <atomic> | ||
| #include <cstdint> | ||
| #include <cstring> | ||
|
|
@@ -28,9 +30,14 @@ class TCPTransport : public ITransport { | |
| int Init() override; | ||
| void Destroy() override; | ||
|
|
||
| std::string GetSourceIp(); | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Please rename to GetClientIp() and GetClientPort() |
||
| int GetSourcePort(); | ||
|
|
||
| private: | ||
| int sockfd_, client_sockfd_; | ||
| char* server_address_; | ||
| char source_address_[INET_ADDRSTRLEN]; | ||
| int source_port_; | ||
| int server_portnum_; | ||
| std::atomic<bool> running_; | ||
| std::thread acceptThread_; | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,35 @@ | ||
| #ifndef UDPCLIENT_H | ||
| #define UDPCLIENT_H | ||
|
|
||
| #include <arpa/inet.h> | ||
| #include <netinet/in.h> | ||
|
|
||
| #include <cstdint> | ||
| #include <string> | ||
| #include <vector> | ||
|
|
||
| #include "IClient.h" | ||
|
|
||
| class UDPClient : public IClient { | ||
| public: | ||
| UDPClient(); | ||
|
|
||
| UDPClient(const UDPClient&) = delete; | ||
| UDPClient& operator=(const UDPClient&) = delete; | ||
| UDPClient(UDPClient&&) = delete; | ||
| UDPClient& operator=(UDPClient&&) = delete; | ||
|
|
||
| int Init(std::string ip_address, int port) override; | ||
| bool Send(const std::vector<std::uint8_t>& data) override; | ||
| bool Receive(std::vector<std::uint8_t>& data) override; | ||
| void Destroy() override; | ||
| ~UDPClient() override; | ||
|
|
||
| private: | ||
| int sockfd_; | ||
| sockaddr_in serverStruct_; | ||
| char server_address_[INET_ADDRSTRLEN]; | ||
| int server_portnum_; | ||
| }; | ||
|
|
||
| #endif |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,89 @@ | ||
| #include "IMUController.h" | ||
|
|
||
| #include <rc/mpu.h> | ||
|
|
||
| #include <algorithm> | ||
| #include <cstdint> | ||
| #include <iostream> | ||
| #include <vector> | ||
|
|
||
| IMUController::IMUController() : data_{}, isStarted_(false), configuration_(rc_mpu_default_config()), actualData_({}) { | ||
| configuration_.i2c_bus = kI2cBus; | ||
| configuration_.gpio_interrupt_pin_chip = kGpioIntPinChip; | ||
| configuration_.gpio_interrupt_pin = kGpioIntPinPin; | ||
| configuration_.dmp_sample_rate = kDmpSampleRate; | ||
| configuration_.enable_magnetometer = kEnableMagnetometer; | ||
| } | ||
|
|
||
| int IMUController::Init() { | ||
| if (rc_mpu_initialize_dmp(&data_, configuration_) != 0) { | ||
| std::cout << "Initializing MPU failed" << '\n'; | ||
| return -1; | ||
| } | ||
|
|
||
| return 0; | ||
| } | ||
|
|
||
| void IMUController::SetEnable() { isStarted_ = true; } | ||
|
|
||
| void IMUController::SetDisable() { isStarted_ = false; } | ||
|
|
||
| std::vector<float> IMUController::GetData() { | ||
| if (!isStarted_) { | ||
| return {}; | ||
| } | ||
|
|
||
| rc_mpu_read_accel(&data_); | ||
| rc_mpu_read_gyro(&data_); | ||
|
|
||
| actualData_.resize(kActualDataSize); | ||
| const std::vector<float> kAccelData = GetAccel(); | ||
| const int kAccelDataSize = static_cast<int>(kAccelData.size()); | ||
| std::copy(kAccelData.begin(), kAccelData.begin() + kAccelDataSize, actualData_.begin()); | ||
|
|
||
| const std::vector<float> kGyroData = GetGyro(); | ||
| const int kGyroDataSize = static_cast<int>(kGyroData.size()); | ||
| std::copy(kGyroData.begin(), kGyroData.begin() + kGyroDataSize, actualData_.begin() + kAccelDataSize); | ||
|
|
||
| const std::vector<float> kQaternionData = GetQaternion(); | ||
| const int kQaternionDataSize = static_cast<int>(kQaternionData.size()); | ||
| std::copy(kQaternionData.begin(), kQaternionData.begin() + kQaternionDataSize, | ||
| actualData_.begin() + kAccelDataSize + kGyroDataSize); | ||
|
|
||
| return actualData_; | ||
| } | ||
|
|
||
| std::vector<float> IMUController::GetAccel() { | ||
| std::vector<float> ret_val; | ||
|
|
||
| ret_val.push_back(static_cast<float>(data_.accel[0])); | ||
| ret_val.push_back(static_cast<float>(data_.accel[1])); | ||
| ret_val.push_back(static_cast<float>(data_.accel[2])); | ||
|
|
||
| return ret_val; | ||
| } | ||
|
|
||
| std::vector<float> IMUController::GetGyro() { | ||
| std::vector<float> ret_val; | ||
|
|
||
| ret_val.push_back(static_cast<float>(data_.gyro[0])); | ||
| ret_val.push_back(static_cast<float>(data_.gyro[1])); | ||
| ret_val.push_back(static_cast<float>(data_.gyro[2])); | ||
|
|
||
| return ret_val; | ||
| } | ||
|
|
||
| std::vector<float> IMUController::GetQaternion() { | ||
| std::vector<float> ret_val; | ||
|
|
||
| ret_val.push_back(static_cast<float>(data_.dmp_quat[QUAT_W])); | ||
| ret_val.push_back(static_cast<float>(data_.dmp_quat[QUAT_X])); | ||
| ret_val.push_back(static_cast<float>(data_.dmp_quat[QUAT_Y])); | ||
| ret_val.push_back(static_cast<float>(data_.dmp_quat[QUAT_Z])); | ||
|
|
||
| return ret_val; | ||
| } | ||
|
|
||
| void IMUController::Stop() { rc_mpu_power_off(); } | ||
|
|
||
| uint8_t IMUController::GetId() { return kIdGetCommand; } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please add new line at the end