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

Commit 89f60e4

Browse files
committed
Read parameters only once each cycle
1 parent 519b436 commit 89f60e4

File tree

4 files changed

+202
-94
lines changed

4 files changed

+202
-94
lines changed

march_hardware/include/march_hardware/motor_controller/odrive/odrive.h

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,8 @@
2222
#define ODRIVE_OK 0;
2323
#define ODRIVE_ERROR 1;
2424

25+
static constexpr double PI_2 = 2 * M_PI;
26+
2527
namespace march
2628
{
2729
typedef struct odrive_json_object
@@ -75,6 +77,23 @@ class Odrive : public MotorController
7577

7678
std::string axis_number;
7779

80+
protected:
81+
float readMotorControllerVoltage();
82+
float readMotorCurrent();
83+
float readMotorVoltage();
84+
85+
uint16_t readAxisError();
86+
uint16_t readAxisMotorError();
87+
uint8_t readAxisEncoderError();
88+
uint8_t readAxisControllerError();
89+
90+
int readAngleCountsAbsolute();
91+
double readVelocityRadAbsolute();
92+
93+
int readAngleCountsIncremental();
94+
double readVelocityRadIncremental();
95+
std::string create_command(std::string command_name);
96+
7897
private:
7998
int json_string_read(const Json::Value& json_parameter_object);
8099

march_hardware/include/march_hardware/motor_controller/odrive/odrive_motor.h

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@
99
#include "odrive_enums.h"
1010
#include <march_hardware/motor_controller/actuation_mode.h>
1111

12-
static constexpr double PI_2 = 2 * M_PI;
1312
static constexpr double MOTOR_KV = 100;
1413
static constexpr double CURRENT_TO_TORQUE_CONVERSION = 8.27;
1514

@@ -60,7 +59,6 @@ class OdriveMotor : public Odrive
6059
}
6160

6261
private:
63-
std::string create_command(std::string command_name);
6462
int setState(uint8_t state);
6563
uint8_t getState();
6664

@@ -69,6 +67,22 @@ class OdriveMotor : public Odrive
6967

7068
ActuationMode mode_;
7169
std::string json_config_file_path_;
70+
71+
void readValues();
72+
73+
uint16_t axis_error;
74+
uint16_t axis_motor_error;
75+
uint8_t axis_encoder_error;
76+
uint8_t axis_controller_error;
77+
78+
float motor_controller_voltage;
79+
float motor_current;
80+
float motor_voltage;
81+
82+
double angle_counts_absolute;
83+
double velocity_rad_absolute;
84+
double angle_counts_incremental;
85+
double velocity_rad_incremental;
7286
};
7387

7488
} // namespace march

march_hardware/src/odrive/odrive.cpp

Lines changed: 135 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -391,6 +391,141 @@ int Odrive::setConfigurations(const std::string& configuration_json_path)
391391
return ODRIVE_OK
392392
}
393393

394+
uint16_t Odrive::readAxisError()
395+
{
396+
uint16_t axis_error;
397+
std::string command_name_ = this->create_command(O_PM_AXIS_ERROR);
398+
if (this->read(command_name_, axis_error) == 1)
399+
{
400+
ROS_ERROR("Could not retrieve axis error");
401+
return ODRIVE_ERROR;
402+
}
403+
404+
return axis_error;
405+
}
406+
407+
uint16_t Odrive::readAxisMotorError()
408+
{
409+
uint16_t axis_motor_error;
410+
std::string command_name_ = this->create_command(O_PM_AXIS_MOTOR_ERROR);
411+
if (this->read(command_name_, axis_motor_error) == 1)
412+
{
413+
ROS_ERROR("Could not retrieve axis motor error");
414+
return ODRIVE_ERROR;
415+
}
416+
417+
return axis_motor_error;
418+
}
419+
420+
uint8_t Odrive::readAxisEncoderError()
421+
{
422+
uint8_t axis_encoder_error;
423+
std::string command_name_ = this->create_command(O_PM_AXIS_ENCODER_ERROR);
424+
if (this->read(command_name_, axis_encoder_error) == 1)
425+
{
426+
ROS_ERROR("Could not retrieve axis encoder error");
427+
return ODRIVE_ERROR;
428+
}
429+
430+
return axis_encoder_error;
431+
}
432+
433+
uint8_t Odrive::readAxisControllerError()
434+
{
435+
uint8_t axis_controller_error;
436+
std::string command_name_ = this->create_command(O_PM_AXIS_CONTROLLER_ERROR);
437+
if (this->read(command_name_, axis_controller_error) == 1)
438+
{
439+
ROS_ERROR("Could not retrieve axis controller error");
440+
return ODRIVE_ERROR;
441+
}
442+
443+
return axis_controller_error;
444+
}
445+
446+
float Odrive::readMotorControllerVoltage()
447+
{
448+
float motor_controller_voltage;
449+
std::string command_name_ = this->create_command(O_PM_ODRIVE_INPUT_VOLTAGE);
450+
if (this->read(command_name_, motor_controller_voltage) == 1)
451+
{
452+
ROS_ERROR("Could not retrieve motor controller voltage");
453+
return ODRIVE_ERROR;
454+
}
455+
return motor_controller_voltage;
456+
}
457+
458+
float Odrive::readMotorCurrent()
459+
{
460+
float motor_current;
461+
std::string command_name_ = this->create_command(O_PM_ACTUAL_MOTOR_CURRENT);
462+
if (this->read(command_name_, motor_current) == 1)
463+
{
464+
ROS_ERROR("Could not retrieve motor current");
465+
return ODRIVE_ERROR;
466+
}
467+
468+
return motor_current;
469+
}
470+
471+
float Odrive::readMotorVoltage()
472+
{
473+
float motor_voltage;
474+
std::string command_name_ = this->create_command(O_PM_ACTUAL_MOTOR_VOLTAGE_D);
475+
if (this->read(command_name_, motor_voltage) == 1)
476+
{
477+
ROS_ERROR("Could not retrieve motor voltage");
478+
return ODRIVE_ERROR;
479+
}
480+
481+
return motor_voltage;
482+
}
483+
484+
int Odrive::readAngleCountsAbsolute()
485+
{
486+
return 0;
487+
}
488+
489+
double Odrive::readVelocityRadAbsolute()
490+
{
491+
return 0;
492+
}
493+
494+
int Odrive::readAngleCountsIncremental()
495+
{
496+
float iu_position;
497+
std::string command_name_ = this->create_command(O_PM_ENCODER_POSITION_UI);
498+
if (this->read(command_name_, iu_position) == 1)
499+
{
500+
ROS_ERROR("Could not retrieve incremental position of the encoder");
501+
return ODRIVE_ERROR
502+
}
503+
return iu_position;
504+
}
505+
506+
double Odrive::readVelocityRadIncremental()
507+
{
508+
float iu_velocity;
509+
std::string command_name_ = this->create_command(O_PM_ENCODER_VELOCITY_UI);
510+
if (this->read(command_name_, iu_velocity) == 1)
511+
{
512+
ROS_ERROR("Could not retrieve incremental velocity of the encoder");
513+
return ODRIVE_ERROR
514+
}
515+
516+
double angle_rad = iu_velocity * PI_2 / (std::pow(2, 12) * 101);
517+
return angle_rad;
518+
}
519+
520+
std::string Odrive::create_command(std::string command_name)
521+
{
522+
if (command_name.at(0) == '.')
523+
{
524+
return this->axis_number + command_name;
525+
}
526+
return command_name;
527+
}
528+
394529
template int Odrive::validateType(const odrive_json_object& json_object, int8_t&);
395530
template int Odrive::validateType(const odrive_json_object& json_object, int16_t&);
396531
template int Odrive::validateType(const odrive_json_object& json_object, int32_t&);

0 commit comments

Comments
 (0)