Skip to content

Commit

Permalink
add python example
Browse files Browse the repository at this point in the history
  • Loading branch information
Lars Berscheid committed Mar 3, 2021
1 parent 0fdd39f commit 141724a
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 5 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,7 @@ Abs(target_acceleration) <= Sqrt(2 * max_jerk * (max_velocity - Abs(target_veloc
```
If a DoF is not enabled, it will be ignored in the calculation. A minimum duration can be optionally given. Furthermore, the minimum velocity and acceleration can be specified. If it is not given, the negative maximum velocity or acceleration will be used (similar to the jerk limit). For example, this might be useful in human robot collaboration settings with a different velocity limit towards a human. Or, the dynamic limits at a given configuration of the robot can be approximated much better with different acceleration limits.

Furthermore, there are some options for advanced functionality, e.g. for velocity control or discrete trajectory durations. We refer to the [API documentation](https://pantor.github.io/ruckig/structruckig_1_1_input_parameter.html) of the `InputParameter` class for the available options.
Furthermore, there are some options for advanced functionality, e.g. for velocity control or discrete trajectory durations. We refer to the [API documentation](https://pantor.github.io/ruckig/structruckig_1_1_input_parameter.html) of the `InputParameter` class for all available options.


### Result Type
Expand Down
51 changes: 51 additions & 0 deletions examples/position.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
from copy import copy
from pathlib import Path
from sys import path

# Path to the build directory including a file similar to '_ruckig.cpython-37m-x86_64-linux-gnu'.
path.insert(0, str(Path(__file__).parent.parent / 'build'))

from _ruckig import InputParameter, OutputParameter, Result, Ruckig


def walk_through_trajectory(otg, inp):
out = OutputParameter()
t, first_output = 0.0, None

print('\t'.join(['t'] + [str(i) for i in range(otg.degrees_of_freedom)]))

res = Result.Working
while res == Result.Working:
res = otg.update(inp, out)

print('\t'.join([f'{t:0.3f}'] + [f'{p:0.3f}' for p in out.new_position]))

inp.current_position = out.new_position
inp.current_velocity = out.new_velocity
inp.current_acceleration = out.new_acceleration

if not first_output:
first_output = copy(out)
t += otg.delta_time

return first_output


if __name__ == '__main__':
inp = InputParameter()
inp.current_position = [0.2, 0, -1]
inp.current_velocity = [0, 0.2, 0]
inp.current_acceleration = [0, 1, 0]
inp.target_position = [0, -1, -1]
inp.target_velocity = [0.2, 0, 0]
inp.target_acceleration = [0, 0.1, -0.1]
inp.max_velocity = [2, 1, 1]
inp.max_acceleration = [0.2, 2, 2]
inp.max_jerk = [3, 4, 5]

otg = Ruckig(0.05)

first_output = walk_through_trajectory(otg, inp)

print(f'Calculation duration: {first_output.calculation_duration:0.1f} [µs]')
print(f'Trajectory duration: {first_output.trajectory.duration:0.4f} [s]')
8 changes: 4 additions & 4 deletions test/otg_plot.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
import copy
from copy import copy
from pathlib import Path
import sys
from sys import path

import matplotlib.pyplot as plt
import numpy as np

sys.path.insert(0, str(Path(__file__).parent.parent / 'build'))
path.insert(0, str(Path(__file__).parent.parent / 'build'))

from _ruckig import Quintic, InputParameter, OutputParameter, Result, Ruckig, Smoothie
from _ruckig import Reflexxes
Expand Down Expand Up @@ -34,7 +34,7 @@ def walk_through_trajectory(otg, inp, print_table=True):
# print(str(inp.current_position[0]) + '\t' + str(inp.current_position[1]))

t_list.append(t)
out_list.append(copy.copy(out))
out_list.append(copy(out))
t += otg.delta_time

return t_list, out_list
Expand Down

0 comments on commit 141724a

Please sign in to comment.