Simple block detector - zero loss

Post Reply
Richard-TX
Posts: 1320
Joined: 10.07.2012, 04:00
Location: Texas, USA
Contact:

Re: Simple block detector - zero loss

Post by Richard-TX » 27.02.2014, 07:54

Here is some demo code.

enable srcp service within rocrail, configure a sensor for bus #2, addr 200-203, and do not define the interface. Edit the file to use the ip addr of Rocrail, and that should take care of it.

There are lots of commented out print statements so that people can see what is happening if desired. This program covers one ADC chip/4 sensors.

Rob, Get some Pepto-Bismol. You will need it. :D

Code: Select all

#!/usr/bin/python
#Zero Loss Occupancy Detector. - ADC version

import smbus
import time
import sys
import socket


srcp_server="10.232.1.44"
#srcp_server="127.0.0.1"
bus = smbus.SMBus(1)
chip_addr=0x48
srcp_base_addr=200
#The base address is the starting address of the sensors
srcp_bus=2

car_threshold=15
# A 10k ohm load usually reads "30" with two turns on the primary.
min=10
#min is set to 10 to prevent track leakage from tripping sensor

array=[0,0,0,0]
readbuf=""

def initsrcp():
        s = socket.socket()
        s.connect((srcp_server,4303))
        readbuf=s.recv(255)
        s.send("go\n")
        dummy = s.recv(256)
        #print readbuf
        for a in range (0,4):
                cmd = "set "  + str(srcp_bus) + " fb " + str(srcp_base_addr+a) + " 0\n"
                #print cmd
                s.send(cmd)
                readbuf=s.recv(255)
                #print readbuf

        s.close()
        return


initsrcp()
while True:

        for a in range(0,4):
                bus.write_byte_data(chip_addr,0x40 | ((a) & 0x03), 0)
                v = bus.read_byte(chip_addr)
                #print "adc = ",
                #print a,v
                #print "A = %i" %a
                if v > car_threshold:
                        if array[a] != 1:
                                print "car detected - channel %i" %(a)
                                s = socket.socket()
                                s.connect((srcp_server,4303))
                                readbuf=s.recv(255)
                                #print readbuf
                                s.send("go\n")
                                dummy = s.recv(256)
                                cmd = "set " + str(srcp_bus) + " fb " + str(srcp_base_addr+a) + " 1\n"
                                #print cmd
                                s.send(cmd)
                                readbuf=s.recv(255)
                                s.close()
                                array[a]=1
                if v < min:
                        #print "a = %i" %(a)
                        if array[a] == 1:
                                print "car gone - channel %i" %a
                                s = socket.socket()
                                s.connect((srcp_server,4303))
                                readbuf=s.recv(255)
                                #print readbuf
                                s.send("go\n")
                                dummy = s.recv(256)
                                cmd = "set " + str(srcp_bus) + " fb " + str(srcp_base_addr+a) + " 0\n"
                                #print cmd
                                s.send(cmd)
                                readbuf=s.recv(255)
                                s.close()
                                array[a]=0



        time.sleep (.01)
Last edited by Richard-TX on 27.02.2014, 08:08, edited 4 times in total.

Richard-TX
Posts: 1320
Joined: 10.07.2012, 04:00
Location: Texas, USA
Contact:

Re: Simple block detector - zero loss

Post by Richard-TX » 27.02.2014, 08:04

If anyone wants to know why I didn't write it in c the answer is simple. I am just not that good a programmer. :roll:

rjversluis
Site Admin
Posts: 42335
Joined: 10.04.2006, 08:48
Location: Speyer, Germany
Contact:

Re: Simple block detector - zero loss

Post by rjversluis » 27.02.2014, 08:24

Hi Richard,

looks simple and straight forward without trimpot. ;)
Congratulations.

rjversluis
Site Admin
Posts: 42335
Joined: 10.04.2006, 08:48
Location: Speyer, Germany
Contact:

Re: Simple block detector - zero loss

Post by rjversluis » 27.02.2014, 08:28

Unfortunately the range 0x48-0x4F is already in use by the RocMouse...
But if you should disable RocMouse support in Rocnetnode in favor of the sensors the max. number of "ZeroLoss" is 8 x 4 = 32 per RasPi System. For modular layouts its OK...

rjversluis
Site Admin
Posts: 42335
Joined: 10.04.2006, 08:48
Location: Speyer, Germany
Contact:

Re: Simple block detector - zero loss

Post by rjversluis » 27.02.2014, 10:06

Hi Richard,

I added support in Rocnetnode for ADC Sensors. (6868)
Check this option in the Rocnetnode setup "Options Tab" and restart the Rocnetnode program.
Its all untested because I do not have anything to test.

Richard-TX
Posts: 1320
Joined: 10.07.2012, 04:00
Location: Texas, USA
Contact:

Re: Simple block detector - zero loss

Post by Richard-TX » 27.02.2014, 13:53

I knew that there would be a collision with the Rocmouse. There is a solution but you won't like it. :roll:

rjversluis
Site Admin
Posts: 42335
Joined: 10.04.2006, 08:48
Location: Speyer, Germany
Contact:

Re: Simple block detector - zero loss

Post by rjversluis » 27.02.2014, 13:54


Richard-TX
Posts: 1320
Joined: 10.07.2012, 04:00
Location: Texas, USA
Contact:

Re: Simple block detector - zero loss

Post by Richard-TX » 27.02.2014, 14:03

Rob,

When I enable the srcp service within Rocrail, I have to define a sensor with a blank Interface ID to be able to set or clear it. Is that normal?

rjversluis
Site Admin
Posts: 42335
Joined: 10.04.2006, 08:48
Location: Speyer, Germany
Contact:

Re: Simple block detector - zero loss

Post by rjversluis » 27.02.2014, 14:07

Richard-TX wrote:When I enable the srcp service within Rocrail, I have to define a sensor with a blank Interface ID to be able to set or clear it. Is that normal?
Using the SRCP Service is not normal for this; Use the SRCP Library and let your sensor behave like a SRCP server. :roll:

I'm posting here that I added support in Rocnetnode and you are yearning about the !&%§*#@ SRCP Service. :(
This service is for Graphical SRCP Clients; Not for sensor reporting Python Scripts!

Richard-TX
Posts: 1320
Joined: 10.07.2012, 04:00
Location: Texas, USA
Contact:

Re: Simple block detector - zero loss

Post by Richard-TX » 27.02.2014, 14:10

rjversluis wrote:Hi Richard,

I added support in Rocnetnode for ADC Sensors. (6868)
Check this option in the Rocnetnode setup "Options Tab" and restart the Rocnetnode program.
Its all untested because I do not have anything to test.

That was fast Rob. I am stunned. Thank You!



I am going to go test. More to come....

richard

Richard-TX
Posts: 1320
Joined: 10.07.2012, 04:00
Location: Texas, USA
Contact:

Re: Simple block detector - zero loss

Post by Richard-TX » 27.02.2014, 14:16

rjversluis wrote:
Richard-TX wrote:When I enable the srcp service within Rocrail, I have to define a sensor with a blank Interface ID to be able to set or clear it. Is that normal?
Using the SRCP Service is not normal for this; Use the SRCP Library and let your sensor behave like a SRCP server. :roll:

I'm posting here that I added support in Rocnetnode and you are yearning about the !&%§*#@ SRCP Service. :(
This service is for Graphical SRCP Clients; Not for sensor reporting Python Scripts!
I was just wondering about srcp.

I am off to test Rocnetnode.

Richard-TX
Posts: 1320
Joined: 10.07.2012, 04:00
Location: Texas, USA
Contact:

Re: Simple block detector - zero loss

Post by Richard-TX » 27.02.2014, 14:47

Rob,

The ADC checkbox doesn't want to stay on.

I check it, click "set" and then "get" and it is cleared.

Richard-TX
Posts: 1320
Joined: 10.07.2012, 04:00
Location: Texas, USA
Contact:

Re: Simple block detector - zero loss

Post by Richard-TX » 27.02.2014, 14:49

rjversluis wrote:Hi Richard,


Its all untested because I do not have anything to test.

If you have a rocmouse, you can test!

rjversluis
Site Admin
Posts: 42335
Joined: 10.04.2006, 08:48
Location: Speyer, Germany
Contact:

Re: Simple block detector - zero loss

Post by rjversluis » 27.02.2014, 14:50

Richard-TX wrote:If you have a rocmouse, you can test!
Yep. I will this evening.

Richard-TX
Posts: 1320
Joined: 10.07.2012, 04:00
Location: Texas, USA
Contact:

Re: Simple block detector - zero loss

Post by Richard-TX » 27.02.2014, 14:58

Rob,

Under the rocnet programming tab, when I check the trace options, those check boxes get cleared when I click "Set". I have to click "Get" to see them again.

Post Reply

Return to “DIY Hardware”