Velocity, acceleration and jerk

From wikiluntti
Revision as of 22:02, 29 September 2020 by Mol (talk | contribs) (→‎Example Codes)

Introduction

The Newton force rule states that force and acceleration are proportionally related. Is it possible to accelerate without noticing the jerk? The method is used in elevators, for example, and hopefully in modern (electric) cars.

We consider the acceleration the robot is subjected to while accelerating it using different acceleration functions.


Robot

Almost any robot will be ok, we use Asimov 2/ Verne.

Sensor

The Vernier acceleration sensor with the NXT Adapter is used. The sensor is attached to port number 4.

Very simple test program to see what are the measured values is shown below. The values are in arbitrary scale, and need to be normalized. We found, that the

#!/usr/bin/env python3
# https://sites.google.com/site/ev3devpython/

from ev3dev2.sensor import *
from ev3dev2.sensor import INPUT_4

import time
import os
import csv 

os.system('setfont Lat15-TerminusBold32x16')  # Try this larger font

p = Sensor( INPUT_4 )
p.mode="ANALOG-1"

def measureAcc(p):
    acc = p.value(0)
    return acc

f = open('acc.csv',"w")
writer = csv.writer(f) 
tstep = 0.001

while True:
    acc = measureAcc(p)
    print( acc )
    writer.writerow((time.time(), acc ))
    time.sleep( tstep )

The resulted file acc.csv is then manually downloaded into a computer, and plotted using Python Pandas:

import pandas as pd
import matplotlib.pyplot as plt

# Read CSV file into DataFrame df
df = pd.read_csv('acc.csv', index_col=0)

# Show thedataframe
print(df)
plt.plot(df)

Theory

The time derivative of position is velocity, then we have acceleration, and the derivative of acceleration is called jerk, . Then we have snap, crackle and pop.

See the Wikipedia page on the physiological_effects of jerk. Anyway, there might be jerky rides.

We measure the acceleration of robot, and use a numerical differential method to estimate the jerk. We use the symmetric differential;

in which the first-order error cancels, and the error is where .

We will start from zero to ms where the speed should be at maximum value, .

Standard Scheme

The ev3dev Python standard scheme is . . .

Linear acceleration

The Linear Acceleration

because the continuity constraint gives that . The graph is shown on Desmos .

Sinusoidal acceleration

Sine acceleration

The sine curve is a harmonic motion between -1 and 1 with period . We use only one period, and scale that to correct acceleration function. The sine accelerates rather smoothly.

See the interactive Desmos graph.

Logistic Curve

The logistic Curve with parameters.

A logistic function is S-shaped curve (or sigmoid curve) is a function with equation

where is the value of the sigmoid's midpoint, is the curve's maximum value and is the curve's growth rate. See the Desmos graph.

The bumb function

The Bumb Function.

There exists some well-known every-where differentiable functions, with a compact support. One example is

with . This is an example of a with support in . We are going to take half of the curve to be our velocity function. The graph is shown on Desmos page.

See more about the function at Math Stack Exchange, and the references therein.

Example Codes

First, we use Python on computer to check the scripts of the acceleration functions. These functions are applied in the motor control part. The robot script logs the data, and the data is uploaded to the computer and analyzed later.

Python

The scripts are on a separate file called accFunction.py, which can be read in both scripts: on a computer and on a motor, to ensure the consistency.

import math

def vlin(t):
    L = 100
    t0= 5000
    if t > t0:
        return L
    if t < 0:
        return 0
    return L*t/t0

def vsin(t):
    L = 100
    t0= 5000
    if t > t0:
        return L
    if t < 0:
        return 0
    return L/2*( math.sin(3.14159*t/t0-3.14159/2)+1)
    
def vlog(t):
    L = 100
    t0= 5000
    if t > t0:
        return L
    if t < 0:
        return 0
    return L/(1 + math.exp(-0.00322*(t-2500)))

def vbum(t):
    L = 100
    a = 4600
    t1= 9270
    if t > a:
        return L
    if t < 100:
        return 0
    else:
        return L*math.exp(-((t1-t)-a)**2/(4*a**2 - (t-t1)**2) )


Ev3dev

The robot program code is still for a second, and then starts accelerating according to the velocity function. The recorded accelerations are logged into corresponding log files, and the next run is appended at the end of the file.

Not that while pressing F5, the VS Code downloads the directory to the robot, and thus you might overwrite the log files.

#!/usr/bin/env python3
# https://sites.google.com/site/ev3devpython/

from ev3dev2.sensor import *
from ev3dev2.sensor import INPUT_4
from ev3dev2.motor import MoveSteering, OUTPUT_B, OUTPUT_C

import time
import os
import csv 

import math
import accFunction 

def measureAcc(p):
    acc = p.value(0)
    return acc

acc = accFunction.vlin
acc = accFunction.vsin
acc = accFunction.vlog
acc = accFunction.vbum

f = open(acc.__name__ + ".csv","a")
writer = csv.writer(f) 

steer_pair = MoveSteering(OUTPUT_B, OUTPUT_C)
p = Sensor( INPUT_4 )
p.mode="ANALOG-1"

s0 = time.time()*1000.0+1000 #One second pause before starting
while time.time()*1000.0 - s0 < 7000:
    t = time.time()*1000.0 - s0
    vt = acc(t)

    steer_pair.on(steering=0, speed=-vt)
    writer.writerow((time.time(), measureAcc(p) ))

f.close()


Analysis Scripts

Note that

Exercises