Re: Dutch Rov.
Posted: Sep 30th, 2014, 12:41 pm
This read just like one of those headlines: "We can't make this stuff up!"
Great project!
Great project!
Exploring the Hobby of Building your own ROV - Imagine, Create, Inspire.
https://www.homebuiltrovs.com/rovforum/
Code: Select all
from Tkinter import *
import pyfirmata
# Activate Arduino
board = pyfirmata.Arduino('/dev/ttyACM0')
Left_pin = board.get_pin('d:5:s')
Right_pin = board.get_pin('d:6:s')
Up_pin = board.get_pin('d:9:s')
def switchSliderState():
# Initial all slider are block so ESC receive a neutral value
if (scale5['state'] == NORMAL):
scale5['state'] = DISABLED
scale6['state'] = DISABLED
scale9['state'] = DISABLED
esc5.set(90)
esc6.set(90)
esc9.set(90)
ChangeLeft(esc5.get())
ChangeRight(esc6.get())
ChangeUp(esc9.get())
else:
scale5['state'] = NORMAL
scale6['state'] = NORMAL
scale9['state'] = NORMAL
def ChangeLeft(left):
valueLeft = "Left = "+str(left)
leftDrive.config(text=valueLeft)
Left_pin.write(left)
def ChangeRight(right):
valueRight = "Right = "+str(right)
rightDrive.config(text=valueRight)
Right_pin.write(right)
def ChangeUp(up):
valueUp = "Up = "+str(up)
upDrive.config(text=valueUp)
Up_pin.write(up)
def sel():
selection = "Value = " + str(esc5.get())
label.config(text = selection)
root = Tk()
esc5 = IntVar()
esc6 = IntVar()
esc9 = IntVar()
scale5 = Scale( root, command = ChangeLeft, label = "left", state= DISABLED, variable = esc5, tickinterval=30, length = 180, orient=VERTICAL, from_ = 0, to_ = 180 )
scale6 = Scale( root, command = ChangeRight, label = "right", state= DISABLED, variable = esc6, tickinterval=30, length = 180, orient=VERTICAL, from_ = 0, to_ = 180 )
scale9 = Scale( root, command = ChangeUp, label = "up", state= DISABLED, variable = esc9, tickinterval=30, length = 180, orient=HORIZONTAL, from_ = 0, to_ = 180 )
# ESC with reverse need to arm at center point
esc5.set(90)
scale5.pack(side=LEFT)
esc6.set(90)
scale6.pack(side=RIGHT)
esc9.set(90)
scale9.pack(side=BOTTOM)
button = Button(root, text="(de)activa sliders", command=switchSliderState)
button.pack(side=BOTTOM)
leftDrive = Label(root)
leftDrive.pack(side = TOP)
upDrive = Label(root)
upDrive.pack(side = TOP)
rightDrive = Label(root)
rightDrive.pack(side = TOP)
# Display initial values
ChangeLeft(esc5.get())
ChangeRight(esc6.get())
ChangeUp(esc9.get())
# write pin values neutral
Left_pin.write(90)
Right_pin.write(90)
Up_pin.write(90)
root.mainloop()
Code: Select all
from tkinter import *
import pigpio
import time
import struct
# foutmelding root /dev/gpiomem
# chgrp gpio /dev/gpiomem
# chmod g+rw /dev/giomem
def switchSliderState():
# Initial all slider are block so ESC receive a neutral value
if (scaleLeft['state'] == NORMAL):
scaleLeft['state'] = DISABLED
scaleRight['state'] = DISABLED
scaleUp['state'] = DISABLED
escLeft.set(0)
escRight.set(0)
escUp.set(0)
ChangeLeft(escLeft.get())
ChangeRight(escRight.get())
ChangeUp(escUp.get())
else:
scaleLeft['state'] = NORMAL
scaleRight['state'] = NORMAL
scaleUp['state'] = NORMAL
def ChangeLeft(left):
valueLeft = "Left = "+str(left)
leftDrive.config(text=valueLeft)
pi.set_servo_pulsewidth(17,1500+10*escLeft.get())
def ChangeRight(right):
valueRight = "Right = "+str(right)
rightDrive.config(text=valueRight)
pi.set_servo_pulsewidth(18,1500+10*escRight.get())
def ChangeUp(up):
valueUp = "Up = "+str(up)
upDrive.config(text=valueUp)
pi.set_servo_pulsewidth(27,1500+10*escUp.get())
def ReadAccelerometer():
read = 0
start_time = time.time()
while (time.time()-start_time) < RUNTIME:
# 0x3 = X MSB, 0x4 = X LSB
# 0x5 = Y MSB, 0x6 = Y LSB
# 0x7 = Z MSB, 0x8 = Z LSB
# > = big endian
(s, b) = pi.i2c_read_i2c_block_data(h, 0x03, 6)
if s >= 0:
(x, y, z) = struct.unpack('>3h', buffer(b))
print("{} {} {}".format(x, y, z))
read += 1
valueAcc= "Accel = "+"{} {} {}".format(x, y, z)
labelAccel.config(text=valueAcc)
root.after(1000, ReadAccelerometer)
# inital program
#
# GPIO 17 pin 11 Left
# GPIO 18 pin 12 Right
# GPIO 27 pin 13 Up/ Down
#accelerometer
buffer = memoryview
BUS=1
HMC5883L_I2C_ADDR=0x1E
RUNTIME=60.0
# handle exceptions in the program
pigpio.exceptions = True
pi = pigpio.pi()
if not pi.connected:
print("No connection to PiGPIO")
exit()
# Pins set to output default frequency
pi.set_mode(17, pigpio.OUTPUT)
pi.set_mode(18, pigpio.OUTPUT)
pi.set_mode(27, pigpio.OUTPUT)
# write pin values neutral
pi.set_servo_pulsewidth(17,1500)
pi.set_servo_pulsewidth(18,1500)
pi.set_servo_pulsewidth(27,1500)
h = pi.i2c_open(BUS, HMC5883L_I2C_ADDR)
if h < 0: # Not Connected OK?
print("No accelerometer found")
exit()
# Initialise HMC5883L.
pi.i2c_write_byte_data(h, 0x00, 0xF8) # CRA 75Hz.
pi.i2c_write_byte_data(h, 0x02, 0x00) # Mode continuous reads.
root = Tk()
while True:
escLeft = IntVar()
escRight = IntVar()
escUp = IntVar()
scaleLeft = Scale( root, command = ChangeLeft, label = "left", state= DISABLED, variable = escLeft, tickinterval=10, length = 500, orient=VERTICAL, from_ = 50, to_ = -50 )
scaleRight = Scale( root, command = ChangeRight, label = "right", state= DISABLED, variable = escRight, tickinterval=10, length = 500, orient=VERTICAL, from_ = 50, to_ = -50 )
scaleUp = Scale( root, command = ChangeUp, label = "up", state= DISABLED, variable = escUp, tickinterval=10, length = 500, orient=HORIZONTAL, from_ = 50, to_ = -50 )
# ESC with reverse need to arm at center point
escLeft.set(0)
scaleLeft.pack(side=LEFT)
escRight.set(0)
scaleRight.pack(side=RIGHT)
escUp.set(0)
scaleUp.pack(side=BOTTOM)
button = Button(root, text="(de)activa sliders", command=switchSliderState)
button.pack(side=BOTTOM)
leftDrive = Label(root)
leftDrive.pack(side = TOP)
upDrive = Label(root)
upDrive.pack(side = TOP)
rightDrive = Label(root)
rightDrive.pack(side = TOP)
labelAccel = Label(root)
labelAccel.text=("Accel=")
labelAccel.pack(side = TOP)
# Display initial values
ChangeLeft(escLeft.get())
ChangeRight(escRight.get())
ChangeUp(escUp.get())
# Accelerometer
#ReadAccelerometer()
root.mainloop()
# clean-up stuff
print("Goodbye clean-up now")
pi.set_servo_pulsewidth(17,0)
pi.set_servo_pulsewidth(18,0)
pi.set_servo_pulsewidth(27,0)
#pi.set_PWM_dutycycle(17,0)
#pi.set_PWM_dutycycle(18,0)
#pi.set_PWM_dutycycle(27,0)
pi.i2c_close(h)
pi.stop()
exit()