Umstellung auf Floatgenerell bei Speed
Umstellung auf Float bei Beschleunigung Entfernen von minspeed Entfernen nicht notwendiger Konstruktoren in Lokomotive
This commit is contained in:
@@ -2,24 +2,12 @@
|
|||||||
#include<fahrt.h>
|
#include<fahrt.h>
|
||||||
#include<locomotive.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)
|
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;
|
|
||||||
_bremsweg=0;
|
|
||||||
}
|
|
||||||
|
|
||||||
Fahrt::Fahrt(Locomotive locomotive, byte speed, MotorDirection Direction, Gleis gleis, Gleisabschnitt gleisabschnitt, float bremsweg): _locomotive(locomotive), _gleis(gleis), _gleisabschnitt(gleisabschnitt)
|
|
||||||
{
|
{
|
||||||
_direction = Direction;
|
_direction = Direction;
|
||||||
_speed = speed;
|
_speed = speed;
|
||||||
|
_startbeschleunigung = startbeschleunigung;
|
||||||
_bremsweg=bremsweg;
|
_bremsweg=bremsweg;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -50,9 +38,6 @@ void Fahrt::Loop()
|
|||||||
delay(20);
|
delay(20);
|
||||||
if (_gleisabschnitt.IstBesetzt() && !_abschnittErkannt)
|
if (_gleisabschnitt.IstBesetzt() && !_abschnittErkannt)
|
||||||
{
|
{
|
||||||
if (_bremsweg==0)
|
|
||||||
_locomotive.Break();
|
|
||||||
else
|
|
||||||
_locomotive.Accelerate(0, _bremsweg);
|
_locomotive.Accelerate(0, _bremsweg);
|
||||||
_abschnittErkannt=true;
|
_abschnittErkannt=true;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -8,9 +8,7 @@
|
|||||||
class Fahrt
|
class Fahrt
|
||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
Fahrt(Locomotive locomotive, MotorDirection Direction, Gleis gleis, Gleisabschnitt gleisabschnitt);
|
Fahrt(Locomotive locomotive, byte speed, MotorDirection Direction, Gleis gleis, float startbeschleunigung, Gleisabschnitt gleisabschnitt, float bremsweg);
|
||||||
Fahrt(Locomotive locomotive, byte speed, MotorDirection Direction, Gleis gleis, Gleisabschnitt gleisabschnitt);
|
|
||||||
Fahrt(Locomotive locomotive, byte speed, MotorDirection Direction, Gleis gleis, Gleisabschnitt gleisabschnitt, float bremsweg);
|
|
||||||
void Vorbereiten();
|
void Vorbereiten();
|
||||||
void Start();
|
void Start();
|
||||||
void Loop();
|
void Loop();
|
||||||
@@ -27,6 +25,7 @@ class Fahrt
|
|||||||
Gleisabschnitt _gleisabschnitt;
|
Gleisabschnitt _gleisabschnitt;
|
||||||
bool _abschnittErkannt;
|
bool _abschnittErkannt;
|
||||||
byte _speed;
|
byte _speed;
|
||||||
|
float _startbeschleunigung;
|
||||||
float _bremsweg;
|
float _bremsweg;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|||||||
@@ -2,22 +2,17 @@
|
|||||||
#include<motor.h>
|
#include<motor.h>
|
||||||
#include<math.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;
|
_currentSpeed=0;
|
||||||
_currentAcc=0;
|
_currentAcc=0;
|
||||||
_accelerate=accelerate;
|
_accelerate=accelerate;
|
||||||
_decelerate=decelerate;
|
_decelerate=decelerate;
|
||||||
_minSpeed=minSpeed;
|
_minSpeedStart=minSpeedStart;
|
||||||
_maxSpeed=maxSpeed;
|
_minSpeedStop=minSpeedStop;
|
||||||
|
|
||||||
};
|
};
|
||||||
|
|
||||||
void Locomotive::Accelerate()
|
|
||||||
{
|
|
||||||
Accelerate(_maxSpeed);
|
|
||||||
};
|
|
||||||
|
|
||||||
void Locomotive::Accelerate(byte Speed)
|
void Locomotive::Accelerate(byte Speed)
|
||||||
{
|
{
|
||||||
/* Serial.print("Locomotive.Accelerate: ");
|
/* Serial.print("Locomotive.Accelerate: ");
|
||||||
@@ -26,6 +21,11 @@ void Locomotive::Accelerate(byte Speed)
|
|||||||
Serial.print(Speed);
|
Serial.print(Speed);
|
||||||
Serial.print(" ");
|
Serial.print(" ");
|
||||||
*/
|
*/
|
||||||
|
Serial.print("Accelerate: currentspeed=");
|
||||||
|
Serial.print((_currentSpeed));
|
||||||
|
Serial.print(" targetspeed=");
|
||||||
|
Serial.print(Speed);
|
||||||
|
|
||||||
_targetSpeed = Speed;
|
_targetSpeed = Speed;
|
||||||
_millisStart = millis()-_delay;
|
_millisStart = millis()-_delay;
|
||||||
if (_targetSpeed>_currentSpeed)
|
if (_targetSpeed>_currentSpeed)
|
||||||
@@ -33,31 +33,41 @@ void Locomotive::Accelerate(byte Speed)
|
|||||||
else
|
else
|
||||||
_currentAcc = - _decelerate;
|
_currentAcc = - _decelerate;
|
||||||
|
|
||||||
|
Serial.print(" _currentAcc=");
|
||||||
|
Serial.print(_currentAcc);
|
||||||
|
Serial.println();
|
||||||
|
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
||||||
void Locomotive::Accelerate(byte Speed, float bremsweg)
|
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;
|
_targetSpeed = Speed;
|
||||||
_currentSpeed = (_currentSpeed<_minSpeed)? _minSpeed : _currentSpeed;
|
_currentSpeed = (_currentSpeed<_minSpeedStart)? _minSpeedStart : _currentSpeed;
|
||||||
_millisStart = millis()-_delay;
|
_millisStart = millis()-_delay;
|
||||||
|
|
||||||
byte _targetSpeed2 = ((_targetSpeed < _currentSpeed)&&(_targetSpeed == 0)) ? _minSpeed : _targetSpeed;
|
byte _targetSpeed2 = ((_targetSpeed < _currentSpeed)&&(_targetSpeed == 0)) ? _minSpeedStop : _targetSpeed;
|
||||||
int deltaspeed = (_targetSpeed2 - _currentSpeed);
|
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)
|
if (_currentAcc == 0)
|
||||||
_currentAcc = copysign(1, deltaspeed);
|
_currentAcc = copysign(1, deltaspeed);
|
||||||
|
|
||||||
|
Serial.print(" targetspeed2=");
|
||||||
|
Serial.print(_targetSpeed2);
|
||||||
|
Serial.print(" _currentAcc=");
|
||||||
|
Serial.print(_currentAcc);
|
||||||
|
Serial.println();
|
||||||
};
|
};
|
||||||
|
|
||||||
|
void Locomotive::setSpeed(float Speed)
|
||||||
void Locomotive::Break()
|
|
||||||
{
|
|
||||||
Accelerate(0);
|
|
||||||
};
|
|
||||||
|
|
||||||
void Locomotive::setSpeed(byte Speed)
|
|
||||||
{
|
{
|
||||||
_currentSpeed = Speed;
|
_currentSpeed = Speed;
|
||||||
_motor.setSpeed( (int) (_currentSpeed+0.5));
|
_motor.setSpeed( (int) (_currentSpeed+0.5));
|
||||||
@@ -86,14 +96,14 @@ void Locomotive::loop()
|
|||||||
Serial.println(newspeed);
|
Serial.println(newspeed);
|
||||||
*/
|
*/
|
||||||
|
|
||||||
int breakspeed = _minSpeed * 80 / 100;
|
int breakspeed = _minSpeedStop;
|
||||||
|
|
||||||
if ((newspeed>_targetSpeed) && (_currentAcc>0))
|
if ((newspeed>_targetSpeed) && (_currentAcc>0))
|
||||||
{
|
{
|
||||||
newspeed = _targetSpeed;
|
newspeed = _targetSpeed;
|
||||||
_currentAcc =0;
|
_currentAcc =0;
|
||||||
}
|
}
|
||||||
else if ((newspeed<_minSpeed) && (_currentAcc>0)) newspeed = _minSpeed;
|
else if ((newspeed<_minSpeedStart) && (_currentAcc>0)) newspeed = _minSpeedStart;
|
||||||
else if ((newspeed<breakspeed) && (_currentAcc<0))
|
else if ((newspeed<breakspeed) && (_currentAcc<0))
|
||||||
{
|
{
|
||||||
newspeed = 0;
|
newspeed = 0;
|
||||||
@@ -107,13 +117,8 @@ void Locomotive::loop()
|
|||||||
|
|
||||||
};
|
};
|
||||||
|
|
||||||
byte Locomotive::currentSpeed()
|
float Locomotive::currentSpeed()
|
||||||
{
|
{
|
||||||
return ((byte) (_currentSpeed+0.5));
|
return (_currentSpeed);
|
||||||
};
|
|
||||||
|
|
||||||
byte Locomotive::getDefaultSpeed()
|
|
||||||
{
|
|
||||||
return _maxSpeed;
|
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|||||||
@@ -7,26 +7,22 @@
|
|||||||
class Locomotive
|
class Locomotive
|
||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
Locomotive(int accelerate, int decelerate, byte minSpeed, byte maxSpeed, IMotor& motor);
|
Locomotive(float accelerate, float decelerate, byte minSpeedStart, byte minSpeedStop, IMotor& motor);
|
||||||
Locomotive(int accelerate, byte speed, int decelerate, byte minSpeed, byte maxSpeed, IMotor& motor);
|
|
||||||
void Accelerate();
|
|
||||||
void Accelerate(byte Speed);
|
void Accelerate(byte Speed);
|
||||||
void Accelerate(byte Speed, float bremsweg);
|
void Accelerate(byte Speed, float bremsweg);
|
||||||
void Break();
|
void setSpeed(float Speed);
|
||||||
void setSpeed(byte Speed);
|
|
||||||
void loop();
|
void loop();
|
||||||
byte currentSpeed();
|
float currentSpeed();
|
||||||
void setDirection(MotorDirection dir);
|
void setDirection(MotorDirection dir);
|
||||||
byte getDefaultSpeed();
|
|
||||||
|
|
||||||
private:
|
private:
|
||||||
int _accelerate;
|
float _accelerate;
|
||||||
int _decelerate;
|
float _decelerate;
|
||||||
float _currentAcc;
|
float _currentAcc;
|
||||||
byte _minSpeed;
|
byte _minSpeedStart;
|
||||||
byte _maxSpeed;
|
byte _minSpeedStop;
|
||||||
byte _targetSpeed;
|
byte _targetSpeed;
|
||||||
const unsigned int _delay=300;
|
const unsigned int _delay=100;
|
||||||
unsigned long _millisStart;
|
unsigned long _millisStart;
|
||||||
float _currentSpeed;
|
float _currentSpeed;
|
||||||
IMotor& _motor;
|
IMotor& _motor;
|
||||||
|
|||||||
12
src/main.cpp
12
src/main.cpp
@@ -25,14 +25,14 @@ Gleisabschnitt bahnhof2(10);
|
|||||||
Locomotive locomotiveTaurus(5, 22, 116, 180, regler1);
|
Locomotive locomotiveTaurus(5, 22, 116, 180, regler1);
|
||||||
Locomotive locomotive2043(6, 25, 129, 160, regler1);
|
Locomotive locomotive2043(6, 25, 129, 160, regler1);
|
||||||
*/
|
*/
|
||||||
Locomotive locomotiveTaurus(5, 22, 58, 120, regler1);
|
Locomotive locomotiveTaurus(2.2, 22, 53, 43, regler1);
|
||||||
Locomotive locomotive2043(6, 25, 70, 160, regler1);
|
Locomotive locomotive2043(2.2, 25, 75, 63, regler1);
|
||||||
|
|
||||||
Fahrt fahrt[] = {
|
Fahrt fahrt[] = {
|
||||||
Fahrt(locomotiveTaurus, 135, motorForward, gleis1, bahnhof2, 6.7),
|
Fahrt(locomotiveTaurus, 135, motorForward, gleis1, 1.9, bahnhof2, 9.0),
|
||||||
Fahrt(locomotiveTaurus, 125, motorBackward, gleis1, bahnhof1, 5.6) ,
|
Fahrt(locomotiveTaurus, 125, motorBackward, gleis1, 1.8, bahnhof1, 8.2) ,
|
||||||
Fahrt(locomotive2043, 145, motorForward, gleis2, bahnhof2, 5.7),
|
Fahrt(locomotive2043, 150, motorForward, gleis2, 2.2, bahnhof2, 9.0),
|
||||||
Fahrt(locomotive2043, 148, motorBackward, gleis2, bahnhof1, 5.3)
|
Fahrt(locomotive2043, 165, motorBackward, gleis2, 2.0, bahnhof1, 8.6)
|
||||||
};
|
};
|
||||||
|
|
||||||
int _currentFahrt;
|
int _currentFahrt;
|
||||||
|
|||||||
Reference in New Issue
Block a user