📄 path_8cpp-source.html
字号:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"><html><head><meta http-equiv="Content-Type" content="text/html;charset=iso-8859-1"><title>Copley Motion Library: Path.cpp Source File</title><link href="doxygen.css" rel="stylesheet" type="text/css"></head><body><!-- Generated by Doxygen 1.4.4 --><div class="qindex"><a class="qindex" href="main.html">Main Page</a> | <a class="qindex" href="hierarchy.html">Class Hierarchy</a> | <a class="qindex" href="classes.html">Alphabetical List</a> | <a class="qindex" href="annotated.html">Class List</a> | <a class="qindex" href="dirs.html">Directories</a> | <a class="qindex" href="files.html">File List</a> | <a class="qindex" href="functions.html">Class Members</a> | <a class="qindex" href="globals.html">File Members</a></div><div class="nav"><a class="el" href="dir_000000.html">c</a></div><h1>Path.cpp</h1><div class="fragment"><pre class="fragment"><a name="l00001"></a>00001 <span class="comment">/************************************************************/</span><a name="l00002"></a>00002 <span class="comment">/* */</span><a name="l00003"></a>00003 <span class="comment">/* Copley Motion Libraries */</span><a name="l00004"></a>00004 <span class="comment">/* */</span><a name="l00005"></a>00005 <span class="comment">/* Author: Stephen Glow */</span><a name="l00006"></a>00006 <span class="comment">/* */</span><a name="l00007"></a>00007 <span class="comment">/* Copyright (c) 2002-2005 Copley Controls Corp. */</span><a name="l00008"></a>00008 <span class="comment">/* http://www.copleycontrols.com */</span><a name="l00009"></a>00009 <span class="comment">/* */</span><a name="l00010"></a>00010 <span class="comment">/************************************************************/</span><a name="l00011"></a>00011 <a name="l00012"></a>00012 <span class="preprocessor">#include "<a class="code" href="CML__Settings_8h.html">CML_Settings.h</a>"</span><a name="l00013"></a>00013 <span class="preprocessor">#ifdef CML_ALLOW_FLOATING_POINT</span><a name="l00014"></a>00014 <span class="preprocessor"></span><a name="l00015"></a>00015 <span class="preprocessor">#include <math.h></span><a name="l00016"></a>00016 <span class="preprocessor">#include "<a class="code" href="CML_8h.html">CML.h</a>"</span><a name="l00017"></a>00017 <span class="preprocessor">#include "CML_Path.h"</span><a name="l00018"></a>00018 <a name="l00019"></a>00019 <a class="code" href="CML__Settings_8h.html#a11">CML_NAMESPACE_START</a>()<a name="l00020"></a>00020 <a name="l00021"></a>00021 <span class="comment">// Error objects specific to path planning</span><a name="l00022"></a>00022 <a class="code" href="CML__Error_8h.html#a0">CML_NEW_ERROR</a>( <a class="code" href="classPathError.html">PathError</a>, BadVel, "Illegal velocity value" );<a name="l00023"></a>00023 <a class="code" href="CML__Error_8h.html#a0">CML_NEW_ERROR</a>( PathError, BadAcc, "Illegal acceleration value" );<a name="l00024"></a>00024 <a class="code" href="CML__Error_8h.html#a0">CML_NEW_ERROR</a>( PathError, VelNotInit, "Velocity limit not yet set" );<a name="l00025"></a>00025 <a class="code" href="CML__Error_8h.html#a0">CML_NEW_ERROR</a>( PathError, AccNotInit, "Acceleration limit not yet set" );<a name="l00026"></a>00026 <a class="code" href="CML__Error_8h.html#a0">CML_NEW_ERROR</a>( PathError, BadPoint, "The passed point doesn't match the path" );<a name="l00027"></a>00027 <a class="code" href="CML__Error_8h.html#a0">CML_NEW_ERROR</a>( PathError, Alloc, "Unable to allocate memory for path" );<a name="l00028"></a>00028 <a class="code" href="CML__Error_8h.html#a0">CML_NEW_ERROR</a>( PathError, BadLength, "An illegal negative length value was passed" );<a name="l00029"></a>00029 <a class="code" href="CML__Error_8h.html#a0">CML_NEW_ERROR</a>( PathError, Empty, "Attempt to execute an empty path" );<a name="l00030"></a>00030 <a name="l00031"></a>00031 <span class="comment">// This constant defines the maximum angle (radians) between two line</span><a name="l00032"></a>00032 <span class="comment">// segments that I will accept without a full stop in between.</span><a name="l00033"></a>00033 <span class="comment">//</span><a name="l00034"></a>00034 <span class="comment">// FIXME: At the moment this is just set to 0.1 deg without much justification.</span><a name="l00035"></a>00035 <span class="comment">// I should study the issue to come up with a better number</span><a name="l00036"></a>00036 #define PI 3.14159265358979323846 <span class="comment">/* pi */</span><a name="l00037"></a>00037 #define PI_by_2 1.57079632679489661923 <span class="comment">/* pi/2 */</span><a name="l00038"></a>00038 #define MAX_ANGLE_ERROR (0.1 * PI/180.0)<a name="l00039"></a>00039 <a name="l00040"></a>00040 #define MIN_PVT_TIME 0.001<a name="l00041"></a>00041 <span class="comment"></span><a name="l00042"></a>00042 <span class="comment">/**</span><a name="l00043"></a>00043 <span class="comment"> Internal class used by Path planning code.</span><a name="l00044"></a>00044 <span class="comment"> */</span><a name="l00045"></a>00045 class PathElement<a name="l00046"></a>00046 {<a name="l00047"></a>00047 <span class="keyword">protected</span>:<a name="l00048"></a>00048 PathElement *next;<a name="l00049"></a>00049 PathElement *prev;<a name="l00050"></a>00050 <a name="l00051"></a>00051 <span class="comment">// These values are calculated when a segment is first </span><a name="l00052"></a>00052 <span class="comment">// initialized and never change during the life of the </span><a name="l00053"></a>00053 <span class="comment">// segment.</span><a name="l00054"></a>00054 <span class="keywordtype">double</span> velUp; <span class="comment">// Maximum increase in velocity based on accel & distance</span><a name="l00055"></a>00055 <span class="keywordtype">double</span> velDn; <span class="comment">// Maximum decrease in velocity based on decel & distance</span><a name="l00056"></a>00056 <span class="keywordtype">double</span> length; <span class="comment">// Length of current segment</span><a name="l00057"></a>00057 <a name="l00058"></a>00058 <span class="keywordtype">double</span> velMax; <span class="comment">// Maximum velocity limit during this segment</span><a name="l00059"></a>00059 <span class="keywordtype">double</span> accMax; <span class="comment">// Acceleration limit to us during this segment</span><a name="l00060"></a>00060 <span class="keywordtype">double</span> decMax; <span class="comment">// Deceleration limit to us during this segment</span><a name="l00061"></a>00061 <a name="l00062"></a>00062 <span class="comment">// These parameters will change as more segments are added</span><a name="l00063"></a>00063 <span class="comment">// to the path.</span><a name="l00064"></a>00064 <span class="keywordtype">double</span> velEnd; <span class="comment">// Maximum velocity at end of segment</span><a name="l00065"></a>00065 <span class="keywordtype">double</span> velPeak; <span class="comment">// Maximum ending velocity possible based on earlier segments</span><a name="l00066"></a>00066 <a name="l00067"></a>00067 <span class="comment">// Once I've determined the starting and ending velocity for a segment, I'll </span><a name="l00068"></a>00068 <span class="comment">// calculate the following information which will describe segment</span><a name="l00069"></a>00069 <span class="comment">// ta - Time to accelerate</span><a name="l00070"></a>00070 <span class="comment">// pa - Position (distance into path) at the end of ta</span><a name="l00071"></a>00071 <span class="comment">// va - Velocity (along path) at the end of ta</span><a name="l00072"></a>00072 <span class="comment">//</span><a name="l00073"></a>00073 <span class="comment">// tv - Time to run at max velocity</span><a name="l00074"></a>00074 <span class="comment">// pv - Position at the end of (ta+tv)</span><a name="l00075"></a>00075 <span class="comment">//</span><a name="l00076"></a>00076 <span class="comment">// td - Time to decelerate</span><a name="l00077"></a>00077 <span class="comment">// pd - Position at the end of (ta+tv+td)</span><a name="l00078"></a>00078 <span class="keywordtype">double</span> ta,tv,td;<a name="l00079"></a>00079 <span class="keywordtype">double</span> pa,pv,pd;<a name="l00080"></a>00080 <span class="keywordtype">double</span> va;<a name="l00081"></a>00081 <span class="keywordtype">bool</span> calculated;<a name="l00082"></a>00082 <span class="comment"></span><a name="l00083"></a>00083 <span class="comment"> /**</span><a name="l00084"></a>00084 <span class="comment"> * Set the segment length. This must be called in the </span><a name="l00085"></a>00085 <span class="comment"> * constructor of the segment object before the segment</span><a name="l00086"></a>00086 <span class="comment"> * is added to a path.</span><a name="l00087"></a>00087 <span class="comment"> */</span><a name="l00088"></a>00088 <span class="keywordtype">void</span> setLength( <span class="keywordtype">double</span> L )<a name="l00089"></a>00089 {<a name="l00090"></a>00090 length = L;<a name="l00091"></a>00091 }<a name="l00092"></a>00092 <a name="l00093"></a>00093 <span class="keyword">public</span>:<a name="l00094"></a>00094 PathElement( <span class="keywordtype">void</span> )<a name="l00095"></a>00095 {<a name="l00096"></a>00096 velPeak = velUp = velDn = velEnd = 0.0;<a name="l00097"></a>00097 velMax = accMax = decMax = 0.0;<a name="l00098"></a>00098 length = 0.0;<a name="l00099"></a>00099 next = prev = 0;<a name="l00100"></a>00100 calculated = <span class="keyword">false</span>;<a name="l00101"></a>00101 }<a name="l00102"></a>00102 <a name="l00103"></a>00103 <span class="keyword">virtual</span> ~PathElement()<a name="l00104"></a>00104 {<a name="l00105"></a>00105 Unlink();<a name="l00106"></a>00106 }<a name="l00107"></a>00107 <a name="l00108"></a>00108 <span class="keywordtype">void</span> Unlink( <span class="keywordtype">void</span> )<a name="l00109"></a>00109 {<a name="l00110"></a>00110 <span class="keywordflow">if</span>( prev ) prev->next = next;<a name="l00111"></a>00111 <span class="keywordflow">if</span>( next ) next->prev = prev;<a name="l00112"></a>00112 next = prev = 0;<a name="l00113"></a>00113 }<a name="l00114"></a>00114 <span class="comment"></span><a name="l00115"></a>00115 <span class="comment"> /**</span><a name="l00116"></a>00116 <span class="comment"> * Add this segment to the end of the </span><a name="l00117"></a>00117 <span class="comment"> * passed path.</span><a name="l00118"></a>00118 <span class="comment"> */</span><a name="l00119"></a>00119 <span class="keywordtype">void</span> Add( PathElement *pe )<a name="l00120"></a>00120 {<a name="l00121"></a>00121 <span class="comment">// Add this segment after the passed one.</span><a name="l00122"></a>00122 <span class="keywordflow">if</span>( pe ) pe->next = <span class="keyword">this</span>;<a name="l00123"></a>00123 this->prev = pe;<a name="l00124"></a>00124 <a name="l00125"></a>00125 <span class="comment">// Find the peak velocity that could be </span><a name="l00126"></a>00126 <span class="comment">// reached at the end of this segment if</span><a name="l00127"></a>00127 <span class="comment">// I didn't have to worry about stopping in </span><a name="l00128"></a>00128 <span class="comment">// the future.</span><a name="l00129"></a>00129 <span class="comment">//</span><a name="l00130"></a>00130 <span class="comment">// This is the previous segment's peak velocity</span><a name="l00131"></a>00131 <span class="comment">// plus the increase I could provide based on </span><a name="l00132"></a>00132 <span class="comment">// this segment's acceleration & length.</span><a name="l00133"></a>00133 <span class="keywordflow">if</span>( pe )<a name="l00134"></a>00134 velPeak = pe->velPeak + velUp;<a name="l00135"></a>00135 <span class="keywordflow">else</span><a name="l00136"></a>00136 velPeak = velUp;<a name="l00137"></a>00137 <a name="l00138"></a>00138 <span class="keywordflow">if</span>( velPeak > getMaxVel() )<a name="l00139"></a>00139 velPeak = getMaxVel();<a name="l00140"></a>00140 }<a name="l00141"></a>00141 <a name="l00142"></a>00142 PathElement *getPrev( <span class="keywordtype">void</span> ){ <span class="keywordflow">return</span> prev; }<a name="l00143"></a>00143 PathElement *getNext( <span class="keywordtype">void</span> ){ <span class="keywordflow">return</span> next; }<a name="l00144"></a>00144 <a name="l00145"></a>00145 <span class="keywordtype">double</span> getVelStart( <span class="keywordtype">void</span> )<a name="l00146"></a>00146 {<a name="l00147"></a>00147 PathElement *pe = getPrev();<a name="l00148"></a>00148 <span class="keywordflow">return</span> (pe) ? pe->getVelEnd() : 0.0;<a name="l00149"></a>00149 }<a name="l00150"></a>00150 <a name="l00151"></a>00151 <span class="keywordtype">double</span> getVelEnd( <span class="keywordtype">void</span> )<a name="l00152"></a>00152 {<a name="l00153"></a>00153 <span class="keywordflow">return</span> velEnd;<a name="l00154"></a>00154 }
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -