Umstellung auf Floatgenerell bei Speed

Umstellung auf Float bei Beschleunigung
Entfernen von minspeed
Entfernen nicht notwendiger Konstruktoren in Lokomotive
This commit is contained in:
2023-05-19 16:41:59 +02:00
parent a5f5b18a27
commit fe94db4e88
5 changed files with 51 additions and 66 deletions

View File

@@ -2,24 +2,12 @@
#include<fahrt.h>
#include<locomotive.h>
Fahrt::Fahrt(Locomotive locomotive, MotorDirection Direction, Gleis gleis, Gleisabschnitt gleisabschnitt): _locomotive(locomotive), _gleis(gleis), _gleisabschnitt(gleisabschnitt)
{
_direction = Direction;
_speed = locomotive.getDefaultSpeed();
_bremsweg=0;
}
Fahrt::Fahrt(Locomotive locomotive, byte speed, MotorDirection Direction, Gleis gleis, Gleisabschnitt gleisabschnitt): _locomotive(locomotive), _gleis(gleis), _gleisabschnitt(gleisabschnitt)
{
_direction = Direction;
_speed = speed;
_bremsweg=0;
}
Fahrt::Fahrt(Locomotive locomotive, byte speed, MotorDirection Direction, Gleis gleis, Gleisabschnitt gleisabschnitt, float bremsweg): _locomotive(locomotive), _gleis(gleis), _gleisabschnitt(gleisabschnitt)
Fahrt::Fahrt(Locomotive locomotive, byte speed, MotorDirection Direction, Gleis gleis, float startbeschleunigung, Gleisabschnitt gleisabschnitt, float bremsweg): _locomotive(locomotive), _gleis(gleis), _gleisabschnitt(gleisabschnitt)
{
_direction = Direction;
_speed = speed;
_startbeschleunigung = startbeschleunigung;
_bremsweg=bremsweg;
}
@@ -50,10 +38,7 @@ void Fahrt::Loop()
delay(20);
if (_gleisabschnitt.IstBesetzt() && !_abschnittErkannt)
{
if (_bremsweg==0)
_locomotive.Break();
else
_locomotive.Accelerate(0, _bremsweg);
_locomotive.Accelerate(0, _bremsweg);
_abschnittErkannt=true;
}
}

View File

@@ -8,9 +8,7 @@
class Fahrt
{
public:
Fahrt(Locomotive locomotive, MotorDirection Direction, Gleis gleis, Gleisabschnitt gleisabschnitt);
Fahrt(Locomotive locomotive, byte speed, MotorDirection Direction, Gleis gleis, Gleisabschnitt gleisabschnitt);
Fahrt(Locomotive locomotive, byte speed, MotorDirection Direction, Gleis gleis, Gleisabschnitt gleisabschnitt, float bremsweg);
Fahrt(Locomotive locomotive, byte speed, MotorDirection Direction, Gleis gleis, float startbeschleunigung, Gleisabschnitt gleisabschnitt, float bremsweg);
void Vorbereiten();
void Start();
void Loop();
@@ -27,6 +25,7 @@ class Fahrt
Gleisabschnitt _gleisabschnitt;
bool _abschnittErkannt;
byte _speed;
float _startbeschleunigung;
float _bremsweg;
};

View File

@@ -2,22 +2,17 @@
#include<motor.h>
#include<math.h>
Locomotive::Locomotive(int accelerate, int decelerate, byte minSpeed, byte maxSpeed, IMotor& motor): _motor(motor)
Locomotive::Locomotive(float accelerate, float decelerate, byte minSpeedStart, byte minSpeedStop, IMotor& motor): _motor(motor)
{
_currentSpeed=0;
_currentAcc=0;
_accelerate=accelerate;
_decelerate=decelerate;
_minSpeed=minSpeed;
_maxSpeed=maxSpeed;
_minSpeedStart=minSpeedStart;
_minSpeedStop=minSpeedStop;
};
void Locomotive::Accelerate()
{
Accelerate(_maxSpeed);
};
void Locomotive::Accelerate(byte Speed)
{
/* Serial.print("Locomotive.Accelerate: ");
@@ -26,6 +21,11 @@ void Locomotive::Accelerate(byte Speed)
Serial.print(Speed);
Serial.print(" ");
*/
Serial.print("Accelerate: currentspeed=");
Serial.print((_currentSpeed));
Serial.print(" targetspeed=");
Serial.print(Speed);
_targetSpeed = Speed;
_millisStart = millis()-_delay;
if (_targetSpeed>_currentSpeed)
@@ -33,31 +33,41 @@ void Locomotive::Accelerate(byte Speed)
else
_currentAcc = - _decelerate;
Serial.print(" _currentAcc=");
Serial.print(_currentAcc);
Serial.println();
};
void Locomotive::Accelerate(byte Speed, float bremsweg)
{
Serial.print("Accelerate: currentspeed=");
Serial.print((_currentSpeed));
Serial.print(" targetspeed=");
Serial.print(Speed);
Serial.print(" bremsweg=");
Serial.print((bremsweg));
_targetSpeed = Speed;
_currentSpeed = (_currentSpeed<_minSpeed)? _minSpeed : _currentSpeed;
_currentSpeed = (_currentSpeed<_minSpeedStart)? _minSpeedStart : _currentSpeed;
_millisStart = millis()-_delay;
byte _targetSpeed2 = ((_targetSpeed < _currentSpeed)&&(_targetSpeed == 0)) ? _minSpeed : _targetSpeed;
byte _targetSpeed2 = ((_targetSpeed < _currentSpeed)&&(_targetSpeed == 0)) ? _minSpeedStop : _targetSpeed;
int deltaspeed = (_targetSpeed2 - _currentSpeed);
_currentAcc = copysign(deltaspeed * deltaspeed / (100.0 * bremsweg), deltaspeed);
_currentAcc = copysign(deltaspeed * deltaspeed / (100.0 * bremsweg ) * (_delay / 300.0) , deltaspeed);
if (_currentAcc == 0)
_currentAcc = copysign(1, deltaspeed);
Serial.print(" targetspeed2=");
Serial.print(_targetSpeed2);
Serial.print(" _currentAcc=");
Serial.print(_currentAcc);
Serial.println();
};
void Locomotive::Break()
{
Accelerate(0);
};
void Locomotive::setSpeed(byte Speed)
void Locomotive::setSpeed(float Speed)
{
_currentSpeed = Speed;
_motor.setSpeed( (int) (_currentSpeed+0.5));
@@ -86,14 +96,14 @@ void Locomotive::loop()
Serial.println(newspeed);
*/
int breakspeed = _minSpeed * 80 / 100;
int breakspeed = _minSpeedStop;
if ((newspeed>_targetSpeed) && (_currentAcc>0))
{
newspeed = _targetSpeed;
_currentAcc =0;
}
else if ((newspeed<_minSpeed) && (_currentAcc>0)) newspeed = _minSpeed;
else if ((newspeed<_minSpeedStart) && (_currentAcc>0)) newspeed = _minSpeedStart;
else if ((newspeed<breakspeed) && (_currentAcc<0))
{
newspeed = 0;
@@ -107,13 +117,8 @@ void Locomotive::loop()
};
byte Locomotive::currentSpeed()
float Locomotive::currentSpeed()
{
return ((byte) (_currentSpeed+0.5));
};
byte Locomotive::getDefaultSpeed()
{
return _maxSpeed;
return (_currentSpeed);
};

View File

@@ -7,26 +7,22 @@
class Locomotive
{
public:
Locomotive(int accelerate, int decelerate, byte minSpeed, byte maxSpeed, IMotor& motor);
Locomotive(int accelerate, byte speed, int decelerate, byte minSpeed, byte maxSpeed, IMotor& motor);
void Accelerate();
Locomotive(float accelerate, float decelerate, byte minSpeedStart, byte minSpeedStop, IMotor& motor);
void Accelerate(byte Speed);
void Accelerate(byte Speed, float bremsweg);
void Break();
void setSpeed(byte Speed);
void setSpeed(float Speed);
void loop();
byte currentSpeed();
float currentSpeed();
void setDirection(MotorDirection dir);
byte getDefaultSpeed();
private:
int _accelerate;
int _decelerate;
float _accelerate;
float _decelerate;
float _currentAcc;
byte _minSpeed;
byte _maxSpeed;
byte _minSpeedStart;
byte _minSpeedStop;
byte _targetSpeed;
const unsigned int _delay=300;
const unsigned int _delay=100;
unsigned long _millisStart;
float _currentSpeed;
IMotor& _motor;

View File

@@ -25,14 +25,14 @@ Gleisabschnitt bahnhof2(10);
Locomotive locomotiveTaurus(5, 22, 116, 180, regler1);
Locomotive locomotive2043(6, 25, 129, 160, regler1);
*/
Locomotive locomotiveTaurus(5, 22, 58, 120, regler1);
Locomotive locomotive2043(6, 25, 70, 160, regler1);
Locomotive locomotiveTaurus(2.2, 22, 53, 43, regler1);
Locomotive locomotive2043(2.2, 25, 75, 63, regler1);
Fahrt fahrt[] = {
Fahrt(locomotiveTaurus, 135, motorForward, gleis1, bahnhof2, 6.7),
Fahrt(locomotiveTaurus, 125, motorBackward, gleis1, bahnhof1, 5.6) ,
Fahrt(locomotive2043, 145, motorForward, gleis2, bahnhof2, 5.7),
Fahrt(locomotive2043, 148, motorBackward, gleis2, bahnhof1, 5.3)
Fahrt(locomotiveTaurus, 135, motorForward, gleis1, 1.9, bahnhof2, 9.0),
Fahrt(locomotiveTaurus, 125, motorBackward, gleis1, 1.8, bahnhof1, 8.2) ,
Fahrt(locomotive2043, 150, motorForward, gleis2, 2.2, bahnhof2, 9.0),
Fahrt(locomotive2043, 165, motorBackward, gleis2, 2.0, bahnhof1, 8.6)
};
int _currentFahrt;