My setup: An Arduino that reads numbers from serial(steps), then it moves the stepper motor that many steps. It uses a4988 driver, a 12 volt power supply, 1.5 amp Nema 17. All of that is fine, my issue is on the Python side.
In Python, I have tried to create a function in a variety of ways.
I used tkinter to get the mouse position on the screen, it moves in the x axis rotating the stepper. The stepper has a flashlight just to shine it at anything I point at with my mouse.
Say there is a camera below the stepper, if I move my mouse over to an object it would move there, within a tolerance of 5 steps either way. +5, -5
The function I have tried to create should work like this.
while True:
#I change direction by writing 400, clockwise, or 401, anti clockwise to serial.
step(10)#move only 10 steps
step(10)#it should do nothing as it has already moved 10 steps.
step(15)#it should move 5 steps as it has moved 10 already
step(5)#change direction and move back 10 steps
I am somewhat decent at python(or so I think)
here is my most promising code so far:
#I have direction changes done by writing a 400 for clockwise or 401 for anti clockwise to serial.
import tkinter as tk
import serial
ser = serial.Serial("COM8", 9600)
root = tk.Tk()
wi = root.winfo_screenwidth() / 2
width = root.winfo_screenwidth()
hi = root.winfo_screenheight() / 2
height = root.winfo_screenheight()
sere = '\r\n'
sender = sere.encode('utf_8')
pps = height / 200
# p.p.s pixels per step divide that by the same thing to get correct.
#how many pixels are for 1 step
print(pps)
#number of pixels difference divided by pps to get steps, then round for ease of use
past = 0
print(height, width, wi) # height divided by pps should always = max steps in my case 200
def send(steps):
global past
global current
sere = '\r\n'
current = steps
neg = past - 5
pos = past + 5
if current != past:
if current > pos or current < neg:
if steps > 1:
sender = sere.encode('utf_8')
e = str(steps).encode('utf_8')
ser.write(e + sender)
past = steps
while True:
pos = root.winfo_pointerx() - wi
steps = round(pos / pps)
print(pos, steps) # shows cursor position
#direction code is not in here as I have no need for it atm.For one simple reason, IT DOESN'T WORK!
Please explain answers if possible, I am a long time lurker, and only made a account just now for this.Thank you all for any assistance.
Edit:
The question is how to make a function that moves a stepper motor to a certain number of steps.If you command it to do 10 steps twice, it only moves 10 steps.
step(10) stepper moves 10 steps, do it again the motor does nothing as it is already at 10 steps.
If I do step(15) it would only move another 5 as it is at 10 steps.
Edit2 Clarification:
By function I mean
def step(steps):
#logic that makes it work
For anyone else who has this problem and people seem to not help a ton, heres my new code to drive it,
past = 0
sender = sere.encode('utf_8')
dir1 = str(400).encode('utf_8')
dir2 = str(401).encode('utf_8')
def send(steps):
global past
global current
global sender
global dir1
global dir2
global stepstaken
sere = '\r\n'
current = steps
neg = past - 5 # tolerance both are unused atm
pos = past + 5 # tolerance
needed = current - past
print(needed, steps)
if current != past:
if needed > 0:
serialsteps = str(needed).encode('utf_8')
ser.write(dir1 + sender)
ser.write(serialsteps + sender)
if needed < 0:
needed = needed * -1
serialstepss = str(needed).encode('utf_8')
ser.write(dir2 + sender)
ser.write(serialstepss + sender)
past = steps
Here is my full code for mouse to stepper motor,
import tkinter as tk
root = tk.Tk()
import serial
import struct
import time
stepstaken = 0
ardunioport = "COM" + str(input("Ardunio Port Number:")) # port number only
ser = serial.Serial(ardunioport, 9600)
wi = root.winfo_screenwidth() / 2
width = root.winfo_screenwidth()
hi = root.winfo_screenheight() / 2
height = root.winfo_screenheight()
sere = '\r\n' #still don't understand why but it does not work without this.
sender = sere.encode('utf_8')
pps = width / 200 #how many pixels are in 1 step
past = 0
sender = sere.encode('utf_8')
dir1 = str(400).encode('utf_8')
dir2 = str(401).encode('utf_8')
def send(steps):
global past
global current
global sender
global dir1
global dir2
global stepstaken
#need overcome overstepping
sere = '\r\n'
current = steps
neg = past - 5 # tolerance
pos = past + 5 # tolerance
needed = current - past
print(needed, steps)
if current != past:
if needed > 0:
serialsteps = str(needed).encode('utf_8')
ser.write(dir1 + sender)
ser.write(serialsteps + sender)
if needed < 0:
needed = needed * -1
serialstepss = str(needed).encode('utf_8')
ser.write(dir2 + sender)
ser.write(serialstepss + sender)
past = steps
while True:
pos = root.winfo_pointerx() - wi
steps = round(pos / pps)
#print("Mouse Position " + str(pos) + " Steps Needed" + str(steps)) # shows cursor position
send(steps)
For me it goes to a Arduino, the Arduino is not programmed to accept negative numbers. Instead it removes the - sign, multiply negative by negative = positive. It sends the opposite direction code/number then the steps needed to get there. Good luck everyone. This code works for me, no promise it will work for you.If it does not I am sorry.
Related
A while ago I started getting into sim racing, but because I can't afford a wheel myself, I searched for ways I could emulate it, so I stumbled upon an application that lets me write scripts in python with certain libraries: https://andersmalmgren.github.io/FreePIE/.
The thing is, this script that I have tends to lag a lot, like, when I hold X (key to alternate between full-braking and braking just a quarter) the more I hold it, the more it delays after I release the key, this also happens when I use the clutch key (C), and the "Smooth Throttle" key (Z), I think this has to do with the way I programmed it (spaghetti pretty much, as I don't know a lot of python) so I thought I could get help from this forum, also It's my first time and this is my fresh new account, so I could be doing something wrong here, but anyways, here's the code:
if starting:
system.setThreadTiming(TimingTypes.HighresSystemTimer)
system.threadExecutionInterval = 5
def set_button(button, key):
if keyboard.getKeyDown(key):
v.setButton(button, True)
else:
v.setButton(button, False)
def calculate_rate(max, time):
if time > 0:
return max / (time / system.threadExecutionInterval)
else:
return max
int32_max = (2 ** 14) - 1
activate = True
int32_min = (( 2** 14) * -1) + 1
v = vJoy[0]
v.x, v.y, v.z, v.rx, v.ry, v.rz, v.slider, v.dial = (int32_min,) * 8
# =============================================================================================
# Axis inversion settings (multiplier): normal = 1; inverted = -1
# =============================================================================================
global throttle_inversion, braking_inversion, clutch_inversion
throttle_inversion = 1
braking_inversion = 1
clutch_inversion = 1
# =============================================================================================
# Mouse settings
# =============================================================================================
global mouse_sensitivity, sensitivity_center_reduction
mouse_sensitivity = 4.0
sensitivity_center_reduction = 1.0
# =============================================================================================
# Ignition cut settings
# =============================================================================================
global ignition_cut_time, ignition_cut_elapsed_time
ignition_cut_enabled = True
ignition_cut_time = 100
ignition_cut_elapsed_time = 0
global ignition_cut, ignition_cut_released
# Init values, do not change
ignition_cut = False
ignition_cut_released = True
# =============================================================================================
# Steering settings
# =============================================================================================
global steering, steering_max, steering_min, steering_center_reduction
# Init values, do not change
steering = 0.0
steering_max = float(int32_max)
steering_min = float(int32_min)
steering_center_reduction = 1.0
# =============================================================================================
# Throttle settings
# =============================================================================================
global throttle_blip_enabled
throttle_blip_enabled = False
# In milliseconds
throttle_increase_time = 300
throttle_increase_time_after_ignition_cut = 0
throttle_increase_time_blip = 50
throttle_decrease_time = 300
global throttle, throttle_max, throttle_min
# Init values, do not change
throttle_max = int32_max * throttle_inversion
throttle_min = int32_min * throttle_inversion
throttle = throttle_min
global throttle_increase_rate, throttle_decrease_rate
# Set throttle behaviour with the increase and decrease time,
# the actual increase and decrease rates are calculated automatically
throttle_increase_rate = calculate_rate(throttle_max, throttle_increase_time)
throttle_increase_rate_after_ignition_cut = calculate_rate(throttle_max, throttle_increase_time_after_ignition_cut)
throttle_increase_rate_blip = calculate_rate(throttle_max, throttle_increase_time_blip)
throttle_decrease_rate = calculate_rate(throttle_max, throttle_decrease_time) * -1
# =============================================================================================
# Braking settings
# =============================================================================================
# In milliseconds
braking_increase_time = 160
braking_decrease_time = 130
global braking, braking_max, braking_min
# Init values, do not change
braking_max = int32_max * braking_inversion
braking_min = int32_min * braking_inversion
braking = braking_min
global braking_increase_rate, braking_decrease_rate
# Set braking behaviour with the increase and decrease time,
# the actual increase and decrease rates are calculated automatically
braking_increase_rate = calculate_rate(braking_max, braking_increase_time)
braking_decrease_rate = calculate_rate(braking_max, braking_decrease_time) * -1
# =============================================================================================
# Clutch settings
# =============================================================================================
# In milliseconds
clutch_increase_time = 0
clutch_decrease_time = 50
global clutch, clutch_max, clutch_min
# Init values, do not change
clutch_max = int32_max * clutch_inversion
clutch_min = int32_min * clutch_inversion
clutch = clutch_min
global clutch_increase_rate, clutch_decrease_rate
# Set clutch behaviour with the increase and decrease time,
# the actual increase and decrease rates are calculated automatically
clutch_increase_rate = calculate_rate(clutch_max, clutch_increase_time)
clutch_decrease_rate = calculate_rate(clutch_max, clutch_decrease_time) * -1
# assign button
# =================================================================================================
# LOOP START
# =================================================================================================
# =================================================================================================
# Activate
# =================================================================================================
if keyboard.getPressed(Key.End):
activate = not activate
if activate == True:
vJoy[0].setButton(0,int(keyboard.getKeyDown(Key.S)))
vJoy[0].setButton(1,int(keyboard.getKeyDown(Key.D)))
vJoy[0].setButton(2,int(keyboard.getKeyDown(Key.C)))
vJoy[0].setButton(3,int(mouse.wheelUp))
vJoy[0].setButton(4,int(keyboard.getKeyDown(Key.Home)))
vJoy[0].setButton(5,int(keyboard.getKeyDown(Key.G)))
vJoy[0].setButton(6,int(keyboard.getKeyDown(Key.H)))
vJoy[0].setButton(7,int(mouse.wheelDown))
else:
vJoy[0].setButton(0,int(keyboard.getKeyDown(Key.Home)))
vJoy[0].setButton(1,int(keyboard.getKeyDown(Key.Home)))
vJoy[0].setButton(2,int(keyboard.getKeyDown(Key.Home)))
vJoy[0].setButton(3,int(keyboard.getKeyDown(Key.Home)))
vJoy[0].setButton(4,int(keyboard.getKeyDown(Key.Home)))
vJoy[0].setButton(5,int(keyboard.getKeyDown(Key.Home)))
vJoy[0].setButton(6,int(keyboard.getKeyDown(Key.Home)))
vJoy[0].setButton(7,int(keyboard.getKeyDown(Key.Home)))
# =================================================================================================
# =================================================================================================
# Steering logic
# =================================================================================================
if steering > 0:
steering_center_reduction = sensitivity_center_reduction ** (1 - (steering / steering_max))
elif steering < 0:
steering_center_reduction = sensitivity_center_reduction ** (1 - (steering / steering_min))
steering = steering + ((float(mouse.deltaX) * mouse_sensitivity) / steering_center_reduction)
if steering > steering_max:
steering = steering_max
elif steering < steering_min:
steering = steering_min
v.x = int(round(steering))
# =================================================================================================
# Clutch logic
# =================================================================================================
if keyboard.getPressed(Key.C):
throttle = -16383
if keyboard.getKeyDown(Key.C):
clutch = clutch + clutch_increase_rate
else:
clutch = clutch + clutch_decrease_rate
if clutch > clutch_max * clutch_inversion:
clutch = clutch_max * clutch_inversion
elif clutch < clutch_min * clutch_inversion:
clutch = clutch_min * clutch_inversion
v.z = clutch
# =================================================================================================
# Smooth Throttle
# =================================================================================================
if keyboard.getKeyDown(Key.Z):
blip = 1
elif keyboard.getKeyUp(Key.Z):
blip = 200
# =================================================================================================
# Brake y Full Brake
# =================================================================================================
if keyboard.getKeyDown(Key.X):
maxBrake = 16383
elif keyboard.getKeyUp(Key.X):
maxBrake = 2048
elif keyboard.getKeyDown(Key.J):
maxBrake = 1
# =================================================================================================
# Throttle logic
# =================================================================================================
if mouse.leftButton:
throttle = throttle + throttle_increase_rate * blip
else:
throttle = throttle + throttle_decrease_rate * blip
if throttle > throttle_max * throttle_inversion:
throttle = throttle_max * throttle_inversion
elif throttle < throttle_min * throttle_inversion:
throttle = throttle_min * throttle_inversion
v.y = throttle
# =================================================================================================
# Braking logic
# =================================================================================================
if mouse.rightButton:
braking = braking + (braking_increase_rate*6)
else:
if braking > 4096:
braking = braking + (braking_decrease_rate * 4)
else:
braking = braking + (braking_decrease_rate * 4)
if braking > maxBrake:
braking = maxBrake
elif braking < braking_min * braking_inversion:
braking = braking_min * braking_inversion
v.rz = braking
# =================================================================================================
# Buttons post-throttle logic
# =================================================================================================
#set_button(look_left_button, look_left_key)
#set_button(look_right_button, look_right_key)
#set_button(look_back_button, look_back_key)
#set_button(change_view_button, change_view_key)
#set_button(indicator_left_button, indicator_left_key)
#set_button(indicator_right_button, indicator_right_key)
# =================================================================================================
# PIE diagnostics logic
# =================================================================================================
diagnostics.watch(v.x)
diagnostics.watch(v.y)
diagnostics.watch(v.rz)
diagnostics.watch(maxBrake)
diagnostics.watch(blip)
diagnostics.watch(activate)
to test the code you simply copy this into a .py file and run it with freePIE, it has a watch tool that is very useful too
I tried to use and modify an already found script for emulating a wheel in Assetto Corsa through freePIE, since the beginning and even before I modified it, It began to lag a lot, like it's buffering a lot of inputs innecesarily.
I'm having a few problems.
First, given wind speeds seem to be low, especially compared to other weather stations reporting close by (apparently there are lots of enthusiasts in my area). Just watching the trees, flag, bushes, small animals flying across my yard, I can tell 1.6mph is a wee bit low. Everything tested fine inside, and when I run the test script outside its picking up the signal as it spins.
the 2nd problem is it always reports the "mean" and "max" speeds as exactly the same. I've tried to adjust the intervals, but no matter what length of time I put in, they always report as the same numbers.
from gpiozero import Button
import requests
import time
import math
import statistics
import database
wind_count = 0
radius_cm = 9.0
wind_interval = 5
ADJUSTMENT = 1.18
interval = 60
gust = 0
def spin():
global wind_count
wind_count = wind_count + 1
def calculate_speed(time_sec):
global wind_count
global gust
circumference_cm = (2 * math.pi) * radius_cm
rotations = wind_count / 2.0
dist_cm = circumference_cm * rotations
dist_km = (circumference_cm * rotations) / 100000
dist_mi = dist_km * 0.621371
mi_per_sec = dist_mi / time_sec
mi_per_hour = mi_per_sec * 3600
return mi_per_hour * ADJUSTMENT
def reset_wind():
global wind_count
wind_count = 0
def reset_gust():
global gust
gust = 0
wind_speed_sensor = Button(16)
wind_speed_sensor.when_activated = spin
while True:
print("Starting Weather Sensor Read Loop...")
start_time = time.time()
while time.time() - start_time <= interval:
print("Start timed loop...")
wind_start_time = time.time()
reset_wind()
reset_gust()
store_speeds = []
time.sleep(wind_interval)
final_speed = calculate_speed(wind_interval)
store_speeds.append(final_speed)
wind_gust_speed = (max(store_speeds))
wind_speed = (statistics.mean(store_speeds))
print(wind_average, wind_speed)
When I comment out "store_speeds = [ ]" the first loop the speeds are reported the same, every loop after I get a "max" reading thats "different" than the mean. This still troubles me, because why on the first loop are they the same? Am I wrong for thinking with the wind_interval set at 5, and the interval set to 60, that its taking 5 second samples over a 60 second period, giving me 12 sample to get the mean and max from?
My goal is every time it reports, I get a mean and max for that "loop" if possible, and not a mean/max over the time the script stays running before its interrupted/stopped.
Here is the working and corrected code:
from gpiozero import Button
import time
import math
import statistics
wind_count = 0
radius_cm = 9.0
wind_interval = 5
interval = 30
CM_IN_A_KM = 100000.0
SECS_IN_AN_HOUR = 3600
ADJUSTMENT = 1.18
store_speeds = []
def reset_wind():
global wind_count
wind_count = 0
def spin():
global wind_count
wind_count = wind_count + 1
def calculate_speed(time_sec):
global wind_count
circumference_cm = (2 * math.pi) * radius_cm
rotations = wind_count / 2.0
dist_km = (circumference_cm * rotations) / CM_IN_A_KM
km_per_sec = dist_km / time_sec
km_per_hour = km_per_sec * SECS_IN_AN_HOUR
mi_per_hour = km_per_hour * 0.6214
return mi_per_hour * ADJUSTMENT
wind_speed_sensor = Button(16)
wind_speed_sensor.when_pressed = spin
while True:
store_speeds = []
for _ in range (interval//wind_interval):
reset_wind()
#reset_gust()
time.sleep(wind_interval) # counter is spinning in background
final_speed = calculate_speed(wind_interval)
store_speeds.append(final_speed)
wind_gust = max(store_speeds)
wind_speed = statistics.mean(store_speeds)
print(wind_speed, wind_gust)
I am collecting over 100k FastFrame images (100 frames, with 15k points each), with summary mode and collecting them via python pyvisa using ni-visa.
The error is as follows:
SEVERE
The system is low on memory. Some results may be incomplete. To remedy, reduce record length or remove one or more analytical features such as math, measurements, bus decode or search.
After that, I can disconnect, connect again, send commands which update the window, but cannot query anything.
I suspect it is something to do with a memory leak on MSO56 RAM, or communication queue.
Commands like *RST, CLEAR, LCS, and FACTORY do not fix the error.
import pyvisa
import time
if __name__ == '__main__':
## DEV Signal
rm = pyvisa.ResourceManager()
ll = rm.list_resources()
print('\n\n\n----------------------\nAvailable Resources\n----------------------')
for i in range(len(ll)):
print(F'Resource ID {i}: {ll[i]}')
#i = int(input(F"\n\nPlease select 'Resource ID' from above: "))
i=0;
inst = rm.open_resource(ll[i])
inst.timeout = 10000
reset = inst.write("*RST")
ind = inst.query("*IDN?")
print(F"\nResource {i}: {ind}")
inst.write('C1:OUTP ON')
inst.write('C2:OUTP ON')
# Wave signal
Ch = 1; # channel 1 || 2
wave_name = 'UT1'
Frq = 500000; #Hz
Peri = 1/Frq;# Length of waveform
print(F"Period: {Peri}")
# trigger on channel 2
inst.write(F'C2:BSWV WVTP,SQUARE,FRQ,{Frq},AMP,1,OFST,0,DUTY,1')
# signal on channel 1
inst.write(F'C1:BSWV WVTP,SQUARE,FRQ,{Frq},AMP,1,OFST,0,DUTY,10')
inst = []
scope_ip = '192.168.0.10';
rm = pyvisa.ResourceManager()
ll = rm.list_resources()
print(ll)
for l in ll:
if scope_ip in l:
vScope = rm.open_resource(l)
#vScope.clear()
#vScope.close()
vScope.timeout = 2000
## attempts to fix Memory Error
vScope.write_raw('FPANEL:PRESS RESTART')
vScope.write_raw('*PSC ON')
vScope.write_raw('*CLS')
vScope.write_raw('FACTORY\n')
vScope.write_raw('*RST')
vScope.write_raw('*CLEAR')
vScope.write_raw('*ESE 1')
vScope.write_raw('*SRE 0')
vScope.write_raw('DESE 1')
print('\nESR')
print(vScope.query('*ESR?'))
#print('\nEVMSG?')
#print(vScope.query('*EVMsg?'))
#print(vScope.query('*ESE ?'))
# Display Wave Forms
vScope.write_raw('DISPLAY:WAVEVIEW1:CH1:STATE 1')
vScope.write_raw('DISPLAY:WAVEVIEW1:CH2:STATE 1')
# Vertical Command Groups.
vScope.write_raw('CH1:Coupling DC')
vScope.write_raw('CH2:Coupling DC')
vScope.write_raw('CH1:SCALE .5') # *10 for the range
vScope.write_raw('CH2:SCALE .5')
vScope.write_raw('CH1:Position 0')
vScope.write_raw('CH2:Position 0')
vScope.write_raw('TRIGGER:A:TYPE EDGE')
vScope.write_raw('TRIGGER:A:EDGE:SOURCE CH2')
vScope.write_raw('TRIGger:A:LEVEL:CH2 0')
vScope.write_raw('TRIGger:A:EDGE:SLOpe RISE')
vScope.write_raw('Horizontal:Position 10')
vScope.write_raw('Horizontal:MODE MANUAL')
vScope.write_raw('Horizontal:Samplerate 25000000000')
vScope.write_raw('HORIZONTAL:MODE:RECORDLENGTH 25000')
vScope.write_raw('DATA:SOURCE CH1')
vScope.write_raw('ACQUIRE:STOPAFTER SEQUENCE')## triggers re-read
nframes = 100;
vScope.write_raw(F"HORIZONTAL:FASTFRAME:COUNT {nframes}")
if int(1):
vScope.write_raw(F"DATA:FRAMESTART {1+nframes}")
else:
vScope.write_raw('DATA:FRAMESTART 1')
vScope.write_raw(F"DATA:FRAMESTOP {1+nframes}")
vScope.write_raw('HORIZONTAL:FASTFRAME:STATE OFF')
vScope.write_raw('HORIZONTAL:FASTFRAME:STATE ON')
vScope.write_raw('HORIZONTAL:FASTFRAME:SUMFRAME:STATE ON')
vScope.write_raw(F"HORIZONTAL:FASTFRAME:SELECTED {1+nframes}")
t0 = time.time()
for i in range(1000000):
vScope.write_raw('ACQUIRE:STATE 1') ## triggers re-read
vScope.query('*opc?')
vScope.write_raw(b'WFMOUTPRE?')
wfmo = vScope.read_raw()
vScope.write_raw('CURVE?')
curve = vScope.read_raw()
if i%100 ==0:
print(F"Iteration {i}")
print(F"Time Delta: {time.time()-t0}")
t0=time.time()
Poor Solution.
Restarting the Scope with the power button works.
I should have added that in the question, but takes ~ 2 minutes and is not an elegant solution.
I am running the following code on my Raspberry Pi 4B, using Python 3.7.3:
from time import sleep
import RPi.GPIO as GPIO
import math
from watchgod import watch
g=open("/home/pi/Desktop/Int2/DesHeight.txt","r")
DesHeight = g.readline()
DesHeight1=float(DesHeight)
print(DesHeight1)
GPIO.cleanup()
DIR = 20
STEP = 21
CW = 0
CCW = 1
TX_ENC = 15
SPR = 200 # Steps per Rev [CONSTANT]
delay = .001 #Seconds per stepper pulse [CONSTANT]
ratio=24 #gear ratio [CONSTANT]
f = open("height.txt", "r")
y0 = f.readline()
y0 = float(y0)
d=1.25
r=d/2
theta0=y0/(2*math.pi*r)
GPIO.setmode(GPIO.BCM) # GPIO numbering
GPIO.setup(12,GPIO.OUT)
GPIO.output(12,1) # Turning on the "Enable Input"
GPIO.setup(DIR, GPIO.OUT)
GPIO.setup(STEP, GPIO.OUT)
GPIO.output(DIR, CW) # Setting CW Direction
GPIO.setup(TX_ENC, GPIO.IN) # Encoder input setup
GPIO.add_event_detect(TX_ENC, GPIO.BOTH)
Tx = 0
MODE = (16,17,18,19) # GPIO 16 is the standby input. It needs to be high for anything to move
GPIO.setup(MODE,GPIO.OUT)
RESOLUTION = {'Standby':(0,0,0,0),
'Full':(1,0,0,0),
'Half':(1,1,0,0),
'1/4':(1,0,1,0),
'1/8':(1,1,1,0),
'1/16':(1,0,0,1),
'1/32':(1,1,0,1),
'1/64':(1,0,1,1),
'1/128':(1,1,1,1)}
GPIO.output(MODE,RESOLUTION['Full'])
ass = (0,0,0,0)
pp = list(ass)
pp[0] = GPIO.input(MODE[0])
pp[1] = GPIO.input(MODE[1])
pp[2] = GPIO.input(MODE[2])
pp[3] = GPIO.input(MODE[3])
ass = tuple(pp)
if(ass == RESOLUTION['Standby']):
res = 0
elif(ass == RESOLUTION['Full']):
res = 200
elif(ass == RESOLUTION['Half']):
res = 400
elif(ass == RESOLUTION['1/4']):
res = 800
elif(ass == RESOLUTION['1/8']):
res = 1600
elif(ass == RESOLUTION['1/16']):
res = 3200
elif(ass == RESOLUTION['1/32']):
res = 6400
elif(ass == RESOLUTION['1/64']):
res=12800
elif(ass == RESOLUTION['1/128']):
res=25600
else:
print("Whoops lol")
while(True):
for changes in watch('/home/pi/Desktop/Int2/DesHeight.txt'):
g=open("/home/pi/Desktop/Int2/DesHeight.txt","r")
DesHeight = g.readline()
DesHeight1=float(DesHeight)
f = open("height.txt", "r")
y0 = f.readline()
y0=float(y0)
while(abs(y0-DesHeight1)>.001):
if(y0 < DesHeight1):
while(y0 < DesHeight1):
GPIO.output(DIR,CCW)
GPIO.output(STEP, GPIO.HIGH)
sleep(delay)
GPIO.output(STEP, GPIO.LOW)
sleep(delay)
Tx = Tx + 1
theta0=theta0+1/res*1/ratio#*1/gearratio
y0 = y0+2*5/4*14/15*.9944*math.pi*(1/res*1/ratio)*r
else:
while(y0 > DesHeight1):
if(y0>0):
GPIO.output(DIR,CW)
GPIO.output(STEP, GPIO.HIGH)
sleep(delay)
GPIO.output(STEP, GPIO.LOW)
sleep(delay)
Tx = Tx - 1
theta0=theta0-1/res*1/ratio#*1/gearratio
y0 = y0-2*5/4*14/15*.9944*math.pi*(1/res*1/ratio)*r
y0 = str(y0)
print(y0)
f.close()
f = open('height.txt', 'w')
f.write(y0)
f.close()
Essentially, what I am trying to do is read the height of a machine from a text file, then compare it with the desired height, as written in a separate text file. When the code detects a change in the desired height, it checks to make sure that the actual height and the desired height are within 1/1000 of an inch of each other, and if not, it moves a NEMA-17 motor until this condition is met.
The problem I am encountering is that if this code is left to run for a little bit (usually around 40 seconds) the stepper motor ceases to run when I change the desired height. The code itself runs, taking as long as expected to "move" the motor and also calculating the height and returning to the top of the while loop, but the motor itself remains stagnant. This does not occur if new changes to the desired height file are implemented immediately. I am at a loss as to what this could be and could use some help.
Okay, so following the advice of RootTwo, and learning how to use an oscilloscope, I was able to isolate part of the problem and determine a solution. Either the pi or the driver quits supplying a voltage to the motor after about 90 seconds of inactivity. I was not able to find a way to continuously keep the motor at holding torque, but I was able to make it so that the code will reinitialize after a long break by moving the while(True): and watchdog command to the beginning of the code. My problem is technically solved, but my question is yet unanswered: Why does my pi stop giving a signal to the driver board?
There is my code. I fixed it like this:
# Take 3 digits for significant figures in this code
import numpy as np
from math import *
from astropy.constants import *
import matplotlib.pyplot as plt
import time
start_time = time.time()
"""
G = Gravitational constant
g0 = Standard acceleration of gravity ( 9.8 m/s2)
M_sun = Solar mass
M_earth = Earth mass
R_sun = Solar darius
R_earth = Earth equatorial radius
au = Astronomical unit
Astropy.constants doesn't have any parameter of moon.
So I bring the data from wikipedia(https://en.wikipedia.org/wiki/Moon)
"""
M_moon = 7.342E22
R_moon = 1.737E6
M_earth = M_earth.value
R_earth = R_earth.value
G = G.value
perigee, apogee = 3.626E8, 4.054E8
position_E = np.array([0,0])
position_M = np.array([(perigee+apogee)/2.,0])
position_com = (M_earth*position_E+M_moon*position_M)/(M_earth+M_moon)
rel_pE = position_E - position_com
rel_pM = position_M - position_com
F = G*M_moon*M_earth/(position_M[0]**2)
p_E = {"x":rel_pE[0], "y":rel_pE[1],"v_x":0, "v_y":(float(F*rel_pE[0])/M_earth)**.5}
p_M = {"x":rel_pM[0], "y":rel_pM[1],"v_x":0, "v_y":(float(F*rel_pM[0])/M_moon)**.5}
print(p_E, p_M)
t = range(0,365)
data_E , data_M = [], []
def s(initial_velocity, acceleration, time):
result = initial_velocity*time + 0.5*acceleration*time**2
return result
def v(initial_velocity, acceleration, time):
result = initial_velocity + acceleration*time
return result
dist = float(sqrt((p_E["x"]-p_M['x'])**2 + (p_E["y"]-p_M["y"])**2))
xE=[]
yE=[]
xM=[]
yM=[]
data_E, data_M = [None]*len(t), [None]*len(t)
for i in range(1,366):
data_E[i-1] = p_E
data_M[i-1] = p_M
dist = ((p_E["x"]-p_M["x"])**2 + (p_E["y"]-p_M["y"])**2)**0.5
Fg = G*M_moon*M_earth/(dist**2)
theta_E = np.arctan(p_E["y"]/p_E["x"])
theta_M = theta_E + np.pi #np.arctan(data_M[i-1]["y"]/data_M[i-1]["x"])
Fx_E = Fg*np.cos(theta_E)
Fy_E = Fg*np.sin(theta_E)
Fx_M = Fg*np.cos(theta_M)
Fy_M = Fg*np.sin(theta_M)
a_E = Fg/M_earth
a_M = Fg/M_moon
v_E = (p_E["v_x"]**2+p_E["v_y"]**2)**.5
v_M = (p_M["v_x"]**2+p_M["v_y"]**2)**.5
p_E["v_x"] = v(p_E["v_x"], Fx_E/M_earth, 24*3600)
p_E["v_y"] = v(p_E["v_y"], Fy_E/M_earth, 24*3600)
p_E["x"] += s(p_E['v_x'], Fx_E/M_earth, 24*3600)
p_E["y"] += s(p_E['v_y'], Fy_E/M_earth, 24*3600)
p_M["v_x"] = v(p_M["v_x"], Fx_M/M_moon, 24*3600)
p_M["v_y"] = v(p_M["v_y"], Fy_M/M_moon, 24*3600)
p_M["x"] += s(p_M['v_x'], Fx_M/M_moon, 24*3600)
p_M["y"] += s(p_M['v_y'], Fy_M/M_moon, 24*3600)
for i in range(0,len(t)):
xE += data_E[i]["x"]
yE += data_E[i]["y"]
xM += data_M[i]["x"]
yM += data_M[i]["y"]
print("\n Run time \n --- %d seconds ---" %(time.time()-start_time))
after run this code i tried to print data_E and data_M.
Then I can get data but there is no difference. All of the data is the same.
But when I printed data step by step, it totally different.
I have wrong data problem and increase distance problem. Please help me this problem..
The code exits near line 45, where you are trying to assign p_E by pulling the square root of a negative number on the right hand side (as you've moved the [0] coordinate of the Earth to negative values while shifting Earth and Moon into the coordinate system of their center of mass). In line 45, the value of F*rel_pE[0]/M_earth is negative. So the code never reaches the end of the program using python 2.7.14. That bug needs to be solved before trying to discuss any further aspects.