LOCODUINO

Aide
Forum de discussion
Dépôt GIT Locoduino
Flux RSS

samedi 20 avril 2024

Visiteurs connectés : 55

Bibliothèque DCCpp

Encore un DCC++ ? Mais à quoi ça sert ?

. Par : Thierry

Pourquoi une bibliothèque ? Nous sommes devant le cas typique d’un projet dont une bonne partie du code doit pouvoir être ré-utilisée dans d’autres projets. C’est le but d’une bibliothèque, et c’est ce que j’ai décidé de faire après avoir vu tout le monde ici s’en servir, et l’avoir moi-même utilisé dans au moins deux projets différents : (...)

Retourner à l'article

Vous répondez à :

Bibliothèque DCCpp 4 juin 2019 18:02, par Thierry

Tu as raison pour les deux fichiers, mais je dois m’attaquer à la version ESP32 incessamment, je vais les laisser en attendant. J’ai ajouté des arguments à powerOn() et powerOff() pour couper/restaurer le courant sur les deux voies séparément. Mais en fait c’est une vraie gestion de boosters qu’il faudrait, avec une classe dédiée qui s’occuperai de la gestion du courant. En attendant, j’ai fait une version spécifique de la classe CurrentMonitor pour mieux gérer les court-circuits indépendamment. Cette version permettrai aussi d’ajouter des moniteurs sur chaque booster :

/**********************************************************************

CurrentMonitor.h
COPYRIGHT (c) 2013-2016 Gregg E. Berman

Part of DCC   BASE STATION for the Arduino

**********************************************************************/

#ifdef ARDUINO_ARCH_AVR
#ifndef CurrentMonitor_h
#define CurrentMonitor_h

/** Factor to smooth the result...*/
#define  CURRENT_SAMPLE_SMOOTHING   0.01

/** Time between two measurements.
@remark Millis() uses TIMER-0.  For UNO, we change the scale on Timer-0.  For MEGA we do not.  This means CURRENT_SAMPLE_TIME is different for UNO then MEGA.
*/
#if defined(ARDUINO_AVR_UNO) || defined(ARDUINO_AVR_NANO)      // Configuration for UNO
	#define  CURRENT_SAMPLE_TIME        10
#else                                         // Configuration for MEGA    
	#define  CURRENT_SAMPLE_TIME        1
#endif

/** This structure/class describes a current monitor.*/

struct CurrentMonitor{
  static long int sampleTime; /**< time elapsed since last measurement. This delay is common to all monitors. */
	int pin;	/**< Attached check pin.*/
	int signalPin;	/**< signal pin.*/
  float currentSampleMax;	/**< Value of the maximum current accepted without problem.*/
  float current; /**< Value of the last measured current.*/
  const char *msg;	/**< Message to send when excessive current is measured.*/
  /** begin function.
	@param pin	Attached pin. UNEFINED_PIN to inactivate this monitor.
	@param inSignalPin	Pin to set LOW if a shortcut is detectexd.
  @param msg	Message to send to console when a smoothed current value greater than maximum is detected.
  @param inSampleMax	Maximum value of the current. Default is 300.
  */
  void begin(int pin, int inSignalPin, const char *msg, float inSampleMax = 300);
  /** Checks if sufficient time has elapsed since last update. Common to all monitors.
  */
  static boolean checkTime();
  /** Checks the current on this monitor.
  */
  void check();
};

#endif
#endif

Noter le signalPin apparu dans la classe et le begin(), qui correspond à la broche à mettre à LOW si un problème intervient. Et

/**********************************************************************

CurrentMonitor.cpp
COPYRIGHT (c) 2013-2016 Gregg E. Berman

Part of DCC   BASE STATION for the Arduino

**********************************************************************/

#include "Arduino.h"

#ifdef ARDUINO_ARCH_AVR

#include "DCCpp_Uno.h"
#include "CurrentMonitor.h"
#include "Comm.h"

///////////////////////////////////////////////////////////////////////////////

void CurrentMonitor::begin(int pin, int inSignalPin, const char *msg, float inSampleMax)
{
	this->pin = pin;
	this->signalPin = inSignalPin;
	this->msg = msg;
	this->current = 0;
	this->currentSampleMax = inSampleMax;
} // CurrentMonitor::begin
  
boolean CurrentMonitor::checkTime()
{
	if(millis( ) - sampleTime < CURRENT_SAMPLE_TIME)            // no need to check current yet
		return(false);
	sampleTime = millis();                                   // note millis() uses TIMER-0.  For UNO, we change the scale on Timer-0.  For MEGA we do not.  This means CURENT_SAMPLE_TIME is different for UNO then MEGA
	return(true);  
} // CurrentMonitor::checkTime
  
void CurrentMonitor::check()
{
	if (this->pin == UNDEFINED_PIN)
		return;

	this->current = (float)(analogRead(this->pin) * CURRENT_SAMPLE_SMOOTHING   + this->current * (1.0 - CURRENT_SAMPLE_SMOOTHING));      // compute new exponentially-smoothed current

	// current overload and Programming Signal is on (or could have checked Main Signal, since both are always on or off together)
	if (this->current > this->currentSampleMax && digitalRead(this->signalPin) == HIGH)
	{
		digitalWrite(this->signalPin, LOW);
		DCCPP_INTERFACE.print(this->msg);                                     // print corresponding error message
#if !defined(USE_ETHERNET)
		DCCPP_INTERFACE.println("");
#endif
	}
} // CurrentMonitor::check  

long int CurrentMonitor::sampleTime=0;

#endif

Noter la fonction check() qui utilise maintenant cette signalPin pour couper le courant. Il faut aussi modifier DCCpp.cpp pour ajouter l’argument manquant du begin.

void DCCpp::beginMain(uint8_t inOptionalDirectionMotor, uint8_t inSignalPin, uint8_t inSignalEnable, uint8_t inCurrentMonitor)
{
	...
	mainMonitor.begin(DCCppConfig::CurrentMonitorMain, DCCppConfig::SignalEnablePinMain, (char *) "<p2>");

	...
}

void DCCpp::beginProg(uint8_t inOptionalDirectionMotor, uint8_t inSignalPin, uint8_t inSignalEnable, uint8_t inCurrentMonitor)
{
	...

	progMonitor.begin(DCCppConfig::CurrentMonitorProg, DCCppConfig::SignalEnablePinProg, (char *) "<p3>");

	...
}

void DCCpp::begin()
{
	...

	mainMonitor.begin(UNDEFINED_PIN, UNDEFINED_PIN, "");
	progMonitor.begin(UNDEFINED_PIN, UNDEFINED_PIN, "");

	...
}

Ajouter des boosters reviendrait à ajouter de nouvelles instances de la classe avec les pins qui vont bien pour couper le courant juste chez eux si un problème survient.
Je n’ai pas poussé ces modifications dans la bibliothèque officielle d’aujourd’hui parce que je n’ai pas pu tester. Si tu peux le faire, dis moi si ça marche...

Qui êtes-vous ?
Votre message

Pour créer des paragraphes, laissez simplement des lignes vides.

Lien hypertexte

(Si votre message se réfère à un article publié sur le Web, ou à une page fournissant plus d’informations, vous pouvez indiquer ci-après le titre de la page et son adresse.)

98 Messages

Rubrique Bibliothèques

Bibliothèque Accessories (1)

Bibliothèque Accessories (2)

Un décodeur d’accessoires universel (1)

Un décodeur d’accessoires universel (2)

Un décodeur d’accessoires universel (3)

Bibliothèque LcdUi (1)

Bibliothèque LcdUi (2)

La bibliothèque Servo

Bibliothèque SoftWare Serial

Bibliothèque Serial

Bibliothèque EEPROM

Bibliothèque Wire : I2C

Bibliothèque LCD

La bibliothèque ScheduleTable

Bibliothèque MemoryUsage

Bibliothèque EEPROMextent

La bibliothèque SlowMotionServo

Bibliothèque Commanders

Bibliothèque DCCpp

Bibliothèque DcDccNanoController

La bibliothèque ACAN (1)

La bibliothèque ACAN (2)

Bibliothèque LightEffect

Les derniers articles

Bibliothèque LightEffect


Christian

La bibliothèque ACAN (2)


Jean-Luc

La bibliothèque ACAN (1)


Jean-Luc

La bibliothèque SlowMotionServo


Jean-Luc

Bibliothèque DCCpp


Thierry

Bibliothèque DcDccNanoController


Thierry

Bibliothèque LcdUi (2)


Thierry

Bibliothèque LcdUi (1)


Thierry

Bibliothèque Accessories (2)


Thierry

Bibliothèque Accessories (1)


Thierry

Les articles les plus lus

Bibliothèque Wire : I2C

Bibliothèque DCCpp

Bibliothèque Commanders

Bibliothèque LCD

Bibliothèque DcDccNanoController

La bibliothèque Servo

Un décodeur d’accessoires universel (1)

Bibliothèque SoftWare Serial

La bibliothèque ACAN (1)

Bibliothèque Serial