carjake
Posts: 18
Joined: Sat Feb 02, 2019 9:30 pm

Force sensor program problem

Tue Feb 05, 2019 1:54 pm

Hello, These day, I am working about 'Playing Video trigger' by using FSR 402 sensor.
I use Raspberry 3 PI b+ model which is installed with Raspbian OS.
My pressure sensor is FSR-406 with MCP3008 ADC, and I connected these components based on below diagram
https://es.mathworks.com/help/supportpk ... meter.html

and that is my code

Code: Select all

import spidev, time, sys, os
from subprocess import Popen
import subprocess as sp


spi = spidev.SpiDev()
spi.open(0, 0)
spi.max_speed_hz = 1000000

def analog_read(channel):
    r = spi.xfer2([1, (0x08 + channel) << 4, 0])
    adc_out = ((r[1]&0x03) << 8) + r[2]
    return adc_out

video1 = ('/home/pi/Videos/sample1.mp4')
video2 = ('/home/pi/Videos/sample2.mp4')

while True:
    reading = analog_read(1)
    time.sleep(1)
    if (reading <= 20):
        zone = 1
    elif (reading > 20):
        zone = 2
    if (zone == 1):
        Popen(['/usr/bin/omxplayer', '--display', '0', video1])
    if (zone == 2):
        Popen(['/usr/bin/omxplayer', '--display', '0', video2])
But I have some problem about my project.

First of all, I though this sensor can detect continuous force, but when I got the output data, this sensor only can detect the force at the moment.

For example, the trigger system should be operated like.. when someone sit on the sensor, the sensor detects the person's wight during siting, then this system play 'video2', and when the person stands up, the sensor's output will be zero or under 20, then play video1. But When I try to run the program, this sensor only detect as someone sit the sensor at the moment, and directly the output became zero. So I wonder, why this sensor only can detect the moment force? Am I wrong something?

Secondly, I made a 'while loop' for continuously playing video1 and 2 depend on the situation, but problem is that same video will start to play every second under own specific condition.

For example, I gave a condition like as 'if (reading <= 20): zone = 1 ....... if (zone == 1): Popen(['/usr/bin/omxplayer', '--display', '0', video1])' but when output data is under 20 situation, this program trigger to play 'video1' in every second. Opposite situation is also same (Over 20 with video2). Can you give me some solution for making that keeping the play 'video1' until the output data is over 20?

Sorry about too long and massive sentences but I am a beginner so If you have any suggestions, I would greatly appreciate it! Thanks!

PhatFil
Posts: 1685
Joined: Thu Apr 13, 2017 3:55 pm
Location: Oxford UK

Re: Force sensor program problem

Tue Feb 05, 2019 4:18 pm

my python is probably at a similar level to yours but i have had the advantage of using other languages.

There are a number of approaches you can employ

if the subprocess lib provides info on the pid (process id) of the process it kicks off with the Popen command you could use that to query if the process is running and if so suppress the playing of a 2nd concurrent video playing.

possibly a quicker to research solution would be to use a timer variable.
You know your video duration.
When you start playing the video record the current time into your timervariable.

Code: Select all

import datetime
vidstarttime=datetime.datetime.now().time()
before playing the video test if (video duration + last time recorded for video start < Now) and if true you can play the vid, if not its still currently playing.

If likely to be used over the transition of a day you may also want to include the date in your test And if the user has control of a pause video feature you could have problems and perhaps looking up how to get a PID and query if its running or not may be worth it..

carjake
Posts: 18
Joined: Sat Feb 02, 2019 9:30 pm

Re: Force sensor program problem

Sat Feb 09, 2019 9:17 pm

PhatFil wrote:
Tue Feb 05, 2019 4:18 pm
my python is probably at a similar level to yours but i have had the advantage of using other languages.

There are a number of approaches you can employ

if the subprocess lib provides info on the pid (process id) of the process it kicks off with the Popen command you could use that to query if the process is running and if so suppress the playing of a 2nd concurrent video playing.

possibly a quicker to research solution would be to use a timer variable.
You know your video duration.
When you start playing the video record the current time into your timervariable.

Code: Select all

import datetime
vidstarttime=datetime.datetime.now().time()
before playing the video test if (video duration + last time recorded for video start < Now) and if true you can play the vid, if not its still currently playing.

If likely to be used over the transition of a day you may also want to include the date in your test And if the user has control of a pause video feature you could have problems and perhaps looking up how to get a PID and query if its running or not may be worth it..
Thank you so much for answering my question! But so sorry I am a beginner, so could you explain more detail about that?

PhatFil
Posts: 1685
Joined: Thu Apr 13, 2017 3:55 pm
Location: Oxford UK

Re: Force sensor program problem

Sun Feb 10, 2019 1:02 am

which method? using time elapsed since last video played ? or checking if the media file player is running or not?
from what you have described i would suggest the time elapsed approach if your user has no control over pause /rewind etc of the vid which would nullify the use of time elapsed significantly.

carjake
Posts: 18
Joined: Sat Feb 02, 2019 9:30 pm

Re: Force sensor program problem

Sun Feb 10, 2019 1:31 pm

PhatFil wrote:
Sun Feb 10, 2019 1:02 am
which method? using time elapsed since last video played ? or checking if the media file player is running or not?
from what you have described i would suggest the time elapsed approach if your user has no control over pause /rewind etc of the vid which would nullify the use of time elapsed significantly.
So sorry, I mean about using time elapsed part. Actually, I don't know how to put the time function in video loof code.
Now I use ultrasonic sensor for trigger, so I know about time function like below

Code: Select all

    StartTime = time.time()
    StopTime = time.time()
 
    while GPIO.input(GPIO_ECHO) == 0:
        StartTime = time.time()
     
    while GPIO.input(GPIO_ECHO) == 1:
        StopTime = time.time()
 
    TimeElapsed = StopTime - StartTime
    distance = (TimeElapsed * 34300) / 2

    return distance



But I don't have any idea how to apply this function for video loof code part.
Thank you for helping me!

Code: Select all

import RPi.GPIO as GPIO
import time, sys, os
from subprocess import Popen
import subprocess as sp

distance = 400
zone = 1  
# to know if omxplayer is playing (= None) or not

#GPIO Mode (BOARD / BCM)
GPIO.setwarnings(False)
GPIO.setmode(GPIO.BCM)
 
#set GPIO Pins
GPIO_TRIGGER = 18
GPIO_ECHO = 24
 
GPIO.setup(GPIO_TRIGGER, GPIO.OUT)
GPIO.setup(GPIO_ECHO, GPIO.IN)

movie2 = ('/home/pi/Videos/sample1.mp4')
movie1 = ('/home/pi/Videos/the tide.mp4')

def distance():
    GPIO.output(GPIO_TRIGGER, True)
 
    time.sleep(0.00001)
    GPIO.output(GPIO_TRIGGER, False)
 
    StartTime = time.time()
    StopTime = time.time()

    while GPIO.input(GPIO_ECHO) == 0:
        StartTime = time.time()
 
 
    while GPIO.input(GPIO_ECHO) == 1:
        StopTime = time.time()
        
    TimeElapsed = StopTime - StartTime
    distance = (TimeElapsed * 34300) / 2

    return distance

if __name__ == '__main__':
    try:
        while True:
            dist = distance()
            print ("Measured Distance = %.1f cm" % dist)
            time.sleep(1)
            
            if (distance() <= 20):
                zone = 1
            elif (distance() > 20):
                zone = 2
        
            if (zone == 1):
                Popen(['/usr/bin/omxplayer', '--display','0', movie1])
                
            if (zone == 2):
                Popen(['/usr/bin/omxplayer', '--display','0', movie2])
                
                
    except KeyboardInterrupt:
    # exits when you press CTRL+C
      os.system('killall omxplayer.bin')
      GPIO.cleanup()

PhatFil
Posts: 1685
Joined: Thu Apr 13, 2017 3:55 pm
Location: Oxford UK

Re: Force sensor program problem

Mon Feb 11, 2019 4:00 am

you have most of the ammunition needed ;) for the simplest implementation to follow I would suggest checking out the basic arduino example sketches 'Blink' and Blink without delay' https://www.arduino.cc/en/Tutorial/Blink And https://www.arduino.cc/en/tutorial/BlinkWithoutDelay

while its not python both examples are very well explained and annotated. Your homework is doing addition and subtarction from a time with second units..
like

Code: Select all

import datetime
a = datetime.datetime(100,1,1,11,34,59)
b = a + datetime.timedelta(0,3) # days, seconds, then other fields.
print a.time()
print b.time()
results in the two values, three seconds apart:

11:34:59
11:35:02

carjake
Posts: 18
Joined: Sat Feb 02, 2019 9:30 pm

Re: Force sensor program problem

Mon Feb 11, 2019 9:18 pm

PhatFil wrote:
Mon Feb 11, 2019 4:00 am
you have most of the ammunition needed ;) for the simplest implementation to follow I would suggest checking out the basic arduino example sketches 'Blink' and Blink without delay' https://www.arduino.cc/en/Tutorial/Blink And https://www.arduino.cc/en/tutorial/BlinkWithoutDelay

while its not python both examples are very well explained and annotated. Your homework is doing addition and subtarction from a time with second units..
like

Code: Select all

import datetime
a = datetime.datetime(100,1,1,11,34,59)
b = a + datetime.timedelta(0,3) # days, seconds, then other fields.
print a.time()
print b.time()
results in the two values, three seconds apart:

11:34:59
11:35:02
Thank you very much! That's very detail and clear explanation about time function :)
But, I have last question, sorry!
I wonder about main difference between 'a = datetime.datetime(100,1,1,11,34,59) b = a + datetime.timedelta(0,3)' and just give 'time.sleep(3) '

PhatFil
Posts: 1685
Joined: Thu Apr 13, 2017 3:55 pm
Location: Oxford UK

Re: Force sensor program problem

Mon Feb 11, 2019 10:42 pm

carjake wrote:
Mon Feb 11, 2019 9:18 pm

I wonder about main difference between 'a = datetime.datetime(100,1,1,11,34,59) b = a + datetime.timedelta(0,3)' and just give 'time.sleep(3) '

The main difference is if you use the sleep function your code stops executing for the duration of the sleep period and will ignore any events that you may? want to respond to. (this might be a desirable effect for some uses)

But if you want your system to respond in almost real time to inputs and other events then the sleep approach isnt very useful as it prohibits anything else from executing.

carjake
Posts: 18
Joined: Sat Feb 02, 2019 9:30 pm

Re: Force sensor program problem

Tue Feb 12, 2019 8:33 pm

PhatFil wrote:
Mon Feb 11, 2019 10:42 pm
carjake wrote:
Mon Feb 11, 2019 9:18 pm

I wonder about main difference between 'a = datetime.datetime(100,1,1,11,34,59) b = a + datetime.timedelta(0,3)' and just give 'time.sleep(3) '

The main difference is if you use the sleep function your code stops executing for the duration of the sleep period and will ignore any events that you may? want to respond to. (this might be a desirable effect for some uses)

But if you want your system to respond in almost real time to inputs and other events then the sleep approach isnt very useful as it prohibits anything else from executing.
Wow you save me! LOL Thank you so much, now I can do what I wanted :)

Return to “Other projects”