Skip to content

Instantly share code, notes, and snippets.

@bitmorse
Created May 17, 2019 00:05
Show Gist options
  • Save bitmorse/c2cbdcfebad369e42e00c02d1c17ae72 to your computer and use it in GitHub Desktop.
Save bitmorse/c2cbdcfebad369e42e00c02d1c17ae72 to your computer and use it in GitHub Desktop.
OIBUS_UAVCAN_DEBUGGER
from multiprocessing import Process, Queue
import uavcan
from contextlib import closing
import tkinter
def node_status_callback(event):
print('#',event.transfer.source_node_id,': ', event.message.uptime_sec)
# Messages, service requests, service responses, and entire events
# can be converted into YAML formatted data structure using to_yaml():
print(uavcan.to_yaml(event))
def getset_response(e):
print(e)
def set_mosfet_driver(node,state,node_id):
node.request(
uavcan.protocol.param.GetSet.Request(
value=uavcan.protocol.param.Value(integer_value=state),
name='drivers'),
node_id,
getset_response,
priority=uavcan.TRANSFER_PRIORITY_HIGHEST
)
def tkinter_worker(q):
class Drivers(object):
A = 0
B = 0
def resetB(self):
q.put(['B', 0])
def setB(self):
q.put(['B', self.B])
def resetA(self):
q.put(['A', 0])
def setA(self):
q.put(['A', self.A])
def toggleA(self, btn, name):
Adrivers = self.A
if btn.config('relief')[-1] == 'sunken':
btn.config(relief="raised")
if name == 'A1':
Adrivers = Adrivers & 0b1111111110
if name == 'A2':
Adrivers = Adrivers & 0b1111111101
if name == 'A3':
Adrivers = Adrivers & 0b1111111011
if name == 'A4':
Adrivers = Adrivers & 0b1111110111
if name == 'A5':
Adrivers = Adrivers & 0b1111101111
if name == 'A6':
Adrivers = Adrivers & 0b1111011111
if name == 'A7':
Adrivers = Adrivers & 0b1110111111
if name == 'A8':
Adrivers = Adrivers & 0b1101111111
if name == 'A9':
Adrivers = Adrivers & 0b1011111111
if name == 'A10':
Adrivers = Adrivers & 0b0111111111
else:
btn.config(relief="sunken")
if name == 'A1':
Adrivers = Adrivers | 0b0000000001
if name == 'A2':
Adrivers = Adrivers | 0b0000000010
if name == 'A3':
Adrivers = Adrivers | 0b0000000100
if name == 'A4':
Adrivers = Adrivers | 0b0000001000
if name == 'A5':
Adrivers = Adrivers | 0b0000010000
if name == 'A6':
Adrivers = Adrivers | 0b0000100000
if name == 'A7':
Adrivers = Adrivers | 0b0001000000
if name == 'A8':
Adrivers = Adrivers | 0b0010000000
if name == 'A9':
Adrivers = Adrivers | 0b0100000000
if name == 'A10':
Adrivers = Adrivers | 0b1000000000
self.A = Adrivers
def toggleB(self, btn, name):
Bdrivers = self.B
if btn.config('relief')[-1] == 'sunken':
btn.config(relief="raised")
if name == 'B1':
Bdrivers = Bdrivers & 0b1111111110
if name == 'B2':
Bdrivers = Bdrivers & 0b1111111101
if name == 'B3':
Bdrivers = Bdrivers & 0b1111111011
if name == 'B4':
Bdrivers = Bdrivers & 0b1111110111
if name == 'B5':
Bdrivers = Bdrivers & 0b1111101111
if name == 'B6':
Bdrivers = Bdrivers & 0b1111011111
if name == 'B7':
Bdrivers = Bdrivers & 0b1110111111
if name == 'B8':
Bdrivers = Bdrivers & 0b1101111111
if name == 'B9':
Bdrivers = Bdrivers & 0b1011111111
if name == 'B10':
Bdrivers = Bdrivers & 0b0111111111
else:
btn.config(relief="sunken")
if name == 'B1':
Bdrivers = Bdrivers | 0b0000000001
if name == 'B2':
Bdrivers = Bdrivers | 0b0000000010
if name == 'B3':
Bdrivers = Bdrivers | 0b0000000100
if name == 'B4':
Bdrivers = Bdrivers | 0b0000001000
if name == 'B5':
Bdrivers = Bdrivers | 0b0000010000
if name == 'B6':
Bdrivers = Bdrivers | 0b0000100000
if name == 'B7':
Bdrivers = Bdrivers | 0b0001000000
if name == 'B8':
Bdrivers = Bdrivers | 0b0010000000
if name == 'B9':
Bdrivers = Bdrivers | 0b0100000000
if name == 'B10':
Bdrivers = Bdrivers | 0b1000000000
self.B = Bdrivers
def exitDebugger():
q.put(['exit',1])
exit()
drivers = Drivers()
root = tkinter.Tk()
A1 = tkinter.Button(text="A1", width=4, relief="raised", command=lambda: drivers.toggleA(A1, 'A1'))
A2 = tkinter.Button(text="A2", width=4, relief="raised", command=lambda: drivers.toggleA(A2, 'A2'))
A3 = tkinter.Button(text="A3", width=4, relief="raised", command=lambda: drivers.toggleA(A3, 'A3'))
A4 = tkinter.Button(text="A4", width=4, relief="raised", command=lambda: drivers.toggleA(A4, 'A4'))
A5 = tkinter.Button(text="A5", width=4, relief="raised", command=lambda: drivers.toggleA(A5, 'A5'))
A6 = tkinter.Button(text="A6", width=4, relief="raised", command=lambda: drivers.toggleA(A6, 'A6'))
A7 = tkinter.Button(text="A7", width=4, relief="raised", command=lambda: drivers.toggleA(A7, 'A7'))
A8 = tkinter.Button(text="A8", width=4, relief="raised", command=lambda: drivers.toggleA(A8, 'A8'))
A9 = tkinter.Button(text="A9", width=4, relief="raised", command=lambda: drivers.toggleA(A9, 'A9'))
A10 = tkinter.Button(text="A10", width=4, relief="raised", command=lambda: drivers.toggleA(A10, 'A10'))
setAbtn = tkinter.Button(text="Set A", width=5, relief="raised", command=drivers.setA)
resetAbtn = tkinter.Button(text="Reset A", width=5, relief="raised", command=drivers.resetA)
B1 = tkinter.Button(text="B1", width=4, relief="raised", command=lambda: drivers.toggleB(B1, 'B1'))
B2 = tkinter.Button(text="B2", width=4, relief="raised", command=lambda: drivers.toggleB(B2, 'B2'))
B3 = tkinter.Button(text="B3", width=4, relief="raised", command=lambda: drivers.toggleB(B3, 'B3'))
B4 = tkinter.Button(text="B4", width=4, relief="raised", command=lambda: drivers.toggleB(B4, 'B4'))
B5 = tkinter.Button(text="B5", width=4, relief="raised", command=lambda: drivers.toggleB(B5, 'B5'))
B6 = tkinter.Button(text="B6", width=4, relief="raised", command=lambda: drivers.toggleB(B6, 'B6'))
B7 = tkinter.Button(text="B7", width=4, relief="raised", command=lambda: drivers.toggleB(B7, 'B7'))
B8 = tkinter.Button(text="B8", width=4, relief="raised", command=lambda: drivers.toggleB(B8, 'B8'))
B9 = tkinter.Button(text="B9", width=4, relief="raised", command=lambda: drivers.toggleB(B9, 'B9'))
B10 = tkinter.Button(text="B10", width=4, relief="raised", command=lambda: drivers.toggleB(B10, 'B10'))
setBbtn = tkinter.Button(text="Set B", width=5, relief="raised", command=drivers.setB)
resetBbtn = tkinter.Button(text="Reset B", width=5, relief="raised", command=drivers.resetB)
A1.grid(row=0,column=0)
A2.grid(row=0,column=1)
A3.grid(row=0,column=2)
A4.grid(row=0,column=3)
A5.grid(row=0,column=4)
A6.grid(row=0,column=5)
A7.grid(row=0,column=6)
A8.grid(row=0,column=7)
A9.grid(row=0,column=8)
A10.grid(row=0,column=9)
setAbtn.grid(row=0,column=10)
resetAbtn.grid(row=0,column=11)
B1.grid(row=1,column=0)
B2.grid(row=1,column=1)
B3.grid(row=1,column=2)
B4.grid(row=1,column=3)
B5.grid(row=1,column=4)
B6.grid(row=1,column=5)
B7.grid(row=1,column=6)
B8.grid(row=1,column=7)
B9.grid(row=1,column=8)
B10.grid(row=1,column=9)
setBbtn.grid(row=1,column=10)
resetBbtn.grid(row=1,column=11)
exitButton = tkinter.Button(text="Exit", command=exitDebugger)
exitButton.grid(row=5)
root.mainloop()
def uavcan_worker(q):
"""uavcan worker function"""
node_info = uavcan.protocol.GetNodeInfo.Response()
node_info.name = 'ch.octanis.debugger'
node_info.software_version.major = 1
node_info.hardware_version.unique_id = b'11100' # Setting first 5 bytes; rest will be kept zero
with closing(uavcan.make_node('/dev/ttyACM3',bitrate=100000,node_id=127,node_info=node_info)) as node:
#handle = node.add_handler(uavcan.protocol.NodeStatus, node_status_callback)
print('opening can adapter')
Aid = 2
Bid = 3
while True:
try:
node.spin(0.2)
m = q.get()
if m[0] == 'exit':
exit()
if m[0] == 'A':
set_mosfet_driver(node,m[1],Aid)
if m[0] == 'B':
set_mosfet_driver(node,m[1],Bid)
node.spin(0.2)
except KeyboardInterrupt:
print('Node error')
break
return
if __name__ == '__main__':
q = Queue()
p1 = Process(target=uavcan_worker, args=(q,))
p2 = Process(target=tkinter_worker, args=(q,))
p1.start()
p2.start()
p1.join()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment