0

I'm trying to build a car (Wild Thumper) that i can drive from my Raspberry Pi. Currently i'm using my Raspberry Pi over SSH. It should send to data to my Arduino so it knows when it has to go forward or when to turn.

I've tried making scripts that get called by jQuery (apache on Pi) and send an integer over the serial port but it requires a delay and this is not ideal. (example forwardStart.py:)

import serial
ser = serial.Serial('/dev/ttyACM0', 9600)
ser.open()
# here a delay is needed
ser.write('4') # go forward
ser.close()

To solve this i tried looking for a single python script that read my keyboard and send the correct integer. However, all keylisteners require display and can't be used over SSH.

Can anybody help me with the Python script or another idea that would works?

Thanks!

Brammz
  • 369
  • 1
  • 9
  • 26
  • Is there any particular reason why you don't want to use the RPi's GPIOs directly? – Ignacio Vazquez-Abrams Mar 28 '15 at 23:45
  • because the car needs a higher voltage than what the raspberry can give. I use a motorcontroller that has a power supply in between the arduino and the car for this. – Brammz Mar 29 '15 at 11:22
  • The delay shouldn't be a problem. You should open the serial port only once at startup to avoid this delay every time you want to send a command. Also, Arduino resets every time you establish a serial connection (and maybe that's why you need that delay) – eventHandler Mar 30 '15 at 19:38
  • Yes that's why I need the delay. But using a script for each keypress can't keep the serial port open. Therefore I want a non-display python keylistener that can keep the serial port open. – Brammz Mar 31 '15 at 14:58

1 Answers1

0

You should start reading from here. The idea would be something like

import serial
ser = serial.Serial('/dev/ttyACM0', 9600)
ser.open()
# here a delay is needed

try:
    while 1:
        try:
            key = sys.stdin.read(1) # wait user input
            actionKey = key2action(key) # translate key to action
            ser.write(actionKey) # go forward
        except IOError: pass
finally:
    ser.close()

Note: this code will fail, it's more like pseudo-code to illustrate the idea.

Community
  • 1
  • 1
eventHandler
  • 1,088
  • 12
  • 20