Il budget a disposizione e' limitatissimo quindi navigando navigando mi sono imbattuto in una soluzione molto semplice e che sembra funzionare molto bene.
Ho investito 12 Euro in una scheda Arduino Mega 2560 e in un'oretta per verificare il codice che vi allego: tutto qui! Arduino parla con Rocrail tramite un cavo USB e Rocrail vede la scheda come MCS2 controller sulla porta 'seriale' assegnata dal PC.
Come sensori utilizzo rilevatori ad infrarossi, sensolri Hall e rilevatori di assorbimento basati sul circuito di DCCWorld per l'occupazione del binario.
Immagine:
48,6 KB
Se qualcuno volesse ulteriori spiegazioni sono qui per voi
Antonello
Codice: Seleziona tutto
/" This program try to implement a kind of sensor management using Arduino Mega 2560,
sending the appropriate info directly to Rocrail by USB. Define an MCS2 controller in
Rocrail. In the controller's properties select "serial" and the Arduino "comport".
It is working with any micro switch or IR module, connected to Mega 2560 digital pins.
With a minor modification you can include the 16 analogue pins as well. No any shield
or other elements are needed. The idea is inspired by Bert Havinga and Joerg Pleumann "/
int a=6; //max 6; number of virtual sensor blocks
int b=8; //max 8; virtual sensor block's port
int sensors[7]; // status "matrix"
byte tx_msg[13]; // CAN message to transmit
void setup() {
Serial.begin(500000);
tx_msg[0] = 0;// prio
tx_msg[1] = 35;// command
tx_msg[2] = 59;// highByte(hash)
tx_msg[3] = 121;// lowByte(hash)
tx_msg[4] = 8;// the number of data bytes in the payload
tx_msg[5] = 0;
tx_msg[6] = 1;// bus number
tx_msg[7] = 0;
tx_msg[9] = 0;
tx_msg[11]= 0;
tx_msg[12]= 0;
for (int ap = 1; ap <= a; ap++) { // read pin status and write to sensors variable
for (int bp = 1; bp <= b; bp++) {
pinMode(ap"b+bp-5, INPUT_PULLUP);
bitWrite(sensors[ap],bp-1,digitalRead(ap"b+bp-5));
tx_msg[8]=ap"b+bp-8; // sensor number
tx_msg[10]=digitalRead(ap"b+bp-5);// pin status
Serial.write(tx_msg,13); // send the pin status to Rocrail
}
}
}
void loop() {
for (int ap = 1; ap <= a; ap++) { // scan pins by virtual blocks and check twice (avoiding mistakes)
for (int bp = 1; bp <= b; bp++) { // there was change on pin state compare to previous scan ?
if (digitalRead(ap"b+bp-5)!=bitRead(sensors[ap],bp-1)) {
delay(10);
if (digitalRead(ap"b+bp-5)!=bitRead(sensors[ap],bp-1)) { // if yes, change the bit
bitWrite(sensors[ap],bp-1,digitalRead(ap"b+bp-5)); // in 'sensors' variable
tx_msg[8]=ap"b+bp-8; // sensor number
tx_msg[10]=digitalRead(ap"b+bp-5);// pin status
Serial.write(tx_msg,13); // send the changed pin status to Rocrail
}
}
}
}
}