Adding Ultraborg sensor readings from ubweb.py to the Diddyborg-web interface
Forums:
Hi
Can someone please help me
I have a Picoborg Reverse (and a Diddyborg metal edition) and a Ultraborg, I have combined all these parts in to my own robot build
I currently use the metalweb.py web interface to control the Robot
BUT I want to be able to show the Ultraborg sensor readings which I can see in ubWeb.py to the metalwb.py interface so everything I need is on one webpage
I would also like if possible to be able to control the buttons for movement using keyboard as well as by clicking the buttons e.g. pressing up arrow to go forward rather than always having to click the button on interface
Can someone help me please?
piborg
Fri, 02/28/2020 - 11:35
Permalink
Adding Ultrasonic readings
It just so happens there is already a version on the WebUI with ultrasonic sensor readings added to it on the forum :)
Custom web UI
Adding keyboard controls is more involved and will require some understanding of HTML / JavaScript to get working. Basically you would need to catch the onkeydown and onkeyup events that occur when a key is pressed and released respectively.
wingers
Fri, 02/28/2020 - 12:08
Permalink
Excellent - thank you, had
Excellent - thank you, had looked through forum but missed that!
wingers
Fri, 02/28/2020 - 12:27
Permalink
works really well - like the
works really well - like the semi auto / auto ideas as well
If someone could possibly help get keyboard controls added it would be perfect
P.S. That script has to have line 259 changed from
if sendFrame != None:
to
if sendFrame is not None:
wingers
Fri, 02/28/2020 - 19:59
Permalink
added touch mode /touch,and keyboard control added to /hold mode
Thanks to the useful post in http://forum.piborg.org/thunderborg/examples/users I have amended scripts from http://forum.piborg.org/comment/4102#comment-4102 to add touch mode /touch, and added keyboard control (arrow keys) to /hold mode, also jpeg quality for streaming can be changed
piborg
Sat, 02/29/2020 - 09:30
Permalink
Great :)
Glad to hear you got everything working.
I had completely forgotten about that other version :)
wingers
Tue, 03/03/2020 - 22:00
Permalink
GitHub and button handling question
I have made all these fixes / improvements to the version on GitHub and submitted a pull request :::
Perhaps you can help / advise on one further change I want to make. I have added a button to the web ui called lights, but I can't work out how you handle it, basically I already have another script called lights_on.py which turns on Knight Rider / KITT style strobing light on front of robot, how can call another script from the web ui from a button onclick?
piborg
Wed, 03/04/2020 - 12:32
Permalink
Adding new buttons
Thanks for the GitHub changes, I am sure others will appreciate it :)
Adding a new button is not hard. We will need to add code in a few places to make it work though.
First we will need to add a new URL to the program which will start the script.
This will need the os module to be imported at the top as well:
Next the page will need a JavaScript function to open this URL. This will be like the
Off
orPhoto
functions.This should be added to the
/
,/hold
, and/touch
versions of the page if they all will have the new button. Add it just below the existingPhoto
function.Finally we need to add a button to the page. This will be a duplicate of the existing
Save Photo
button with just the text and onclick function changed.Again this should be added to the
/
,/hold
, and/touch
versions of the page if they all will have the new button. Add it just below the existingSave Photo
function to put it on the right of that button.wingers
Wed, 03/04/2020 - 15:31
Permalink
Thank you very much
Thank you very much, very helpful indeed
Got it working for most lights I need, but one script is not just simple turn on/off, it actually loops a sequence (so it looks like the car KITT from Knight Rider), this script runs fine using method above but how can I then have a button to stop / kill / cancel that script? any thoughts?
Thanks again
piborg
Wed, 03/04/2020 - 15:52
Permalink
Start and stop a process
This can be done with some small changes :)
First we will need the subprocess module:
Next add a new global value which will keep a reference to the running script:
Replace the
/lights_on
code with this version that starts the script and saves the reference:This also checks if it was already running and does not start it again.
Now add this section for
/lights_off
which will end the script if it is running:Now all that is needed is to add another function and button pair to the pages like before which open the
/lights_off
page instead of/lights_on
to stop the script.wingers
Wed, 03/04/2020 - 17:11
Permalink
typo? and thanks...
Thank you so much
I assume for /lights_on it should be "if lightsProcess is None:" rather than "if lightsProcess is not None:" - as it didn't work initially and my logic assumed that was why?
Much appreciate your assistance, I can program all day with VB.NET but struggle with Python!
I can now fully control my robot via the web interface on computer, phone and tablet - including movement, ultrasonic readings, lights and speech ::!! - thank you :;)
Next stage is to tidy up web ui to make it look nicer, any recommendations for web ui editors or tools to help? always love a good recommendation
piborg
Wed, 03/04/2020 - 19:57
Permalink
Well spotted
Yes, that was a typo. Corrected in the original message :)
Tidying the Web UI is a bit more tricky as the code has all of the HTML written out by hand.
Generally I would access the Web UI from a PC web browser and use the view source option to get the final HMTL output. You can then use a tool like the Tryit Editor to experiment with changes to the HTML and see how they would look.
wingers
Wed, 03/04/2020 - 17:22
Permalink
SyntaxWarning - is that an issue?
Do I need to worry about getting::-
SyntaxWarning: name 'lightsProcess' is assigned to before global declaration when running the script? I had read somewhere that in Python 3.6 onwards these will be classed as Errors so code won't then run? obviously it works fine now and is just a warning so happy to just ignore it
piborg
Wed, 03/04/2020 - 20:00
Permalink
Sounds like a bug
It sounds like a bug, each section which reads or writes
lightsProcess
should have aglobal lightsProcess
line before any of the other lines.It might be worth double-checking for typos (Python is case-sensitive) as that would be the most likely culprit.
wingers
Wed, 03/04/2020 - 20:39
Permalink
global syntaxwarning
Very odd then as definitely got a global lightsProcess at start of code in # Global values section, and then again in both lights on and lights off sections as per your code, all spelled correctly and correct case. Will leave it for now as working fine but still odd
Full error: is:
./MyRobotv4.py:469: SyntaxWarning: name 'lightsProcess' is assigned to before global declaration
global lightsProcess
And then look at line it mentions: and clearly as a global for it....
elif getPath.startswith('/lights'):
global lightsProcess
piborg
Thu, 03/05/2020 - 10:38
Permalink
My mistake
Had another look this morning, it seems like this is my mistake :)
Remove the two
global
lines in the lights on and lights off sections and instead put one further up, just belowdef handle(self):
where the other ones are. That should fix the warning.wingers
Thu, 03/05/2020 - 16:48
Permalink
fix worked
Thank you - can confirm moving the global to where you mention does indeed resolve the error :
Thanks again for all your help