In [8]:
# Function to calculate the Bits needed fo a given number
def unsigned_num_bits(num):
_nbits = 1
_n = num
while(_n > 1):
_nbits = _nbits + 1
_n = _n / 2
return _nbits
In [9]:
rev_distance = 0.5 # mm
step_angle = 1.8 # °
# Calculation one Step
step_distance = rev_distance/360*step_angle
print("Step Distance = {} mm".format(step_distance))
print("Step Distance = {} um".format(step_distance*1000))
# Calculation max and min register position
RegBitNb = 32
regval_max = 2**(RegBitNb-1)-1
regval_min = -2**(RegBitNb-1)
step_distance_max = regval_max*step_distance
step_distance_min = regval_min*step_distance
print("Register Position Values = {} ... {}".format(regval_max, regval_min))
print("Position Register distances = {} m ... {} m".format(step_distance_max/1000, step_distance_min/1000))
In [10]:
speed_max = 60# rev/min
step_angle = 1.8 # °
steps_per_rev = 360/step_angle
speed_max_sec = speed_max/60 # rev/sec
f_max = speed_max_sec * steps_per_rev
print("Max Frequency of Steppermotor is {} Hz".format(f_max))
In [11]:
speed_resolution = 2**8 # different speed values
clk_freq = 100e6 # Hz
speed_max = 120*1/60 # rev/min * min/s = rev/s
steps_per_rev = 200 # steps per revolution
g_max_speed = ((speed_resolution-1)*clk_freq)/(speed_max*steps_per_rev)
print("g_MAX_SPEED = {} needs {} Bits".format(int(g_max_speed), unsigned_num_bits(int(g_max_speed))))
$g\_MAX\_ACCELERATION = \frac{speed_{max}*clk_{freq}}{(speed_{resolution}-1)*acceleration\_speed} = \frac{[\frac{rev}{s}]*[Hz]}{([values]-1)*[\frac{rev}{s^{2}}]}$
$g\_MAX\_DECCELERATION = \frac{speed_{max}*clk_{freq}}{(speed_{resolution}-1)*decceleration\_speed} = \frac{[\frac{rev}{s}]*[Hz]}{([values]-1)*[\frac{rev}{s^{2}}]}$
In [12]:
speed_resolution = 2**8 # different speed values
clk_freq = 100e6 # Hz
speed_max = 60*1/60 # rev/min * min/s = rev/s
max_acceleration_time = 2.0 # seconds from 0 to max speed
max_acceleration_rev = speed_max/max_acceleration_time # rev/s^2
max_decceleration_time = 1.0 # seconds from max to 0 speed
max_decceleration_rev = speed_max/max_decceleration_time # rev/s^2
g_max_acceleration = (speed_max*clk_freq)/((speed_resolution-1)*max_acceleration_rev)
g_max_decceleration = (speed_max*clk_freq)/((speed_resolution-1)*max_decceleration_rev)
print("g_MAX_ACCELERATION = {} needs {} Bits".format(int(g_max_acceleration),unsigned_num_bits(int(g_max_acceleration))))
print("g_MAX_DECCELERATION = {} needs {} Bits".format(int(g_max_decceleration),unsigned_num_bits(int(g_max_decceleration))))
In [13]:
import math
speed_resolution = 2**8 # different speed values
speed_max = 120*1/60 # rev/min * min/s = rev/s
max_acceleration_time = 2.0 # seconds from 0 to max speed
max_acceleration_rev = speed_max/max_acceleration_time # rev/s^2
def calc_speed_intended(max_acceleration_rev, position_difference):
# return round(math.sqrt(2*64*max_acceleration_rev*position_difference))
return round(41*math.log(max_acceleration_rev*position_difference+1))
for position_difference in [0,1,2,4,8,16,32,64,128,256,512,1024,2048,4096,8192,16384,32768,65536]:
speed_intended = calc_speed_intended(max_acceleration_rev, position_difference)
if speed_intended > speed_resolution-1:
speed_intended = speed_resolution-1
print("speed_intended: {:3} @ position_difference: {:5}".format(int(speed_intended),position_difference))
# Draw Plot
import numpy as np
import pylab as pl
pl.clf()
nbrOfPoints = 600
position_difference = np.linspace(0,nbrOfPoints,nbrOfPoints)
speed_intended = np.empty(shape=[len(position_difference)], dtype=np.float64)
for i in range(len(position_difference)):
speed_intended[i] = calc_speed_intended(max_acceleration_rev, position_difference[i])
if speed_intended[i] > speed_resolution-1:
speed_intended[i] = speed_resolution-1
# Plot graph
pl.plot(position_difference,speed_intended, label="Acceleration")
speed_intended = np.empty(shape=[len(position_difference)], dtype=np.float64)
for i in range(len(position_difference)):
speed_intended[i] = 255-calc_speed_intended(max_acceleration_rev, position_difference[i])
if speed_intended[i] <= 0:
speed_intended[i] = 0
# Plot graph
pl.plot(position_difference,speed_intended, label="Decceleration")
# Place legend, Axis and Title
pl.legend(loc='best')
pl.xlabel("PositionDifference [Steps]")
pl.ylabel("Speed [0-255]")
pl.title("Acceleration & Deccleration")
Out[13]:
$g\_STEP\_FREQ = \frac{f_{clk}}{f\_step\_driver_{max}}$
For $f\_step\_driver_{max}$ see datasheet motor driver (L6208 = $100kHz$)
In [14]:
f_clk = 100e6 # Hz
f_step_max = 100e3 # Hz
g_step_freq = f_clk/f_step_max
print("Number of steps for max step frequency: {} needs {} Bits".format(int(g_step_freq), unsigned_num_bits(g_step_freq)))