Skip to content

Commit c2a9550

Browse files
committed
Made a point to point command with PID control. Not tested yet
1 parent 59f2c07 commit c2a9550

File tree

7 files changed

+205
-5
lines changed

7 files changed

+205
-5
lines changed

ut-robomaster/src/communication/cv_board.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,8 @@ void CVBoard::sendOdometryData()
4141
message.messageType = CV_MESSAGE_TYPE_ODOMETRY_DATA;
4242

4343
// TODO: Implement sending of data once odometry module is finished
44-
44+
OdometryData* data = reinterpret_cast<OdometryData*>(message.data);
45+
// data->xPos = odometry->getPosition().getX();
4546
message.setCRC16();
4647
drivers->uart.write(UART_PORT, reinterpret_cast<uint8_t*>(&message), sizeof(message));
4748
}

ut-robomaster/src/communication/cv_board.hpp

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
#include "tap/communication/serial/dji_serial.hpp"
55
#include "tap/communication/serial/uart.hpp"
66
#include "tap/util_macros.hpp"
7-
7+
#include "subsystems/odometry/odometry_subsystem.hpp"
88
#include "cv_message.hpp"
99

1010
namespace src
@@ -14,6 +14,9 @@ class Drivers;
1414
namespace communication
1515
{
1616
using tap::communication::serial::Uart;
17+
using subsystems::odometry::OdometrySubsystem;
18+
using subsystems::chassis::ChassisSubsystem;
19+
using subsystems::turret::TurretSubsystem;
1720

1821
class CVBoard : public tap::communication::serial::DJISerial
1922
{
@@ -83,6 +86,11 @@ class CVBoard : public tap::communication::serial::DJISerial
8386

8487
/** Last turret aiming data received from the CV board */
8588
TurretData lastTurretData;
89+
90+
//used for getting odom data
91+
ChassisSubsystem* chassis;
92+
TurretSubsystem* turret;
93+
OdometrySubsystem *odom;
8694

8795
/** UART port used to communicate with the CV board */
8896
static constexpr Uart::UartPort UART_PORT = Uart::Uart1;

ut-robomaster/src/robots/common/common_control.hpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
#include "subsystems/sound/command_play_sound.hpp"
1212
#include "subsystems/sound/sound_subsystem.hpp"
1313
#include "subsystems/turret/turret_subsystem.hpp"
14+
#include "subsystems/odometry/odometry_subsystem.hpp"
1415
#include "utils/power_limiter/barrel_cooldown.hpp"
1516

1617
#include "drivers.hpp"
@@ -23,6 +24,7 @@ using namespace subsystems::agitator;
2324
using namespace subsystems::flywheel;
2425
using namespace subsystems::turret;
2526
using namespace subsystems::sound;
27+
using namespace subsystems::odometry;
2628

2729
using namespace commands;
2830
using power_limiter::BarrelId;

ut-robomaster/src/robots/standard/standard_control.hpp

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
#include "robots/common/common_control_manual.hpp"
22
#include "subsystems/agitator/command_agitator_continuous.hpp"
3+
#include "subsystems/chassis/command_move_point.hpp"
4+
35

46
class StandardControl : CommonControlManual
57
{
@@ -17,6 +19,7 @@ class StandardControl : CommonControlManual
1719

1820
drivers->commandMapper.addMap(&leftMouseDown);
1921
drivers->commandMapper.addMap(&leftSwitchUp);
22+
drivers->commandMapper.addMap(&pressOdom);
2023
}
2124

2225
private:
@@ -31,6 +34,8 @@ class StandardControl : CommonControlManual
3134
&agitator,
3235
BarrelId::STANDARD1,
3336
true};
37+
CommandOdomPoint movePoint{
38+
drivers, &chassis, &odometry};
3439

3540
// Mappings
3641
HoldCommandMapping leftMouseDown{
@@ -42,4 +47,9 @@ class StandardControl : CommonControlManual
4247
drivers,
4348
{&rotateAgitator_SwitchUp, &rotateFlywheel_SwitchMid},
4449
RemoteMapState(Remote::Switch::LEFT_SWITCH, Remote::SwitchState::UP)};
50+
51+
PressCommandMapping pressOdom{
52+
drivers,
53+
{&movePoint},
54+
RemoteMapState({Remote::Key::B})};
4555
};
Lines changed: 112 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,112 @@
1+
#include "command_move_point.hpp"
2+
3+
namespace commands
4+
{
5+
6+
void CommandOdomPoint::initialize()
7+
{
8+
//initialize the target points, eventually read the waypoints from CV board
9+
desiredX = 0.5f;
10+
desiredY = 1.0f;
11+
12+
}
13+
14+
void CommandOdomPoint::execute()
15+
{
16+
//?
17+
Vector2f inputMove = Vector2f(0.0f, 0.0005f);
18+
19+
//get current position from odometry
20+
currentPosition = odometry->getPosition();
21+
currYaw = odometry->getChassisYaw();
22+
currX = currentPosition.getX();
23+
currY = currentPosition.getY();
24+
25+
//calculate the angle to the target point, for moving the chassis to the target point
26+
//returns -pi to pi
27+
desiredYaw = atan2f(desiredY - currY, desiredX - currX);
28+
29+
currYaw += M_PI;
30+
desiredYaw += M_PI;
31+
32+
float outputRot, offset;
33+
float integral = 0, deriv;
34+
prevError = 0.0f;
35+
//turn the chassis first until reached desiredYaw
36+
//threshold is 0.1 radians
37+
for(float error = desiredYaw - currYaw; abs(error) > angle_threshold; error = desiredYaw - currYaw){
38+
float output;
39+
outputRot = 0.0f;
40+
output = error * p;
41+
integral = (integral + error) * i;
42+
deriv = (error - prevError) * d;
43+
44+
output = output + integral + deriv;
45+
46+
//need to spin the chassis CCW
47+
if (output > 0.5f)
48+
{
49+
outputRot = -0.05f;
50+
}
51+
//need to spin the chassis CW
52+
else if (output < -0.5f)
53+
{
54+
outputRot = 0.05f;
55+
}
56+
57+
offset = 0.0f;
58+
if(error > 1.0f){
59+
offset = -0.05f;
60+
}
61+
else if(error < -1.0f){
62+
offset = 0.05f;
63+
}
64+
65+
66+
chassis->input(Vector2f(0.0f), outputRot + offset); //rotate the chassis
67+
68+
prevError = error;
69+
//wait for a bit
70+
while(!refreshTimer.execute()){}
71+
72+
currYaw = odometry->getChassisYaw();
73+
}
74+
75+
//now rotated to correct angle, move to the target point
76+
chassis->input(Vector2f(0.0f), 0.0f); //stop the rotation
77+
78+
inputMove = inputMove.rotate(desiredYaw); //rotate the movement vector relative to the desired yaw
79+
//move to the target point
80+
chassis->input(inputMove, 0.0f);
81+
82+
83+
//keep moving until reached the target point
84+
//threshold is 0.1m
85+
for(float dist = sqrt(pow(desiredX - currX, 2) + pow(desiredY - currY, 2)); dist > dist_threshold;
86+
dist = sqrt(pow(desiredX - currX, 2) + pow(desiredY - currY, 2))){
87+
currX = currentPosition.getX();
88+
currY = currentPosition.getY();
89+
}
90+
91+
inputMove.setX(0);
92+
inputMove.setY(0);
93+
94+
chassis->input(inputMove, 0.0f);
95+
96+
}
97+
98+
void CommandOdomPoint::end(bool) {chassis->input(Vector2f(0.0f), 0.0f);}
99+
100+
bool CommandOdomPoint::isFinished() const {
101+
//check if the chassis is at the target point within some threshold
102+
// if (fabs(currX - desiredX) < 0.1f && fabs(currY - desiredY) < 0.1f)
103+
// {
104+
// return true;
105+
// }
106+
// else
107+
// {
108+
// return false;
109+
// }
110+
111+
}
112+
} // namespace commands
Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
#pragma once
2+
3+
#include "tap/control/command.hpp"
4+
5+
#include "subsystems/chassis/chassis_subsystem.hpp"
6+
#include "subsystems/odometry/odometry_subsystem.hpp"
7+
#include "communication/cv_message.hpp"
8+
#include "utils/chassis_auto_align.hpp"
9+
10+
11+
#include "drivers.hpp"
12+
13+
namespace commands
14+
{
15+
using namespace modm;
16+
using subsystems::odometry::OdometrySubsystem;
17+
using subsystems::chassis::ChassisSubsystem;
18+
19+
using communication::OdometryData;
20+
21+
class CommandOdomPoint : public tap::control::Command
22+
{
23+
public:
24+
CommandOdomPoint(src::Drivers *drivers, ChassisSubsystem* chassis, OdometrySubsystem *odom)
25+
: drivers(drivers),
26+
chassis(chassis),
27+
odometry(odom)
28+
{
29+
addSubsystemRequirement(chassis);
30+
}
31+
32+
void initialize() override;
33+
34+
void execute() override;
35+
36+
void end(bool interrupted) override;
37+
38+
bool isFinished() const override;
39+
40+
const char *getName() const override { return "rotate odom command"; }
41+
42+
private:
43+
src::Drivers *drivers;
44+
ChassisSubsystem *chassis;
45+
OdometrySubsystem *odometry;
46+
Vector2f currentPosition;
47+
48+
const float p = 1.0f;
49+
const float i = 1.0f;
50+
const float d = 1.0f;
51+
const float dist_threshold = 0.1f; //m
52+
const float angle_threshold = 0.3f; //rads
53+
float prevError = 0.0f;
54+
55+
float currX;
56+
float currY;
57+
58+
float desiredX;
59+
float desiredY;
60+
61+
float currYaw;
62+
float desiredYaw;
63+
64+
tap::arch::PeriodicMilliTimer refreshTimer{10};
65+
66+
}; // class CommandOdomPoint
67+
} // namespace commands

ut-robomaster/src/subsystems/odometry/odometry_subsystem.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -35,9 +35,9 @@ void OdometrySubsystem::refresh()
3535
if (refreshTimer.execute())
3636
{
3737

38-
drivers->rtt.plot(3, getPosition().getX()); //problematic function
39-
drivers->rtt.plot(2, getPosition().getY()); //problematic function
40-
drivers->rtt.plot(1, getLinearVelocity().getX());
38+
drivers->rtt.plot(1, getPosition().getX());
39+
drivers->rtt.plot(2, getPosition().getY());
40+
// drivers->rtt.plot(3, getLinearVelocity().getX());
4141
}
4242
}
4343

0 commit comments

Comments
 (0)