Skip to content

Commit

Permalink
Renaming interface, adding basic kinematics
Browse files Browse the repository at this point in the history
  • Loading branch information
NathanDuPont committed Jul 16, 2024
1 parent ddd6f93 commit c1000b3
Show file tree
Hide file tree
Showing 14 changed files with 220 additions and 78 deletions.
15 changes: 14 additions & 1 deletion MODULE.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,9 @@ git_override(
)
bazel_dep(name = "rules_go", version = "0.46.0")
bazel_dep(name = "rules_multirun", version = "0.9.0")
bazel_dep(name = "platforms", version = "0.0.7")
bazel_dep(name = "platforms", version = "0.0.9")
bazel_dep(name = "rules_oci", version = "1.6.0")
bazel_dep(name = "rules_pkg", version = "1.0.1")
bazel_dep(name = "rules_python", version = "0.29.0")

# Extensions
Expand All @@ -55,6 +56,18 @@ oci.pull(
digest = "sha256:47d0618fb878d93e1b8cacb184fd8f727ae95c1b85d5959723e1d3e1848e2aba",
platforms = [
"linux/amd64",
"linux/arm64",
],
)
use_repo(oci, "py3.11")

oci.pull(
name = "gcc",
image = "index.docker.io/library/gcc",
digest = "sha256:sha256:c24e2fd069574a63513d749a90459eed8b0cc43f3fbb580a0296cdee3ae0df0d",
platforms = [
"linux/amd64",
"linux/arm64",
],
)
use_repo(oci, "gcc")
135 changes: 97 additions & 38 deletions MODULE.bazel.lock

Large diffs are not rendered by default.

2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,3 +9,5 @@
## Setup

https://github.com/hedronvision/bazel-compile-commands-extractor

TODO get renovate working: https://github.com/renovatebot/renovate
12 changes: 12 additions & 0 deletions src/kinematics/BUILD.bazel
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
cc_library(
name = "generic_kinematics_lib",
hdrs = [
"ikinematics.h",
],
visibility = ["//visibility:public"],
deps = [
"//src/math:vector_lib",
"//src/robot/actuator:generic_motor_lib",
"@eigen",
],
)
23 changes: 23 additions & 0 deletions src/kinematics/ikinematics.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
#pragma once

#include "src/math/vector_math.h"
#include "src/robot/actuator/imotor.h"
#include <Eigen/Eigen>

class IKinematics {
public:
/**
* @brief Forward kinematics uses the position of actuators and/or
* sensors on the robot, to determine the overall position of the
* robot.
*
* @return true
* @return false
*/
virtual bool CalculateForwardKinematics(int dT, Location *out_location,
Eigen::Rotation2Df *out_rotation) = 0;

virtual bool
CalculateInverseKinematics(int dT, Movement desired_movement,
std::vector<IMotor *> motor_list, ) = 0;
};
2 changes: 2 additions & 0 deletions src/math/pid.h
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
#pragma once

struct PIDGain {
float kP;
float kI;
Expand Down
2 changes: 2 additions & 0 deletions src/math/vector_math.h
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
#pragma once

#include <Eigen/Eigen>

// Struct to hold a position (as a vector from the origin) and a rotation of
Expand Down
21 changes: 20 additions & 1 deletion src/robot/BUILD.bazel
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
load("@rules_python//python:defs.bzl", "py_library")
load("@rules_pkg//pkg:tar.bzl", "pkg_tar")

# Put all robot files in this library
py_library(
Expand All @@ -23,7 +24,7 @@ cc_library(
"robot.cpp",
],
hdrs = [
"motor.h",
"imotor.h",
"robot.h",
],
deps = ["@eigen"],
Expand All @@ -50,3 +51,21 @@ cc_binary(
"@eigen",
],
)

pkg_tar(
name = "tar",
srcs = [":main"],
)

oci_image(
name = "image",
base = "@docker_lib_ubuntu",
entrypoint = ["/example_binary"],
tars = [":tar"],
)

oci_tarball(
name = "image_tarball",
image = ":image",
repo_tags = ["example:latest"],
)
2 changes: 1 addition & 1 deletion src/robot/actuator/BUILD.bazel
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
cc_library(
name = "generic_motor_lib",
hdrs = [
"motor.h",
"imotor.h",
],
deps = [
"//src/math:math_lib",
Expand Down
32 changes: 17 additions & 15 deletions src/robot/actuator/dynamixel_motor.h
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
#include "motor.h"
#pragma once

#include "imotor.h"
#include <Eigen/Eigen>

enum DynamixelPropertyType {
Expand Down Expand Up @@ -70,33 +72,33 @@ class DynamixelMotor : IMotor {
public:
DynamixelMotor(int id, Location location, bool inverted = false);

bool GetPosition(int *out_position);
virtual bool GetPosition(int *out_position);

bool SetPosition(int position);
virtual bool SetPosition(int position);

bool GetSpeed(float *out_speed);
virtual bool GetSpeed(float *out_speed);

bool SetSpeed(float speed);
virtual bool SetSpeed(float speed);

bool GetAcceleration(float *out_acceleration);
virtual bool GetAcceleration(float *out_acceleration);

bool SetAcceleration(float acceleration);
virtual bool SetAcceleration(float acceleration);

bool GetPIDGain(PIDGain *out_pid_gain);
virtual bool GetPIDGain(PIDGain *out_pid_gain);

bool SetPIDGain(PIDGain pid_gain);
virtual bool SetPIDGain(PIDGain pid_gain);

bool GetLED(bool *out_enabled);
virtual bool GetLED(bool *out_enabled);

bool SetLED(bool enabled);
virtual bool SetLED(bool enabled);

bool GetEnabled(bool *out_enabled);
virtual bool GetEnabled(bool *out_enabled);

bool SetEnabled(bool enabled);
virtual bool SetEnabled(bool enabled);

bool GetMaxSpeed(float *out_speed);
virtual bool GetMaxSpeed(float *out_speed);

bool GetMotorLocation(Location *out_location);
virtual bool GetMotorLocation(Location *out_location);

bool RawSpeedToNormalizedSpeed(float speed, float *out_speed);

Expand Down
2 changes: 2 additions & 0 deletions src/robot/actuator/motor.h → src/robot/actuator/imotor.h
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
#pragma once

#include "src/math/pid.h"
#include "src/math/vector_math.h"
#include <Eigen/Eigen>
Expand Down
32 changes: 17 additions & 15 deletions src/robot/actuator/mock_motor.h
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
#include "motor.h"
#pragma once

#include "imotor.h"
#include <Eigen/Eigen>

class MockMotor : IMotor {
Expand All @@ -20,31 +22,31 @@ class MockMotor : IMotor {
public:
MockMotor(int id, Location location, bool inverted = false);

bool GetPosition(int *out_position);
virtual bool GetPosition(int *out_position);

bool SetPosition(int position);
virtual bool SetPosition(int position);

bool GetSpeed(float *out_speed);
virtual bool GetSpeed(float *out_speed);

bool SetSpeed(float speed);
virtual bool SetSpeed(float speed);

bool GetAcceleration(float *out_acceleration);
virtual bool GetAcceleration(float *out_acceleration);

bool SetAcceleration(float acceleration);
virtual bool SetAcceleration(float acceleration);

bool GetPIDGain(PIDGain *out_pid_gain);
virtual bool GetPIDGain(PIDGain *out_pid_gain);

bool SetPIDGain(PIDGain pid_gain);
virtual bool SetPIDGain(PIDGain pid_gain);

bool GetLED(bool *out_enabled);
virtual bool GetLED(bool *out_enabled);

bool SetLED(bool enabled);
virtual bool SetLED(bool enabled);

bool GetEnabled(bool *out_enabled);
virtual bool GetEnabled(bool *out_enabled);

bool SetEnabled(bool enabled);
virtual bool SetEnabled(bool enabled);

bool GetMaxSpeed(float *out_speed);
virtual bool GetMaxSpeed(float *out_speed);

bool GetMotorLocation(Location *out_location);
virtual bool GetMotorLocation(Location *out_location);
};
9 changes: 4 additions & 5 deletions src/robot/robot.cpp
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
#include "robot.h"

Robot::Robot(std::vector<DynamixelMotor> motor_list)
: motor_list_(motor_list) {}
Robot::Robot(std::vector<IMotor *> motor_list) : motor_list_(motor_list) {}

bool Robot::SetSpeed(Eigen::Vector2d desired_speed,
Eigen::Rotation2Df rotation) {
Expand All @@ -12,7 +11,7 @@ bool Robot::SetSpeed(Eigen::Vector2d desired_speed,
for (auto motor : motor_list_) {
// Get locations for each motor
Location motor_location;
if (!motor.GetMotorLocation(&motor_location)) {
if (!motor->GetMotorLocation(&motor_location)) {
return false;
}

Expand All @@ -21,8 +20,8 @@ bool Robot::SetSpeed(Eigen::Vector2d desired_speed,
float dot_prod = motor_location.position.dot(desired_speed);

// We then scale our desired speed vector, and rotate it to be
// parallel with the wheel
Eigen::Vector2d scaled_speed = desired_speed * dot_prod;
// parallel with the wheel's rotation
Eigen::Vector2d transformed_speed = (desired_speed * dot_prod) * rotation;
}

return true;
Expand Down
9 changes: 7 additions & 2 deletions src/robot/robot.h
Original file line number Diff line number Diff line change
@@ -1,12 +1,17 @@
#pragma once

#include "src/robot/actuator/dynamixel_motor.h"
#include <Eigen/Eigen>

class Robot {
private:
std::vector<DynamixelMotor> motor_list_;
// We need to use pointers to allow polymorphism to work. We
// cannot have a vector of references to an abstract class, as
// an abstract class itself can never be instantiated.
std::vector<IMotor *> motor_list_;

public:
Robot(std::vector<DynamixelMotor> motor_list);
Robot(std::vector<IMotor *> motor_list);

bool SetSpeed(Eigen::Vector2d unit_speed, Eigen::Rotation2Df rotation);
};

0 comments on commit c1000b3

Please sign in to comment.