cygni52
Posts: 4
Joined: Sun Jun 08, 2014 12:33 am

FSR with Scratch

Sun Jun 08, 2014 12:43 am

Hello,

I am trying to help my class develop a video game where they throw a ball at a target (force sensitive resistor) which then powers up the in game character for a brief amount of time. I have wired the FSR to the raspberry pi using the following tutorial from adafruit:

https://learn.adafruit.com/reading-a-an ... i/overview

Rather than use the potentiometer, I have wired the FSR

I am now wondering if this is a fruitless pursuit? Can such a setup work within Scratch?

Any guidance would be greatly appreciated!

Thanks in advance to anyone that can offer some help.

Take care!

ghp
Posts: 1543
Joined: Wed Jun 12, 2013 12:41 pm
Location: Stuttgart Germany
Contact: Website

Re: FSR with Scratch

Sun Jun 08, 2014 11:10 am

Hello,

connecting an ADC MCP3208 via SPI to RPi is not a problem. Connecting this to scratch depends on the capabilities of a client software. In my school workshop, I use my own implementation for this named 'scratchClient', written in python. I use MCP3202 there. See my website for details http://heppg.de/ikg/wordpress/?p=134, http://heppg.de/ikg/wordpress/?page_id=4

I guess the problem is timing. The force is applied during quite short time, and you need to measure values at double the rate minimum. Assume the 'impact time' is 100 ms, you need to measure 20 times a second or faster. This is beyond the capabilities of scratch.
What you can do is to place the polling task to the scratchClient software, which performs peak detection and then sends a 'hit'-broadcast event to scratch. My scratchClient implementation has a 'adapter' concept which easily allows this.

Another possibility would be to have the target fixed to a pendulum, which the ball starts to move. Place a potentiometer on the pendulum axis. The movement of the pendulum is quite slow, with a board of 70cm length I got 1.4 sec pendulum period time. This is well in the range of scratch response time. And use a heavy, sand filled, soft ball which gives good impulse transfer.
Or use a forked light barrier.
The next problem is to provide a suitable brake or attenuation, otherwise you need to wait for minutes till the next event is possible. But this is mechanics and not scratch then.

Another possibility is to use an electronic analog comparator and a set/reset flipflop. A gpio input and output needed, op amp and cd4013. The pulse peak from FSR sets the FF, when scratch has read this it resets the FF.

Regards, Gerhard

simplesi
Posts: 2327
Joined: Fri Feb 24, 2012 6:19 pm
Location: Euxton, Lancashire, UK
Contact: Website

Re: FSR with Scratch

Sun Jun 08, 2014 1:42 pm

If you don't need to get a reading of the force of the ball and can make your sensor just give high/low outputs then my ScratchGPIO can detect very short events.
http://cymplecy.wordpress.com/scratchgpio/
Seeking help with Scratch and I/O stuff for Primary age children
http://cymplecy.wordpress.com/ @cymplecy on twitter

cygni52
Posts: 4
Joined: Sun Jun 08, 2014 12:33 am

Re: FSR with Scratch

Sun Jun 08, 2014 3:07 pm

Hello,

Thank you both for your reply! I don't need an actual reading of how much force is being applied, simply a way of telling scratch the ball hit the FSR, that would enable a 10 second power up the main character in the game. Simplesi, thank you! We have been using your ScratchGPIO for this project, I just am getting started, and trying to get the students to learn along side of me is a fun, but sometimes very daunting and challengin task. I got really confused when I learned that the Raspberry Pi needed the ADC MCP3208 to convert things for the Pi to understand the analog sensor. ghp, Thank you for your post, but I will confess, it is a little beyond my capabilities and am unable to really understand the details of what you're sharing with me. I am relatively new with all of this. I will follow the link you posted and perhaps that will help me better understand. Simplesi, do you have an example on your site somewhere which incorporates the ADC MCP3208 chip with your ScratchGPIO? Perhaps you do and I am just blind? I will keep looking...

Thank you both for taking the time to help me... Your wisdom will be passed on to my students once I figure out how to deliver the message! lol

take care!

simplesi
Posts: 2327
Joined: Fri Feb 24, 2012 6:19 pm
Location: Euxton, Lancashire, UK
Contact: Website

Re: FSR with Scratch

Sun Jun 08, 2014 4:14 pm

No sorry - I support the Analog - Digital conversion using the PCF8591 chip based boards which connects using I2C - I've no SPI support for any device :(

But you could try just wiring your FSR between say RPI physical pin7 and ground and see if ScratchGPIO can detect the difference between hit and not hit with:

when spacebar pressed
forever
wait until pin7 sensor = 0
say "Hit!"
Seeking help with Scratch and I/O stuff for Primary age children
http://cymplecy.wordpress.com/ @cymplecy on twitter

ghp
Posts: 1543
Joined: Wed Jun 12, 2013 12:41 pm
Location: Stuttgart Germany
Contact: Website

Re: FSR with Scratch

Sun Jun 08, 2014 7:57 pm

Hello,

sometimes solutions are more simple if you step back and look from the distance.
I had a cd4013 in my collection, and wired some plywood to build a 'button', formed by a pair of de-isolated wire, crosswise. See the image. The gap is prox 1mm, the length of the 'fork' is 20 cm. At the side of the clamp, I have placed some 1mm thick material between the two plywood stripes. The force needed to close the gap is moderate, and a 100g sand filled ball easily presses it. Assume that at the front of the 'fork', where the wires are connected, a target is connected.
b.jpg
b.jpg (45.52 KiB) Viewed 4806 times
The electrical circuit uses a Set/Reset Flip flop http://en.wikipedia.org/wiki/Flip-flop_ ... tronics%29. The CD4013 also has Clock, Data inputs, which are not used. A high on SET results in a High on the output (connected to GPIO17). The 1kOhm is used to prepare for input/output mismatch on this pin. When RPi sees a high on the input, it should place a High to GPIO18, the reset-signal. Then this line needs to be set to low again.
setresetff_Steckplatine.jpg
setresetff_Steckplatine.jpg (50.64 KiB) Viewed 4806 times
And the game can begin again.

Advantage is, that the circuit records even very fast glitches, and keeps them recorded till scratch resets the logic. And you do not need any analog stuff, which keeps risk low. Price for the CD4013 is .40 euro if you buy ten.

Regards
Gerhard

cygni52
Posts: 4
Joined: Sun Jun 08, 2014 12:33 am

Re: FSR with Scratch

Mon Jun 09, 2014 5:40 pm

Thank you gentlemen for your guidance! I will try a few things this evening and see what I can come up with based on your recommendations!

It is a wonderful thing knowing there are people out there in the world that can extend a helpful hand to a complete stranger!

You have my gratitude!

Keep going!

:)

cygni52
Posts: 4
Joined: Sun Jun 08, 2014 12:33 am

Re: FSR with Scratch

Tue Jun 10, 2014 8:30 pm

Hello again,

Problem solved! Thank you simplesi... you were right, for my purpose, this is all it took:

when spacebar pressed
forever
wait until pin7 sensor = 0
say "Hit!"

...here I was thinking this was going to be complicated!

...now the class can try some of the things ghp suggested.

take care everyone!

simplesi
Posts: 2327
Joined: Fri Feb 24, 2012 6:19 pm
Location: Euxton, Lancashire, UK
Contact: Website

Re: FSR with Scratch

Tue Jun 10, 2014 9:05 pm

Please write a blog about it afterwards :)

Simon
Seeking help with Scratch and I/O stuff for Primary age children
http://cymplecy.wordpress.com/ @cymplecy on twitter

User avatar
williamhbell
Posts: 291
Joined: Mon Dec 26, 2011 5:13 pm
Contact: Website Twitter

Re: FSR with Scratch

Tue Jun 10, 2014 10:01 pm

cygni52 wrote:ADC MCP3208 to convert things for the Pi to understand the analog sensor.
Hi,

The MCP3008 (SPI ADC) is supported out of the box by RpiScratchIO [ http://www.themagpi.com/issue/issue-20/ ]. It should be trivial for someone to write the Python plugin for MCP3208 and load the device in Scratch.

The current version of Scratch is typically limited to a 2 Hz sensor update rate. You can see how that limit was reached in the BrickPi Scratch article of Issue 23 [ http://www.themagpi.com/issue/issue-23/ ]

Best regards,

Will

ghp
Posts: 1543
Joined: Wed Jun 12, 2013 12:41 pm
Location: Stuttgart Germany
Contact: Website

Re: FSR with Scratch

Wed Jun 11, 2014 7:57 pm

Hello,

started to prepare some performance test scripts. I am interested to see differences between new scratch, but also in scratch remote sensor connections.

One of the first results here is:
scratch script sending broadcast "ping", and my scratchClient-software responding with "pong". In scratch, this is repeated 200 times and time recorded.

RPi-1.4-scratch fullscreen 30.3 sec (!)
RPi-1.4-scratch edit mode 18.4 sec

RPi-beta4-scratch fullscreen 16 sec.

The legacy scratch in fullscreen needs much longer than in edit mode, although the script animations cost some time. Strange.
The new scratch is 10 percent faster.

What does this mean on IO-Performance ? One event out, one in in 16sec/200 = 80ms or one event in 40ms. This is much faster than reported elsewhere. Not to forget: nothing else running around, no animations or alike.

Regards,
Gerhard

User avatar
williamhbell
Posts: 291
Joined: Mon Dec 26, 2011 5:13 pm
Contact: Website Twitter

Re: FSR with Scratch

Thu Jun 12, 2014 6:35 am

Hi Gerhard,

Would it be correct to assume that you are timing the speed that a message leaves Scratch (broadcast to all) and arrives at your Python code? If so, have you also checked the maximum message retrieval time (Python message sent to Scratch) and the maximum sensor update time? The value of 2Hz previously quoted was based on Scratch receiving a network message from Python and receiving a sensor update. The same program was also performing motor updates with global variables, which appears to slow Scratch down too.

Glad to see the new Scratch performance is better.

Best regards,

Will

ghp
Posts: 1543
Joined: Wed Jun 12, 2013 12:41 pm
Location: Stuttgart Germany
Contact: Website

Re: FSR with Scratch

Thu Jun 12, 2014 6:49 pm

Timing is:

in scratch:

script (A):
on start:
send "ping"

script (B);
when receive "pong"
(admin stuff: count till 200 cycles are reached, measure time, stop)
broadcast "ping"

in scratchClient (over socket, scratch sensor client software in python )
when receive "ping"
broadcast "pong"

So what I measure is the overall time from scratch to client, client to scratch, and a little overhead in scratch.
When a signal would be lost, the execution would stop. Which never happened. The 200-count cycle is compromise between accuracy and impatience. And I ran this in presentation mode, to avoid graphic animations in script display.

Good idea to have value broadcasts as a separate test case.

Regards, Gerhard

ghp
Posts: 1543
Joined: Wed Jun 12, 2013 12:41 pm
Location: Stuttgart Germany
Contact: Website

Re: FSR with Scratch

Thu Jun 12, 2014 9:58 pm

As promised some analog value results.
Here the scratch script. It uses a variable 'a', global, and receives a sensor value 'b'.
analog.gif
analog.gif (5.19 KiB) Viewed 4600 times
The basic story is like: set value a to 1. ScratchClient is receiving this, and increments this value by 1, and sends it for a sensor value 'b'.
scratch checks this b-value, and when a +1 == b, then a gets incremented.

Execution time for this 1000 iterations is (drum fill) 77.1 sec in 1.4-rpi-scratch. Which is close to the 80 ms of the broadcasts.
As each loop consists of a 'send out' and 'send in' path, a single path can be assumed to last prox 40ms.

Regards,
Gerhard

Return to “Scratch”