Introduction
This week I have learned how to write interface programs on computer. Basically, I need to write a program to interface with my device, which includes receiving data from my devices and displaying them on screen, and controlling the devices in the program.
Interfacing with Jotsticks
I will staart with a very simple case, where I will control the position of a ball by joysticks.
Here is the code for that function:
import serial
import sys
from Tkinter import *
x_pos = 512
y_pos = 512
x_change=0
y_change=0
r = 50 #size of the circle
def idle(parent,canvas):
global x_pos, y_pos
byte2 = 0
byte3 = 0
byte4 = 0
while 1:
byte1 = byte2
byte2 = byte3
byte3 = byte4
byte4 = ord(ser.read())
if ((byte1 == 1) & (byte2 == 2) & (byte3 == 3) & (byte4 == 4)):
break
x0 = ord(ser.read())
x1 = ord(ser.read())
y0 = ord(ser.read())
y1 = ord(ser.read())
x_change = (x0+255*x1-512)/200. - x_pos
y_change = (y0+255*y1-512)/200. - y_pos
x_pos = (x0+255*x1-512)/200.
y_pos = (y0+255*y1-512)/200.
#print(x_pos,y_pos)
#canvas.delete("path")
canvas.move(ball, x_change, y_change)
parent.after_idle(idle,parent,canvas)
if (len(sys.argv) != 2):
print("command line: monitor.py serial_port")
sys.exit()
port = sys.argv[1]
ser = serial.Serial(port,9600)
root = Tk()
root.title("Joystick.py")
root.bind('q','exit')
canvas = Canvas(root, width=1000, height=1000, background='white')
canvas.pack()
ball = canvas.create_oval(x_pos, y_pos, x_pos-r, y_pos-r, fill="blue")
root.after(50,idle,root,canvas)
root.mainloop()
Accelerometer and gyro
In this section, I wil make use of Axes3D in matplotlib, together with tkinter, to draw a rectangular. Then I will use the data from my LSM6DS33 to control the motion and position of the box.