Skip to content
This repository was archived by the owner on Dec 1, 2020. It is now read-only.

Commit 28bbd26

Browse files
Feature: add incremental encoder (#225)
* add absolute and differential encoder * update IMotionCube * update Joint * update march_hardware tests * update march_hardware_builder * update hardware_builder tests * update march_hardware_interface * fix build errors * fix build errors part two * fix incorrect yaml files * format code to clang format * Move encoders to separate directory * Rename encoders * Sort library sources list * Add tests for incremental and base class encoder * Actually add the incremental value to the state lol * Add transmission variable for incremental encoders to calculate radians * Make Encoder pure abstract Co-authored-by: bjornminderman <33688606+bjornminderman@users.noreply.github.com>
1 parent 37e3296 commit 28bbd26

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

55 files changed

+889
-469
lines changed

.gitignore

+1
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,3 @@
11
.idea/*
22
*/cmake-build-debug/**
3+
.vscode/settings.json

march_hardware/CMakeLists.txt

+11-2
Original file line numberDiff line numberDiff line change
@@ -38,10 +38,12 @@ if(CATKIN_ENABLE_TESTING AND ENABLE_COVERAGE_TESTING)
3838
endif()
3939

4040
add_library(${PROJECT_NAME}
41+
include/${PROJECT_NAME}/encoder/AbsoluteEncoder.h
42+
include/${PROJECT_NAME}/encoder/Encoder.h
43+
include/${PROJECT_NAME}/encoder/IncrementalEncoder.h
4144
include/${PROJECT_NAME}/error/error_type.h
4245
include/${PROJECT_NAME}/error/hardware_exception.h
4346
include/${PROJECT_NAME}/error/motion_error.h
44-
src/Encoder.cpp
4547
src/EtherCAT/EthercatIO.cpp
4648
src/EtherCAT/EthercatMaster.cpp
4749
src/EtherCAT/EthercatSDO.cpp
@@ -54,6 +56,9 @@ add_library(${PROJECT_NAME}
5456
src/PDOmap.cpp
5557
src/PowerDistributionBoard.cpp
5658
src/TemperatureGES.cpp
59+
src/encoder/AbsoluteEncoder.cpp
60+
src/encoder/Encoder.cpp
61+
src/encoder/IncrementalEncoder.cpp
5762
src/error/error_type.cpp
5863
src/error/motion_error.cpp
5964
)
@@ -89,7 +94,6 @@ install(DIRECTORY launch
8994
if(CATKIN_ENABLE_TESTING)
9095
catkin_add_gmock(${PROJECT_NAME}_test
9196
test/TestBootShutdownOffsets.cpp
92-
test/TestEncoder.cpp
9397
test/TestHighVoltage.cpp
9498
test/TestIMotionCube.cpp
9599
test/TestJoint.cpp
@@ -100,10 +104,15 @@ if(CATKIN_ENABLE_TESTING)
100104
test/TestPowerDistributionBoard.cpp
101105
test/TestRunner.cpp
102106
test/TestTemperatureGES.cpp
107+
test/encoder/TestAbsoluteEncoder.cpp
108+
test/encoder/TestIncrementalEncoder.cpp
109+
test/encoder/TestEncoder.cpp
103110
test/error/test_hardware_exception.cpp
104111
test/error/test_motion_error.cpp
112+
test/mocks/MockAbsoluteEncoder.cpp
105113
test/mocks/MockEncoder.cpp
106114
test/mocks/MockIMotionCube.cpp
115+
test/mocks/MockIncrementalEncoder.cpp
107116
test/mocks/MockJoint.cpp
108117
test/mocks/MockTemperatureGES.cpp
109118
test/mocks/MockTemperatureSensor.cpp

march_hardware/include/march_hardware/Encoder.h

-99
This file was deleted.

march_hardware/include/march_hardware/IMotionCube.h

+20-13
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,14 @@
22

33
#ifndef MARCH_HARDWARE_IMOTIONCUBE_H
44
#define MARCH_HARDWARE_IMOTIONCUBE_H
5-
#include <march_hardware/ActuationMode.h>
6-
#include <march_hardware/Encoder.h>
7-
#include <march_hardware/EtherCAT/EthercatIO.h>
8-
#include <march_hardware/IMotionCubeState.h>
9-
#include <march_hardware/IMotionCubeTargetState.h>
10-
#include <march_hardware/PDOmap.h>
11-
#include <march_hardware/Slave.h>
5+
#include "march_hardware/ActuationMode.h"
6+
#include "march_hardware/encoder/AbsoluteEncoder.h"
7+
#include "march_hardware/encoder/IncrementalEncoder.h"
8+
#include "march_hardware/EtherCAT/EthercatIO.h"
9+
#include "march_hardware/IMotionCubeState.h"
10+
#include "march_hardware/IMotionCubeTargetState.h"
11+
#include "march_hardware/PDOmap.h"
12+
#include "march_hardware/Slave.h"
1213

1314
#include <unordered_map>
1415
#include <string>
@@ -18,15 +19,18 @@ namespace march
1819
class IMotionCube : public Slave
1920
{
2021
public:
21-
IMotionCube(int slave_index, Encoder encoder, ActuationMode actuation_mode);
22+
IMotionCube(int slave_index, AbsoluteEncoder absolute_encoder, IncrementalEncoder incremental_encoder,
23+
ActuationMode actuation_mode);
2224

2325
~IMotionCube() = default;
2426

2527
void writeInitialSDOs(int cycle_time) override;
2628

27-
double getAngleRad();
29+
double getAngleRadAbsolute();
30+
double getAngleRadIncremental();
2831
int16_t getTorque();
29-
int32_t getAngleIU();
32+
int32_t getAngleIUAbsolute();
33+
int32_t getAngleIUIncremental();
3034

3135
uint16_t getStatusWord();
3236
uint16_t getMotionError();
@@ -49,13 +53,15 @@ class IMotionCube : public Slave
4953
/** @brief Override comparison operator */
5054
friend bool operator==(const IMotionCube& lhs, const IMotionCube& rhs)
5155
{
52-
return lhs.slaveIndex == rhs.slaveIndex && lhs.encoder_ == rhs.encoder_;
56+
return lhs.slaveIndex == rhs.slaveIndex && lhs.absolute_encoder_ == rhs.absolute_encoder_ &&
57+
lhs.incremental_encoder_ == rhs.incremental_encoder_;
5358
}
5459
/** @brief Override stream operator for clean printing */
5560
friend std::ostream& operator<<(std::ostream& os, const IMotionCube& imc)
5661
{
5762
return os << "slaveIndex: " << imc.slaveIndex << ", "
58-
<< "encoder: " << imc.encoder_;
63+
<< "incrementalEncoder: " << imc.incremental_encoder_ << ", "
64+
<< "absoluteEncoder: " << imc.absolute_encoder_;
5965
}
6066

6167
constexpr static double MAX_TARGET_DIFFERENCE = 0.393;
@@ -75,7 +81,8 @@ class IMotionCube : public Slave
7581
void mapMosiPDOs();
7682
void writeInitialSettings(uint8_t cycle_time);
7783

78-
Encoder encoder_;
84+
AbsoluteEncoder absolute_encoder_;
85+
IncrementalEncoder incremental_encoder_;
7986
ActuationMode actuation_mode_;
8087

8188
std::unordered_map<IMCObjectName, uint8_t> miso_byte_offsets_;

march_hardware/include/march_hardware/IMotionCubeState.h

+1
Original file line numberDiff line numberDiff line change
@@ -132,6 +132,7 @@ struct IMotionCubeState
132132
std::string motionErrorDescription;
133133
float motorCurrent;
134134
float motorVoltage;
135+
int incrementalEncoderValue;
135136
};
136137

137138
} // namespace march

march_hardware/include/march_hardware/Joint.h

+4-3
Original file line numberDiff line numberDiff line change
@@ -34,9 +34,11 @@ class Joint
3434
void actuateRad(double targetPositionRad);
3535
void actuateTorque(int16_t targetTorque);
3636

37-
double getAngleRad();
37+
double getAngleRadAbsolute();
38+
double getAngleRadIncremental();
3839
int16_t getTorque();
39-
int32_t getAngleIU();
40+
int32_t getAngleIUAbsolute();
41+
int32_t getAngleIUIncremental();
4042
float getTemperature();
4143
IMotionCubeState getIMotionCubeState();
4244

@@ -78,7 +80,6 @@ class Joint
7880

7981
void setName(const std::string& name);
8082
void setAllowActuation(bool allowActuation);
81-
void setIMotionCube(const IMotionCube& iMotionCube);
8283
void setTemperatureGes(const TemperatureGES& temperatureGes);
8384
void setNetNumber(int netNumber);
8485
};

march_hardware/include/march_hardware/Slave.h

-2
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
11
// Copyright 2019 Project March.
22
#ifndef MARCH_HARDWARE_SLAVE_H
33
#define MARCH_HARDWARE_SLAVE_H
4-
5-
#include <stdexcept>
64
#include <ros/ros.h>
75

86
namespace march
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
// Copyright 2019 Project March.
2+
3+
#ifndef MARCH_HARDWARE_ABSOLUTE_ENCODER_H
4+
#define MARCH_HARDWARE_ABSOLUTE_ENCODER_H
5+
#include "march_hardware/encoder/Encoder.h"
6+
7+
#include <ostream>
8+
9+
namespace march
10+
{
11+
class AbsoluteEncoder : public Encoder
12+
{
13+
public:
14+
AbsoluteEncoder(size_t number_of_bits, int32_t lower_limit_iu, int32_t upper_limit_iu, double lower_limit_rad,
15+
double upper_limit_rad, double lower_soft_limit_rad, double upper_soft_limit_rad);
16+
17+
double toRad(int32_t iu) const override;
18+
19+
/*
20+
* Converts radians to encoder Internal Units (IU).
21+
*/
22+
int32_t fromRad(double rad) const;
23+
24+
bool isWithinHardLimitsIU(int32_t iu) const;
25+
bool isWithinSoftLimitsIU(int32_t iu) const;
26+
bool isValidTargetIU(int32_t current_iu, int32_t target_iu) const;
27+
28+
int32_t getUpperSoftLimitIU() const;
29+
int32_t getLowerSoftLimitIU() const;
30+
int32_t getUpperHardLimitIU() const;
31+
int32_t getLowerHardLimitIU() const;
32+
33+
/** @brief Override comparison operator */
34+
friend bool operator==(const AbsoluteEncoder& lhs, const AbsoluteEncoder& rhs)
35+
{
36+
return lhs.getSlaveIndex() == rhs.getSlaveIndex() && lhs.getTotalPositions() == rhs.getTotalPositions() &&
37+
lhs.upper_soft_limit_iu_ == rhs.upper_soft_limit_iu_ &&
38+
lhs.lower_soft_limit_iu_ == rhs.lower_soft_limit_iu_ && lhs.upper_limit_iu_ == rhs.upper_limit_iu_ &&
39+
lhs.lower_limit_iu_ == rhs.lower_limit_iu_ && lhs.zero_position_iu_ == rhs.zero_position_iu_;
40+
}
41+
/** @brief Override stream operator for clean printing */
42+
friend std::ostream& operator<<(std::ostream& os, const AbsoluteEncoder& encoder)
43+
{
44+
return os << "slaveIndex: " << encoder.getSlaveIndex() << ", "
45+
<< "totalPositions: " << encoder.getTotalPositions() << ", "
46+
<< "upperHardLimit: " << encoder.upper_limit_iu_ << ", "
47+
<< "lowerHardLimit: " << encoder.lower_limit_iu_ << ", "
48+
<< "upperSoftLimit: " << encoder.upper_soft_limit_iu_ << ", "
49+
<< "lowerSoftLimit: " << encoder.lower_soft_limit_iu_ << ", "
50+
<< "zeroPositionIU: " << encoder.zero_position_iu_;
51+
}
52+
53+
static constexpr double MAX_RANGE_DIFFERENCE = 0.05;
54+
55+
private:
56+
int32_t zero_position_iu_ = 0;
57+
int32_t lower_limit_iu_ = 0;
58+
int32_t upper_limit_iu_ = 0;
59+
int32_t lower_soft_limit_iu_ = 0;
60+
int32_t upper_soft_limit_iu_ = 0;
61+
};
62+
} // namespace march
63+
64+
#endif // MARCH_HARDWARE_ABSOLUTE_ENCODER_H
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
// Copyright 2019 Project March.
2+
3+
#ifndef MARCH_HARDWARE_ENCODER_H
4+
#define MARCH_HARDWARE_ENCODER_H
5+
#include <cmath>
6+
#include <cstddef>
7+
#include <cstdint>
8+
9+
namespace march
10+
{
11+
class Encoder
12+
{
13+
public:
14+
explicit Encoder(size_t number_of_bits);
15+
16+
/*
17+
* Reads out the encoder from the slave and returns the value in Internal Units (IU).
18+
* @param byte_offset the byte offset in the slave register for the IU position
19+
* @returns The current position of the encoder in Internal Units (IU)
20+
*/
21+
int32_t getAngleIU(uint8_t byte_offset) const;
22+
23+
/*
24+
* Reads out the encoder from the slave and transforms the result to radians.
25+
* @param byte_offset the byte offset in the slave register for the IU position
26+
* @returns The current position of the encoder in radians
27+
*/
28+
double getAngleRad(uint8_t byte_offset) const;
29+
30+
/*
31+
* Converts encoder Internal Units (IU) to radians.
32+
* This is a pure virtual function and must be implemented by subclasses,
33+
* since each type of encoder has a different way of calculating radians.
34+
*/
35+
virtual double toRad(int32_t iu) const = 0;
36+
37+
size_t getTotalPositions() const;
38+
39+
int getSlaveIndex() const;
40+
void setSlaveIndex(int slave_index);
41+
42+
static const size_t MIN_RESOLUTION = 1;
43+
static const size_t MAX_RESOLUTION = 32;
44+
45+
static constexpr double PI_2 = 2 * M_PI;
46+
47+
private:
48+
/*
49+
* Returns the total number of positions possible on an encoder
50+
* with the given amount of bits.
51+
* @param number_of_bits The resolution of the encoder
52+
* @returns The total amount of different positions
53+
* @throws HardwareException When the given resolution is outside the allowed range
54+
* Which is determined by Encoder::MIN_RESOLUTION
55+
* and Encoder::MAX_RESOLUTION.
56+
*/
57+
static size_t calculateTotalPositions(size_t number_of_bits);
58+
59+
int slave_index_ = -1;
60+
size_t total_positions_ = 0;
61+
};
62+
} // namespace march
63+
64+
#endif // MARCH_HARDWARE_ENCODER_H

0 commit comments

Comments
 (0)