Skip to content

Commit 211d4b8

Browse files
committed
decided to use an interface instead of an ordinary class to hold general motor functions. cleaned up Motors.cpp and Motors.h
1 parent cb67c4c commit 211d4b8

File tree

3 files changed

+39
-129
lines changed

3 files changed

+39
-129
lines changed

jetson_script/Arm.h

Lines changed: 4 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
// Includes brought from read_write.cpp in DynamixelSDK
21
#if defined(__linux__)
32
#include <fcntl.h>
43
#include <termios.h>
@@ -11,19 +10,11 @@
1110
#include <stdio.h>
1211

1312
#include "dynamixel_sdk.h" // Uses DYNAMIXEL SDK library
13+
#define PROTOCOL_VERSION 2.0 // Dynamixel Protocol Version
14+
#define DXL_ID 1 // Default ID for Dynamixel servos
15+
#define DEVICENAME "/dev/ttyUSB0" // Linux port assigned to U2D2 (USB-to-serial converter)
1416

15-
// Includes brought from StepperArm in urc-firmware
16-
#include <Arduino.h>
17-
#include <TMC2209.h>
18-
#include <QNEthernet.h>
19-
#include <vector>
20-
#include <RoboClaw.h>
21-
#include <string>
22-
#include <unordered_map>
23-
#include "urc.pb.h"
24-
#include "Messages.hpp"
25-
26-
#define X_SERIES
17+
#define X_SERIES // Type of Dynamixel servos we are using
2718

2819
#define ADDR_TORQUE_ENABLE 64
2920
#define ADDR_GOAL_POSITION 116
@@ -32,17 +23,6 @@
3223
#define MAXIMUM_POSITION_LIMIT 4095 // Refer to the Maximum Position Limit of product eManual
3324
#define BAUDRATE 57600
3425

35-
// DYNAMIXEL Protocol Version (1.0 / 2.0)
36-
// https://emanual.robotis.com/docs/en/dxl/protocol2/
37-
#define PROTOCOL_VERSION 2.0
38-
39-
// Factory default ID of all DYNAMIXEL is 1
40-
#define DXL_ID 1
41-
42-
// Use the actual port assigned to the U2D2.
43-
// ex) Windows: "COM*", Linux: "/dev/ttyUSB*", Mac: "/dev/tty.usbserial-*"
44-
#define DEVICENAME "/dev/ttyUSB0"
45-
4626
#define TORQUE_ENABLE 1
4727
#define TORQUE_DISABLE 0
4828
#define DXL_MOVING_STATUS_THRESHOLD 20 // DYNAMIXEL moving status threshold

jetson_script/Motors.cpp

Lines changed: 14 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,20 @@
1-
#include "Motors.h" // Includes all essential libraries for debugging and running motors
1+
#include "Motors.h"
22

3-
// Pins for each status light may not be the same, change if needed
4-
const int GREEN_PIN = 32;
5-
const int BLUE_PIN = 30;
6-
const int RED_PIN = 31;
3+
/**************************GENERAL LIBRARIES**************************/
4+
/*******************TO BE UTILIZED FOR ALL MOTORS*******************/
75

8-
const int BLINK_RATE_MS = 500;
9-
const int CAN_READ_RATE_MS = 30;
10-
const int UDP_WRITE_RATE_MS = 50;
11-
const int BAUD_RATE = 500000;
12-
const int NUM_MOTORS = 4;
13-
const int PORT = 8443;
6+
#include <Arduino.h>
7+
#include <FlexCAN_T4.h> // CAN enabling tool
8+
#include "SoloCAN.hpp" // SOLO UNO motor controller's library
9+
#include <QNEthernet.h> // Teensy's ethernet library
10+
#include <map>
11+
#include "urc.ph.h" // Protobuf header file for compiling code
12+
#include "Messages.hpp" // Protobuff decode functions (encodeResponse, decodeResponse)
1413

15-
// Need to match the IDs assigned via Motion Terminal
16-
/* NOTE: As of 1/21/2026, there have been issues with testing motors on Motion Terminal
17-
due to firmware update issues. Will have to check motors with traditional firmware code
18-
or potentially using new SOLOs with updated firmware*/
19-
const int MOTOR_IDS[NUM_MOTORS] = {0xA1, 0xA2, 0xA3, 0xA4};
14+
#include "pb_encode.h" // Protobuf encode functions
15+
#include "pb_decode.h" // Protobuf decode functions
16+
17+
#endif
2018

2119
// Data about each motor to be sent to SOLO
2220
enum class CAN_Send_State {
@@ -33,33 +31,6 @@ struct Solo_Feedback_Data {
3331
uint32_t positionFeedback;
3432
};
3533

36-
// Format of status light's data
37-
struct Status_Light_Data {
38-
uint8_t enabled;
39-
uint8_t blink;
40-
};
41-
42-
// Control status light (but cannot link the status light)
43-
void handleLEDRequest(NewStatusLightCommand newMessage) {
44-
if (newMessage.redEnabled == 0) {
45-
digitalWrite(RED_PIN, LOW);
46-
} else {
47-
digitalWrite(RED_PIN, HIGH);
48-
}
49-
50-
if (newMessage.blueEnabled == 0) {
51-
digitalWrite(BLUE_PIN, LOW);
52-
} else {
53-
digitalWrite(BLUE_PIN, HIGH);
54-
}
55-
56-
if (newMessage.greeEnabled == 0) {
57-
digitalWrite(GREEN_PIN, LOW);
58-
} else {
59-
digitalWrite(GREEN_PIN, HIGH);
60-
}
61-
}
62-
6334
// Handles input speed and adjusts it to correct range
6435
int clampDriveRequest(int speedReference) {
6536
if (speedReference > 4000) {

jetson_script/Motors.h

Lines changed: 21 additions & 62 deletions
Original file line numberDiff line numberDiff line change
@@ -1,68 +1,27 @@
1-
// create a general class file that holds same previously used
2-
// functions that we can call in different substystem cpp files
3-
// such as arm, science, and drivetrain
1+
/*
2+
Utilizing this header file as an interface to house empty skeletons for
3+
all the general motor functions that will be used across different
4+
subsystems (arm, science, drivetrain)
5+
*/
46

57
#ifdef MOTORS_H
68
#define MOTORS_H
79

8-
/**************************GENERAL LIBRARIES**************************/
9-
/*******************TO BE UTILIZED FOR ALL MOTORS*******************/
10-
11-
#include <Arduino.h>
12-
#include <FlexCAN_T4.h> // CAN enabling tool
13-
#include "SoloCAN.hpp" // SOLO UNO motor controller's library
14-
#include <QNEthernet.h> // Teensy's ethernet library
15-
#include <map>
16-
#include "urc.ph.h" // Protobuf header file for compiling code
17-
#include "Messages.hpp" // Protobuff decode functions (encodeResponse, decodeResponse)
18-
19-
#include "pb_encode.h" // Protobuf encode functions
20-
#include "pb_decode.h" // Protobuf decode functions
21-
22-
/**************************DYNAMIXEL LIBRARIES**************************/
23-
/*********************TO BE UTILIZED FOR ARM.CPP*********************/
24-
25-
#if defined(__linux__)
26-
#include <fcntl.h>
27-
#include <termios.h>
28-
#define STDIN_FILENO 0
29-
#elif defined(_WIN64)
30-
#include <conio.h>
31-
#endif
32-
33-
#include <stdlib.h>
34-
#include <stdio.h>
35-
36-
#include "dynamixel_sdk.h" // Uses DYNAMIXEL SDK library
37-
#define PROTOCOL_VERSION 2.0 // Dynamixel Protocol Version
38-
#define DXL_ID 1 // Default ID for Dynamixel servos
39-
#define DEVICENAME "/dev/ttyUSB0" // Linux port assigned to U2D2 (USB-to-serial converter)
40-
41-
#define X_SERIES // Type of Dynamixel servos we are using
42-
43-
#define ADDR_TORQUE_ENABLE 64
44-
#define ADDR_GOAL_POSITION 116
45-
#define ADDR_PRESENT_POSITION 132
46-
#define MINIMUM_POSITION_LIMIT 0 // Refer to the Minimum Position Limit of product eManual
47-
#define MAXIMUM_POSITION_LIMIT 4095 // Refer to the Maximum Position Limit of product eManual
48-
#define BAUDRATE 57600
49-
50-
#define TORQUE_ENABLE 1
51-
#define TORQUE_DISABLE 0
52-
#define DXL_MOVING_STATUS_THRESHOLD 20 // DYNAMIXEL moving status threshold
53-
#define ESC_ASCII_VALUE 0x1b
54-
55-
/**************************GENERAL MOTOR FUNCTIONS**************************/
56-
void handleLEDRequest(NewStatusLightCommand message);
57-
int clampDriveRequest(int speedReference);
58-
void handleDriveRequest(DrivetrainRequest message);
59-
60-
/**************************STEPPER ARM FUNCTIONS**************************/
61-
void test_stepper();
62-
void test_stepper_2();
63-
void run_stepper();
64-
void run_stepper_2();
65-
void run_roboclaw_speed(int address, int channel, int speed);
66-
void run_roboclaw_effort(int address, int channel, int effort);
10+
class Motor {
11+
public:
12+
// Essential so that children classes are cleaned up properly
13+
virtual ~Motor() = default;
14+
15+
// Disable slicing as safety precaution
16+
Motor(const Motor&) = delete;
17+
Motor& operator = (const Motor&) = delete;
18+
19+
// General motor methods that each motor in every subsystem may need
20+
// UPDATE AS WE GO ALONG
21+
virtual void setSpeed(double speed) = 0;
22+
virtual void setPosition(double position) = 0;
23+
virtual void setEffort(double torque) = 0;
24+
virtual void stop() = 0;
25+
};
6726

6827
#endif

0 commit comments

Comments
 (0)