PizzaBoy150 Posted December 15, 2017 Share Posted December 15, 2017 Hi All,I thought it would be a fun project to create a dcc commander using a raspberry pi to control my railway. I am using hornby decoders though.I have created a h bridge circuit with a 12v supply to power it and the h bridge is controlled using the gpio pins on the raspberry pi to create my +/- 12v square wave.The hbridge seems to be working perfectly and i have writted a simple program in c++ to control the pins and send the packet to the train.Though it seems to be functioning more in dc mode than dcc..?I am assuming that the default address of the train is 3? and the documentation i am using is NMRA standard.I have placed the code below to see if anyone can spot my errors?All help and ideas welcome#include <iostream>#include <unistd.h>#include "wiringPi.h"using namespace std;//wiring pi pin numbersint pinA = 8;int pinB = 9;char packet[] = {1,1,1,1,1,1,1,1,1,1,1,1,1,1, 0, 0,0,0,0,0,0,1,1, 0, 0,1,1,1,0,0,1,0, 1};void sendPacket() { int length = sizeof(packet) / sizeof(char); //loop through sending bits for(int i = 0; i < length; i++) {//check for 1 or 0? if(packet == 1) { digitalWrite(pinA, LOW); digitalWrite(pinB, HIGH); usleep(56); digitalWrite(pinB, LOW); digitalWrite(pinA, HIGH); usleep(56); } else { digitalWrite(pinA, LOW); digitalWrite(pinB, HIGH); usleep(100); digitalWrite(pinB, LOW); digitalWrite(pinA, HIGH); usleep(100);} usleep(1000); }}int main(int argc, char *argv[]) {//Start library wiringPiSetup();//setup the pins pinMode(pinA, OUTPUT); pinMode(pinB, OUTPUT); while(1) { sendPacket(); }return 0;} Link to comment Share on other sites More sharing options...
Howbi Posted December 15, 2017 Share Posted December 15, 2017 A DCC decoder expects to receive a form of AC, a modulated pulse wave I believe. Are you sending 12v DC to the track?/media/tinymce_upload/a46dcfc1aff4bd4102a6aa09239f580f.jpg Link to comment Share on other sites More sharing options...
2e0dtoeric Posted December 15, 2017 Share Posted December 15, 2017 I'm not a computer 'geek' so I haven't a clue what all that means!I know C++ is a computer language, but that's the extent of my knowledge! No idea what gpio pins are! (guessing General Purpose in/out?)I prefer Apple pie with custard! 😛 Link to comment Share on other sites More sharing options...
Fishmanoz Posted December 15, 2017 Share Posted December 15, 2017 I’m also not able to decipher PB’s code to see if it is correct. However, I can put HB’s concern to rest as PB says he is first generating a square wave and I’m assuming his programming is modulating that with a packet to send to the loco. PB, the fact you say it is behaving more like DC is probably DC Runaway caused by the decoder not recognizing yours as being a DCC signal and so assuming it is DC. Does your loco take off like a scalded cat and only stop when you remove power? Link to comment Share on other sites More sharing options...
zeroOneman Posted December 15, 2017 Share Posted December 15, 2017 line 16 is checking for packet ==1, this should be packet(i) ==1, as you want to check that element of the array, not the entire array....my syntax may be wrong - you migh need square brackets Link to comment Share on other sites More sharing options...
PizzaBoy150 Posted December 15, 2017 Author Share Posted December 15, 2017 thanks for the response, i definately have a square wave and the code is running as desired, i think its the packet im sending is incorrect. The hornby decoder defaults to the 128 speed step in the datasheet and im not error checking correctly.does anyone have a packet example for 128 speed step?it should be somthing likepreamble1 1 1 1 1 1 1 1 1 1 end bit 0addres0 0 0 0 0 0 1 1 end bit 0data byte start0 1 ? ? ? ? ? ? end bit 0second data bit? ? ? ? ? ? ? ? end bit 0error XOR of address and data? ? ? ? ? ? ? ? end packet 1 Link to comment Share on other sites More sharing options...
zeroOneman Posted December 15, 2017 Share Posted December 15, 2017 it looks to me that your packet will transmit as all '1's and no '0's because of the bug in line 16. try changing it to if(packet == 1) { Link to comment Share on other sites More sharing options...
PizzaBoy150 Posted December 15, 2017 Author Share Posted December 15, 2017 it looks to me that your packet will transmit as all '1's and no '0's because of the bug in line 16. try changing it to if(packet == 1) {thats a condition statement if the number in the packet is equal to 1 it will transmit a 1 bit else { it will transmit a 0 bit } in the else block below. I know the code is correct and its the data in packet which is incorrect Link to comment Share on other sites More sharing options...
zeroOneman Posted December 15, 2017 Share Posted December 15, 2017 yes, but you are testing the entire packet, you should be testing packet which is why you are incrementing i Link to comment Share on other sites More sharing options...
PizzaBoy150 Posted December 15, 2017 Author Share Posted December 15, 2017 I’m also not able to decipher PB’s code to see if it is correct. However, I can put HB’s concern to rest as PB says he is first generating a square wave and I’m assuming his programming is modulating that with a packet to send to the loco. PB, the fact you say it is behaving more like DC is probably DC Runaway caused by the decoder not recognizing yours as being a DCC signal and so assuming it is DC. Does your loco take off like a scalded cat and only stop when you remove power?yes it just runs away full speed until i disconnect Link to comment Share on other sites More sharing options...
PizzaBoy150 Posted December 15, 2017 Author Share Posted December 15, 2017 yes, but you are testing the entire packet, you should be testing packet which is why you are incrementing iI am testing packet? Link to comment Share on other sites More sharing options...
zeroOneman Posted December 15, 2017 Share Posted December 15, 2017 yes,the line if(packet == 1) { is testing to see if packet is equal to 1, but packet is actually '111111111111110000000110011100101' so the statement always returns false and the code goes through the else section.by putting the [ i ] , each iteration through the loop will test the next element in the array.for instance packet [0] is equal to 1but packet[14] is equal to zero Link to comment Share on other sites More sharing options...
PizzaBoy150 Posted December 15, 2017 Author Share Posted December 15, 2017 yes,the line if(packet == 1) { is testing to see if packet is equal to 1, but packet is actually '111111111111110000000110011100101' so the statement always returns false and the code goes through the else section.by putting the [ i ] , each iteration through the loop will test the next element in the array.for instance packet [0] is equal to 1but packet[14] is equal to zero I see when i pasted my code it removed the square brackets for packet they are there in the actual code so it is iterating through the code as desired. Its the format of the packet I am sending which is the issue. Link to comment Share on other sites More sharing options...
zeroOneman Posted December 15, 2017 Share Posted December 15, 2017 ok, so it should start with 10 '1's followed by a zero, then three 8-bit sections, each terminated by a zeroi would guess that the packet should be:{1,1,1,1,1,1,1,1,1,1, 0, 0,0,0,0,0,0,1,1, 0, 0,1,1,1,0,0,1,0, 0,1,1,1,0,0,0,1, 1};i've reduced the number of '1's at the start to ten, then created the xor stream of 8 bits at the end.... Link to comment Share on other sites More sharing options...
zeroOneman Posted December 15, 2017 Share Posted December 15, 2017 nope, that's wrong try {1,1,1,1,1,1,1,1,1,1, 0, 0,0,0,0,0,0,1,1, 0, 0,1,1,1,0,0,1,0,0, 0,1,1,1,0,0,0,1, 1}; Link to comment Share on other sites More sharing options...
PizzaBoy150 Posted December 16, 2017 Author Share Posted December 16, 2017 nope, that's wrong try {1,1,1,1,1,1,1,1,1,1, 0, 0,0,0,0,0,0,1,1, 0, 0,1,1,1,0,0,1,0,0, 0,1,1,1,0,0,0,1, 1};still not working the packet must be right by now? im really stumped with this one... Link to comment Share on other sites More sharing options...
zeroOneman Posted December 16, 2017 Share Posted December 16, 2017 I'll try and have a look at the protocol tomorrow & come up with a packet to get a loco with id 3 to 50% Link to comment Share on other sites More sharing options...
PizzaBoy150 Posted December 16, 2017 Author Share Posted December 16, 2017 finally working as desired, yay!the problem was the delay function i used the one included in wiring pi and it worked!updated code below, thanks chaps!! #include #includeusing namespace std;//wiring pi pin numbersint pinA = 2;int pinB = 0;char packet[] = {1,1,1,1,1,1,1,1,1,1,1,1, 0, 0,0,0,0,0,0,1,1, 0, 0,1,0,0,0,0,1,1, 0, 0,1,0,0,0,0,0,0, 1};void sendPacket() { int length = sizeof(packet) / sizeof(char); //loop through sending bits for(int i = 0; i < length; i++) {//check for 1 or 0? if(packet == 1) { digitalWrite(pinA, LOW); digitalWrite(pinB, HIGH); delayMicroseconds(58); digitalWrite(pinB, LOW); digitalWrite(pinA, HIGH); delayMicroseconds(58); } else { digitalWrite(pinA, LOW); digitalWrite(pinB, HIGH); delayMicroseconds(100); digitalWrite(pinB, LOW); digitalWrite(pinA, HIGH); delayMicroseconds(100);} }}int main(int argc, char *argv[]) {//Start library wiringPiSetup();//setup the pins pinMode(pinA, OUTPUT); pinMode(pinB, OUTPUT);while(1) { sendPacket(); }return 0;} Link to comment Share on other sites More sharing options...
zeroOneman Posted December 16, 2017 Share Posted December 16, 2017 excellent - well done! do keep us up to date with your progress as this develops. Link to comment Share on other sites More sharing options...
RDS Posted December 16, 2017 Share Posted December 16, 2017 Yes, I agree. It has always been a target of mine to use a Raspberry Pi to control a DCC layout. Link to comment Share on other sites More sharing options...
Fishmanoz Posted December 16, 2017 Share Posted December 16, 2017 Given it seems that a wrong packet gives you DC Runaway, it would be a good idea to turn off DC Running in CV29. Subtracting decimal 4 from it will do that for you. Link to comment Share on other sites More sharing options...
PizzaBoy150 Posted December 17, 2017 Author Share Posted December 17, 2017 When i have the program working where i can throw in packets easier then i will make it idiot proof and upload all my code and circuit design. Link to comment Share on other sites More sharing options...
Chrissaf Posted December 17, 2017 Share Posted December 17, 2017 Can I suggest when you do that you post it in the "Off Topic" sub-forum. There are a few electronic projects in that section already and interesting posts stay visible longer as it is a slow moving forum section. Link to comment Share on other sites More sharing options...
PizzaBoy150 Posted December 18, 2017 Author Share Posted December 18, 2017 Can I suggest when you do that you post it in the "Off Topic" sub-forum. There are a few electronic projects in that section already and interesting posts stay visible longer as it is a slow moving forum section.Hi Chris, I will do that Thanks! Link to comment Share on other sites More sharing options...
PizzaBoy150 Posted December 18, 2017 Author Share Posted December 18, 2017 When i have the program working where i can throw in packets easier then i will make it idiot proof and upload all my code and circuit design. Link to comment Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.