-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathDriveSystem.cs
More file actions
125 lines (113 loc) · 4.63 KB
/
DriveSystem.cs
File metadata and controls
125 lines (113 loc) · 4.63 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
using Avans.StatisticalRobot;
using Avans.StatisticalRobot.Interfaces;
/// <summary>
/// This class controls the movements of the robot
/// </summary>
public class DriveSystem : IUpdatable
{
// Speeds independent of what wheel configuration is used
// so there is a value for forward (and reverse) and a value for turning
// Note: we could have made these C# properties as an alternative to creating
// SetForwardSpeed() and SetTurnSpeed(), to achieve the same result
private double forwardSpeed; // Between -1.0 and 1.0
private double turnSpeed; // Between -1.0 and 1.0
// Speeds for the left and right wheels
private double actualSpeedLeft; // Between -1.0 and 1.0, where 0.0 is stop
private double actualSpeedRight; // Between -1.0 and 1.0, where 0.0 is stop
public DriveSystem()
{
Console.WriteLine("DriveSystem constructor called");
// Start off stationary
forwardSpeed = 0.0;
turnSpeed = 0.0;
CalculateRobotMotorSpeeds(); // Updates actualSpeedLeft and actualSpeedRight
}
/// <summary>
/// Return the current forward speed of the robot
/// </summary>
/// <returns>The logical speed, where -1.0 is full speed reverse, 0.0 is stop,
/// and 1.0 is full speed forward</returns>
public double GetSpeed() => forwardSpeed;
/// <summary>
/// Set the speed of the robot and make it move
/// Use values between -1.0 (reverse) and 1.0 (forward)
/// </summary>
/// <param name="speed">A logical speed value between -1.0 (full speed reverse)
/// and 1.0 (full speed forward), 0.0 means stop</param>
public void SetForwardSpeed(double newSpeed)
{
forwardSpeed = newSpeed;
CalculateRobotMotorSpeeds();
ControlRobotMotorSpeeds();
}
/// <summary>
/// Set the speed of turning the robot and make the robot move
/// Use values between 1.0 for clockwise (right hand turn)
/// and -1.0 for counter-clockwise (left hand turn)
/// 0.0 is straight ahead
/// </summary>
/// <param name="newTurnSpeed">A logical turn speed between -1.0
/// (full speed counterclockwise) and 1.0 (full speed clockwise)</param>
public void SetTurnSpeed(double newTurnSpeed)
{
turnSpeed = newTurnSpeed;
CalculateRobotMotorSpeeds();
ControlRobotMotorSpeeds();
}
/// <summary>
/// Set the speed of the robot to zero to stop the robot
/// </summary>
public void Stop()
{
SetForwardSpeed(0.0);
SetTurnSpeed(0.0);
}
/// <summary>
/// Converts the logical speed value (-1.0 to 1.1) to the value that
/// the robot needs (-300 to 300)
/// Note that 300 is approximately the maximum for the Romi robot motors
/// </summary>
/// <param name="speed">Logical speed value, should be between -1.0 and 1.0</param>
/// <returns>Robot motor speed value</returns>
private short ConvertToRobotSpeedValue(double speed)
{
return (short) Math.Clamp(Math.Round(speed * 300.0), -300.0, 300.0);
}
/// <summary>
/// Calculates actual speed values for the left and right motor
/// by combining forward speed and turn speed
/// </summary>
private void CalculateRobotMotorSpeeds()
{
// Calculate speed for the left and right motors
// Clockwise rotation (turn speed > 0.0) means that the left motor
// moves faster forward than the right motor, so the turn speed
// is added for the left motor and subtracted for the right motor
actualSpeedLeft = Math.Clamp( turnSpeed + forwardSpeed, -1.0, 1.0);
actualSpeedRight = Math.Clamp(-turnSpeed + forwardSpeed, -1.0, 1.0);
Console.WriteLine($"Actual speed left={actualSpeedLeft,4:F2} right={actualSpeedRight,4:F2}");
}
/// <summary>
/// Sends the actual speed to both robot motors
/// </summary>
private void ControlRobotMotorSpeeds()
{
short speedLeft = ConvertToRobotSpeedValue(actualSpeedLeft);
short speedRight = ConvertToRobotSpeedValue(actualSpeedRight);
// Show the motor speed values on the console
Console.WriteLine($"Motor speed left: {speedLeft} right: {speedRight}");
// Change the motor speeds on the robot itself
Robot.Motors(speedLeft, speedRight);
}
/// <summary>
/// This method should be called frequently
/// </summary>
public void Update()
{
// Nothing needed yet
// TODO Use a separate target speed and an actual speed
// and change the actual speed to approach the target speed
// in small steps, to make speed changes of the robot more
// gradual
}
}