Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Where's a place to do individual things? #48

Closed
thymjan opened this issue Jun 20, 2017 · 14 comments
Closed

Where's a place to do individual things? #48

thymjan opened this issue Jun 20, 2017 · 14 comments

Comments

@thymjan
Copy link

thymjan commented Jun 20, 2017

Where's the right place to implement individual rover gimmicks/sensors?
I connected two headlight LEDs to the explorer pHat outputs and want to switch them on to do journeys by night.

@thymjan thymjan changed the title Where's a place to do indiviudal things Where's a place to do indiviudal things? Jun 20, 2017
@thymjan thymjan changed the title Where's a place to do indiviudal things? Where's a place to do individual things? Jun 20, 2017
@WayneKeenan
Copy link
Owner

It may help for me to mention you can add 'PiCraftZero features' to any kind of standalone python script, e.g. gpiozero scripts. For example the CamJam robot with PiCraftZero controls added, can be run as

python3 camjam_edukit3.py

However, at the moment PiCraftZero just deals with joysticks, sorry, I've not yet added button presses and I've not documented how to use the HTML/JS/WebSocket parts of PiCraftZero in order for someone to be able to create their own UI components and send custom messages (but it's doable and part of 'the plan').

I am up for taking requests so please raise an issue/request for this and anything else you think needs adding. I think adding couple of buttons/switches to the default UI and a 'button press' would be a nice thing to have.

I'd also like to mention that if you've enabled the system service you can also start your own script, instead of the default PiCraftZero script, by adding a line to a 'service' section in the PiCraftZero config.

Here's some steps todo that, e.g. if you have a script at /home/pi/my_robot.py

Running your own script as a service

Edit the PicraftZero config file:

sudo nano /etc/picraftzero.cfg

Add these lines:

[service]
script=/home/pi/my_robot.py

Follow the log file, in the background, before you restart the service:

sudo journalctl -f -u picraftzero & 

Restart the service:

sudo systemctl restart picraftzero

You should see in the logs something like:

... Running user script: /home/pi/my_picraftzero.py

Your script will now run instead of the default PiCraftZero script every time the Pi boots.

@thymjan
Copy link
Author

thymjan commented Jun 20, 2017

Maybe this is a bit stupid (I'm not a professional coder...) but I tried to run the following python-code parallel to picraftzero:

#!/usr/bin/env python
# coding: Latin-1
# Load library functions we want
from explorerhat import output

output.one.on()
output.two.on()

try:
    print('Press CTRL+C to quit')

    while True:
       x = 0

except KeyboardInterrupt:
    output.one.off()
    output.two.off()

print("bye")

The LEDs flicker (normally they should stay on till exit). And by exiting I can't move the rover with picraftzero anymore. Seems to reset the explorer pHat. So this may not be trivial. Also the movement of the Tiny4wd is not as smooth than without the script.

@thymjan
Copy link
Author

thymjan commented Jun 20, 2017

By the way: what's that LED-setting in the [camera]-section about?

@WayneKeenan
Copy link
Owner

When running your own script standalone you need to not run python3 -m picraftzero and/or stop the picraftzero service with:

sudo systemctl stop picraftzero

@WayneKeenan
Copy link
Owner

the LED in the camera section is to enable/disable the operation of the red LED indicating when the camera is capturing.

@WayneKeenan
Copy link
Owner

WayneKeenan commented Jun 20, 2017

I made some modifications to the explorer library and bundle it with picraftzero, for now.

please try replacing:

from explorerhat import output

with

from picraftzero.thirdparty.pimoroni.explorerhat import output

@thymjan
Copy link
Author

thymjan commented Jun 20, 2017

I made the modifications and started the code with:

python3 -m LightsOn.py 
2017-06-20 23:13:46,703 -          zero.py: 18 -                 <module>() ] - INFO - picraftzero, version=0.2.3
2017-06-20 23:13:46,717 -          zero.py: 32 -                 <module>() ] - INFO - Selected pin factory is <class 'gpiozero.pins.rpigpio.RPiGPIOPin'>
2017-06-20 23:13:54,414 -      joystick.py: 44 -                 <module>() ] - INFO - Optional BlueDot library not found
2017-06-20 23:13:54,419 -      joystick.py: 73 -                 <module>() ] - INFO - Using EventDev implementation
2017-06-20 23:13:54,545 -      __init__.py:724 -                 <module>() ] - INFO - Explorer pHAT detected...
Press CTRL+C to quit
^Cbye
/usr/bin/python3: Error while finding spec for 'LightsOn.py' (<class 'AttributeError'>: 'module' object has no attribute '__path__')
2017-06-20 23:15:01,549 -      __init__.py:685 -         explorerhat_exit() ] - INFO - 
Explorer HAT exiting cleanly, please wait...

LEDs are still flickering, control is still much slower, no change of behavior. After CTRL+C picraftzero-gui doesn't work anymore.

@WayneKeenan
Copy link
Owner

I've not replicated your setup but that 'while True' loop you have is a 'busy loop' and is going to be a CPU hog, explains why things have become less responsive.
There is a more efficient way to be 'idle' in python using the signal module and pause(), which is what the start() call uses under the hood in the script below.

Please try this:

#!/usr/bin/env python
# coding: Latin-1
# Load library functions we want

from picraftzero import Joystick, Wheelbase, steering_mixer, start
from picraftzero.thirdparty.pimoroni.explorerhat import output 

joystick = Joystick() 
motors  = Wheelbase(left=0, right=1)
motors.source = steering_mixer(joystick.values) 

output.one.on()
output.two.on()

try:
    print('Press CTRL+C to quit')

    start()

except KeyboardInterrupt:
    output.one.off()
    output.two.off()

print("bye")

@WayneKeenan
Copy link
Owner

For got to add that you start the script by running it 'as normal' without the -m flag:

python3  LightsOn.py 

WayneKeenan added a commit that referenced this issue Jun 21, 2017
@WayneKeenan
Copy link
Owner

Adding an issue to record the need to upstream some of the changes made in the bundled Explorer library #50

@thymjan
Copy link
Author

thymjan commented Jun 21, 2017

The modified explorer_lights.py works excellent with the LEDs now, no flickering anymore.
img_1393

@thymjan
Copy link
Author

thymjan commented Jun 21, 2017

The rock candy joypad has several unused buttons. Is it possible to react on a button press?

Within the pimoroni tinypirate example python script there are possibilities to do so:

 if event.code == "BTN_TL":
                if event.state == True:
                    print("Botton Left")

@WayneKeenan
Copy link
Owner

Cool, good to see it's working. I will raise a separate issue for adding button support.

@WayneKeenan
Copy link
Owner

please see #53

Repository owner locked and limited conversation to collaborators Jun 22, 2017
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants