Skip to content

Commit 080e98b

Browse files
committed
dasad
1 parent d131146 commit 080e98b

10 files changed

+340
-171
lines changed

.vscode/settings.json

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
{
2+
"files.associations": {
3+
"cctype": "cpp",
4+
"clocale": "cpp",
5+
"cmath": "cpp",
6+
"cstdarg": "cpp",
7+
"cstddef": "cpp",
8+
"cstdio": "cpp",
9+
"cstdlib": "cpp",
10+
"cstring": "cpp",
11+
"ctime": "cpp",
12+
"cwchar": "cpp",
13+
"cwctype": "cpp",
14+
"array": "cpp",
15+
"atomic": "cpp",
16+
"bit": "cpp",
17+
"*.tcc": "cpp",
18+
"chrono": "cpp",
19+
"compare": "cpp",
20+
"concepts": "cpp",
21+
"cstdint": "cpp",
22+
"deque": "cpp",
23+
"string": "cpp",
24+
"unordered_map": "cpp",
25+
"vector": "cpp",
26+
"exception": "cpp",
27+
"algorithm": "cpp",
28+
"functional": "cpp",
29+
"iterator": "cpp",
30+
"memory": "cpp",
31+
"memory_resource": "cpp",
32+
"numeric": "cpp",
33+
"random": "cpp",
34+
"ratio": "cpp",
35+
"string_view": "cpp",
36+
"system_error": "cpp",
37+
"tuple": "cpp",
38+
"type_traits": "cpp",
39+
"utility": "cpp",
40+
"fstream": "cpp",
41+
"initializer_list": "cpp",
42+
"iomanip": "cpp",
43+
"iosfwd": "cpp",
44+
"iostream": "cpp",
45+
"istream": "cpp",
46+
"limits": "cpp",
47+
"new": "cpp",
48+
"numbers": "cpp",
49+
"ostream": "cpp",
50+
"sstream": "cpp",
51+
"stdexcept": "cpp",
52+
"streambuf": "cpp",
53+
"cinttypes": "cpp",
54+
"typeinfo": "cpp"
55+
}
56+
}

PositionManager.h

Lines changed: 24 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,9 @@ class PositionManager {
1616
stepsToAccelerate(stepsToAccelerate), decelerationSteps(decelerationSteps), req(req) {}
1717

1818
// Motor control functions
19+
inline void holdPosition(float position);
1920
inline void holdPositionDuration(float position, float duration);
20-
inline void homing(float& commandedPosition, float& currentPosition);
21+
inline bool homing(float& commandedPosition, float& currentPosition);
2122

2223
inline void performAcceleration(float& commandedPosition, float& currentPosition);
2324
inline void performCruising(float& commandedPosition, float& currentPosition);
@@ -53,13 +54,13 @@ void PositionManager::performAcceleration(float& commandedPosition, float& curre
5354

5455
float currentVelocity = 0.0f;
5556
float accelerationPerStep = -maxSpeed / stepsToAccelerate; // Negative for reverse direction
56-
std::cout << "Reverse Acceleration Per Step: " << accelerationPerStep << std::endl;
57+
std::cout << "Acceleration Per Step: " << accelerationPerStep << std::endl;
5758

5859
for (int i = 1; i <= stepsToAccelerate; i++) {
5960
currentVelocity += accelerationPerStep;
6061
float newPosition = accelPositions.back() + currentVelocity;
6162
accelPositions.push_back(newPosition);
62-
std::cout << "Current Reverse Velocity: " << currentVelocity << "\tNew Reverse Position: " << newPosition << std::endl;
63+
std::cout << "CurrentVelocity: " << currentVelocity << "\tNewPosition: " << newPosition << std::endl;
6364
}
6465

6566
for (size_t i = 0; i < accelPositions.size(); i++) {
@@ -71,7 +72,7 @@ void PositionManager::performAcceleration(float& commandedPosition, float& curre
7172
currentPosition = controller_state[0];
7273
torques.push_back(controller_state[2]);
7374
}
74-
std::cout << "Reverse Step: " << i << ", Target: " << accelPositions[i]
75+
std::cout << "Step: " << i << ", Target: " << accelPositions[i]
7576
<< ", Actual: " << currentPosition << "\tVelocity" << controller_state[1] << std::endl;
7677
}
7778
}
@@ -88,7 +89,7 @@ void PositionManager::performCruising(float& commandedPosition, float& currentPo
8889
currentPosition = controller_state[0];
8990
torques.push_back(controller_state[2]);
9091
}
91-
std::cout << "Reverse Cruising at position: " << currentPosition << "\tVelocity" << controller_state[1] << "\tTorque" << controller_state[2] << std::endl;
92+
std::cout << "Cruising at position: " << currentPosition << "\tVelocity" << controller_state[1] << "\tTorque" << controller_state[2] << std::endl;
9293
}
9394
}
9495

@@ -108,10 +109,10 @@ void PositionManager::performDeceleration(float& commandedPosition, float& curre
108109
currentPosition = controller_state[0];
109110
torques.push_back(controller_state[2]);
110111
}
111-
std::cout << "Reverse Decelerating, step " << step + 1 << ": Position = " << controller_state[0]
112+
std::cout << "Decelerating, step " << step + 1 << ": Position = " << controller_state[0]
112113
<< ": Velocity = " << controller_state[1] << std::endl;
113114
if (currentVelocity >= 0) { // Check for stopping condition
114-
std::cout << "Reverse Deceleration complete at step " << step + 1 << std::endl;
115+
std::cout << "Deceleration complete at step " << step + 1 << std::endl;
115116
break;
116117
}
117118
}
@@ -207,8 +208,21 @@ void PositionManager::holdPositionDuration(float position, float duration) {
207208
}
208209
}
209210

211+
// HOLD POSITION
212+
void PositionManager::holdPosition(float position) {
213+
214+
struct timespec req = {0, 1200 * 1000};
215+
controller.sendWriteCommand(position, 0.0);
216+
nanosleep(&req, NULL);
217+
auto controller_state = controller.sendReadCommand();
218+
if (controller_state[0] >= 450 && controller_state[0] <= 550) {
219+
torques.push_back(controller_state[2]);
220+
}
221+
//std::cout << "Position: " << controller_state[0] << " Velocity: " << controller_state[1] << " Torque: " << controller_state[2] << std::endl;
222+
}
223+
210224
// HOMING
211-
void PositionManager::homing(float& commandedPosition, float& currentPosition) {
225+
bool PositionManager::homing(float& commandedPosition, float& currentPosition) {
212226
std::cout << "Starting Homing..." << std::endl;
213227
controller.sendRezeroCommand(500.0); // sets the current position to 500.0
214228
int index = 0;
@@ -233,7 +247,7 @@ void PositionManager::homing(float& commandedPosition, float& currentPosition) {
233247
auto controller_state = controller.sendReadCommand();
234248
torques.push_back(controller_state[2]);
235249
}
236-
break;
250+
return false;
237251
}
238252
}
239253
// Check if the button is pressed
@@ -248,7 +262,7 @@ void PositionManager::homing(float& commandedPosition, float& currentPosition) {
248262
commandedPosition = 500.0f;
249263
currentPosition = 500.0f;
250264
std::cout << "HOMED" << std::endl;
251-
return;
265+
return true;
252266
} // End HOMING
253267

254268
#endif // POSITION_MANAGER_H

VelocityCurveGenerator.h

Lines changed: 0 additions & 43 deletions
This file was deleted.

bk4_position_control_test.cpp

Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
1+
#include "MyController.h"
2+
#include "GraphPlotter.h"
3+
#include "MyGpio.h"
4+
#include "PositionManager.h"
5+
6+
7+
8+
int main() {
9+
// GPIO SETUP
10+
MyGpio homeLimitSwitch("gpiochip0", 24);
11+
if (!homeLimitSwitch.init()) {
12+
std::cerr << "Failed to initialize hom button" << std::endl;
13+
return 1;
14+
}
15+
MyGpio extendLimitSwitch("gpiochip0", 27);
16+
if (!extendLimitSwitch.init()) {
17+
std::cerr << "Failed to initialize extend button" << std::endl;
18+
return 1;
19+
}
20+
MyGpio activateSwitch("gpiochip0", 17);
21+
if (!activateSwitch.init()) {
22+
std::cerr << "Failed to initialize extend button" << std::endl;
23+
return 1;
24+
}
25+
MyGpio safetySwitch("gpiochip0", 26);
26+
if (!activateSwitch.init()) {
27+
std::cerr << "Failed to initialize extend button" << std::endl;
28+
return 1;
29+
}
30+
31+
// CONTROLLER SETUP
32+
MyController controller("/dev/fdcanusb");
33+
if (!controller.setupSerialPort()) {
34+
std::cerr << "Failed to setup serial port" << std::endl;
35+
return 1;
36+
}
37+
controller.sendStopCommand(); //gets controller to a known state
38+
controller.sendRezeroCommand(500.0f); // sets the current position to 500.0
39+
40+
41+
// Initialization
42+
std::vector<float> torques;
43+
const float startPosition = 500.0f;
44+
const float cruisingEndPosition = 480.0f; // + 2.2f;
45+
const float cruisingReverseEndPosition = 520.0f; // - 1.10f;
46+
const float stepsToAccelerate = 50.0f;
47+
const float decelerationSteps = 20.0f;
48+
const float maxSpeed = 0.055f; // Max speed in units per control loop iteration
49+
struct timespec req = {0, 1200 * 1000}; // Control loop frequency
50+
float commandedPosition = startPosition;
51+
float currentPosition = startPosition;
52+
PositionManager positionManager (controller, homeLimitSwitch, extendLimitSwitch, maxSpeed, cruisingEndPosition, cruisingReverseEndPosition, stepsToAccelerate, decelerationSteps, req);
53+
54+
// Homing
55+
positionManager.holdPositionDuration(commandedPosition, 0.5);
56+
positionManager.homing(commandedPosition, currentPosition);
57+
positionManager.holdPositionDuration(commandedPosition, 0.5);
58+
59+
// Perform Forward motion
60+
controller.sendRezeroCommand(500.0f); // sets the current position to 500.0
61+
commandedPosition = 500.0f;
62+
currentPosition = 500.0f;
63+
positionManager.performAcceleration(commandedPosition, currentPosition);
64+
positionManager.performCruising(commandedPosition, currentPosition);
65+
positionManager.performDeceleration(commandedPosition, currentPosition);
66+
positionManager.holdPositionDuration(commandedPosition, 0.5);
67+
68+
// Perform Reverse motion
69+
controller.sendRezeroCommand(500.0f); // sets the current position to 500.0
70+
commandedPosition = 500.0f;
71+
currentPosition = 500.0f;
72+
positionManager.performAccelerationReverse(commandedPosition, currentPosition);
73+
positionManager.performCruisingReverse(commandedPosition, currentPosition);
74+
positionManager.performDecelerationReverse(commandedPosition, currentPosition);
75+
positionManager.holdPositionDuration(commandedPosition, 0.5);
76+
77+
78+
//////////////////////////////////////////////////////////////////////////////////////////
79+
80+
// Stop the motor
81+
//controller.sendStopCommand();
82+
controller.closeSerialPort();
83+
84+
// Graph the torque values collected
85+
//GraphPlotter plotter;
86+
//plotter.plot(positionManager.getTorques(), "Torque Readings Through Various Phases");
87+
88+
return 0;
89+
}
90+

button_test

21.5 KB
Binary file not shown.

button_test.cpp

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,13 @@ int main() {
2323
return 1;
2424
}
2525

26+
MyGpio button4("gpiochip0", 21); //SAFETY BUTTON
27+
28+
if (!button4.init()) {
29+
std::cerr << "Failed to initialize GPIO button" << std::endl;
30+
return 1;
31+
}
32+
2633

2734
while (true) {
2835
//button1
@@ -67,6 +74,20 @@ int main() {
6774
std::cout << "Button not pressed." << std::endl;
6875
}
6976

77+
//button4
78+
int value4 = button4.readValue();
79+
80+
if (value4 == -1) {
81+
std::cerr << "Error reading GPIO value" << std::endl;
82+
break;
83+
}
84+
85+
if (value4 == 0) {
86+
std::cout << "Button pressed44444444444444444444444444444444444444444444444!" << std::endl;
87+
} else {
88+
std::cout << "Button not pressed." << std::endl;
89+
}
90+
7091
// sleep(1); // Check the button state every second
7192
}
7293

position_control_test

-3.94 KB
Binary file not shown.

0 commit comments

Comments
 (0)