|
| 1 | +#!/usr/bin/env python |
| 2 | + |
| 3 | +# this script demonstrates a simple algorithm for velocity settings |
| 4 | + |
| 5 | +from wpProcess import WaypointProcessor |
| 6 | +import matplotlib.pyplot as plt |
| 7 | +import numpy as np |
| 8 | + |
| 9 | +CSV_IMPORT_FILE = 'tmp_lindenwp_post.csv' |
| 10 | + |
| 11 | +# constants defined below |
| 12 | +AX_MAX = 1.0 |
| 13 | +AY_MAX = 2.0 |
| 14 | +V_HI = 20.0 |
| 15 | +V_LO = 3.0 |
| 16 | +CURV_THR = 0.05 # curvature threshold for low speeds |
| 17 | + |
| 18 | + |
| 19 | + |
| 20 | +def setVel( station, v_hi, a_max ): |
| 21 | + n = len(v_hi) |
| 22 | + v_set = v_hi |
| 23 | + l = 0 |
| 24 | + r = 0 |
| 25 | + for r in range( n ): |
| 26 | + if ( r==n-1 or v_set[r]<v_set[r+1] ): |
| 27 | + # left sweep |
| 28 | + for i in range( l-1,-1,-1 ): |
| 29 | + a_l = ( v_set[i+1]-v_set[i] ) / ( station[i+1]-station[i] ) |
| 30 | + if ( a_l >= -a_max ): |
| 31 | + break; |
| 32 | + else: |
| 33 | + v_set[i] = v_set[i+1]+a_max*( station[i+1]-station[i] ); |
| 34 | + |
| 35 | + # right sweep |
| 36 | + for i in range( r+1, n ): |
| 37 | + a_r = ( v_set[i]-v_set[i-1] ) / ( station[i]-station[i-1] ); |
| 38 | + if ( a_r <= a_max ): |
| 39 | + break; |
| 40 | + else: |
| 41 | + v_set[i] = v_set[i-1]+a_max*( station[i]-station[i-1] ); |
| 42 | + |
| 43 | + elif ( v_set[r]>v_set[r+1] ): |
| 44 | + l = r+1 |
| 45 | + |
| 46 | + return v_set |
| 47 | + |
| 48 | + |
| 49 | +# |
| 50 | +def calAcc( station, vel ): |
| 51 | + s_ = np.asarray(station) |
| 52 | + v_ = np.asarray(vel) |
| 53 | + ds_ = np.gradient(s_) |
| 54 | + dv_ = np.gradient(v_) |
| 55 | + return dv_/ds_ |
| 56 | + |
| 57 | + |
| 58 | + |
| 59 | +# |
| 60 | +WaypointProcessor() |
| 61 | +wp = WaypointProcessor() |
| 62 | +wp.load_waypoints( CSV_IMPORT_FILE ) |
| 63 | +wp.update_curvature() |
| 64 | + |
| 65 | + |
| 66 | +# intial vel |
| 67 | +v_hi = [ V_HI for i in range(len(wp.station)) ] |
| 68 | +for i in range( len(wp.station) ): |
| 69 | + v_hi[i] = V_HI if abs(wp.curvature[i])<CURV_THR else V_LO |
| 70 | + |
| 71 | +# |
| 72 | +vel = setVel( wp.station, v_hi, AX_MAX ) |
| 73 | +acc = calAcc( wp.station, vel ) |
| 74 | + |
| 75 | +# plot velocity wrt station |
| 76 | +plt.figure(1) |
| 77 | +plt.plot( wp.station, v_hi, 'r-' ) |
| 78 | +plt.plot( wp.station, vel, 'b.') |
| 79 | +plt.xlabel('staion [m]') |
| 80 | +plt.ylabel('v [m/s]') |
| 81 | +plt.legend(['v_hi','v'], loc='best') |
| 82 | + |
| 83 | +# plot curvature wrt station |
| 84 | +plt.figure(2) |
| 85 | +plt.plot( wp.station, wp.curvature, 'b.' ) |
| 86 | +plt.xlabel('station [m]') |
| 87 | +plt.ylabel('curvature') |
| 88 | + |
| 89 | +# plot acceleration |
| 90 | +plt.figure(3) |
| 91 | +plt.plot( wp.station, acc, 'b.' ) |
| 92 | +plt.xlabel('staion [m]') |
| 93 | +plt.ylabel('a [m/s^2]') |
| 94 | +plt.show() |
0 commit comments