|
1 |
| -const path = new Path(); // robot path |
| 1 | +let path = new Path(); // robot path |
2 | 2 | let debugPath = [];
|
| 3 | +let debugDataList = []; |
| 4 | +let debugDataTime = 0; |
| 5 | +let debugSet = false; |
3 | 6 |
|
4 | 7 |
|
5 | 8 | // program mode
|
@@ -30,7 +33,7 @@ function hslToHex(h, s, l) {
|
30 | 33 | return Math.round(255 * color).toString(16).padStart(2, '0');
|
31 | 34 | };
|
32 | 35 | return `#${f(0)}${f(8)}${f(4)}`;
|
33 |
| -} |
| 36 | +}; |
34 | 37 |
|
35 | 38 |
|
36 | 39 | /**
|
@@ -68,77 +71,151 @@ function getInput() {
|
68 | 71 |
|
69 | 72 |
|
70 | 73 | /**
|
71 |
| - * @brief draw the spline |
| 74 | + * @brief function to create a field |
72 | 75 | */
|
73 |
| -function render() { |
74 |
| - // clear the canvas |
75 |
| - ctx.clearRect(0, 0, canvas.width, canvas.height); |
76 |
| - // draw the field |
77 |
| - ctx.drawImage(img, 0, 0, img.width, img.height, // source rectangle |
78 |
| - 0, 0, canvas.width, canvas.height); // destination rectangle |
| 76 | +function renderCreate() { |
| 77 | + // init |
| 78 | + getInput(); |
| 79 | + const finalSpacing = Math.round(path.length / inchesPerPoint); |
| 80 | + path.genPoints(precision, finalSpacing); |
79 | 81 |
|
80 |
| - // create mode render |
81 |
| - if (mode == 0) { |
82 |
| - // init |
83 |
| - getInput(); |
84 |
| - const finalSpacing = Math.round(path.length / inchesPerPoint); |
85 |
| - path.genPoints(precision, finalSpacing); |
86 |
| - |
87 |
| - // draw control points |
88 |
| - for (let i = 0; i < path.splines.length; i++) { |
89 |
| - const p1 = coordToPx(path.splines[i].p1); |
90 |
| - const p2 = coordToPx(path.splines[i].p2); |
91 |
| - const p3 = coordToPx(path.splines[i].p3); |
92 |
| - const p4 = coordToPx(path.splines[i].p4); |
93 |
| - ctx.fillStyle = hslToHex(140, 50, 50); |
94 |
| - ctx.strokeStyle = hslToHex(0, 0, 0); |
95 |
| - // draw the lines between the control points |
| 82 | + // draw control points |
| 83 | + for (let i = 0; i < path.splines.length; i++) { |
| 84 | + const p1 = coordToPx(path.splines[i].p1); |
| 85 | + const p2 = coordToPx(path.splines[i].p2); |
| 86 | + const p3 = coordToPx(path.splines[i].p3); |
| 87 | + const p4 = coordToPx(path.splines[i].p4); |
| 88 | + ctx.fillStyle = hslToHex(140, 50, 50); |
| 89 | + ctx.strokeStyle = hslToHex(0, 0, 0); |
| 90 | + // draw the lines between the control points |
| 91 | + ctx.beginPath(); |
| 92 | + ctx.moveTo(p1.x, p1.y); |
| 93 | + ctx.lineTo(p2.x, p2.y); |
| 94 | + ctx.stroke(); |
| 95 | + ctx.closePath(); |
| 96 | + ctx.beginPath(); |
| 97 | + ctx.moveTo(p3.x, p3.y); |
| 98 | + ctx.lineTo(p4.x, p4.y); |
| 99 | + ctx.stroke(); |
| 100 | + ctx.closePath(); |
| 101 | + // draw the control points |
| 102 | + ctx.beginPath(); |
| 103 | + ctx.arc(p1.x, p1.y, controlPointRadius*imgPixelsPerInch, 0, 2*Math.PI); |
| 104 | + ctx.arc(p2.x, p2.y, controlPointRadius*imgPixelsPerInch, 0, 2*Math.PI); |
| 105 | + ctx.fill(); |
| 106 | + ctx.closePath(); |
| 107 | + ctx.beginPath(); |
| 108 | + ctx.arc(p3.x, p3.y, controlPointRadius*imgPixelsPerInch, 0, 2*Math.PI); |
| 109 | + ctx.arc(p4.x, p4.y, controlPointRadius*imgPixelsPerInch, 0, 2*Math.PI); |
| 110 | + ctx.fill(); |
| 111 | + ctx.closePath(); |
| 112 | + } |
| 113 | + |
| 114 | + // draw spline |
| 115 | + for (let i = 0; i < path.points2.length; i++) { |
| 116 | + const p1 = coordToPx(path.points2[i]); |
| 117 | + // draw the points |
| 118 | + const radiusSetting = 0.5; |
| 119 | + const radius = radiusSetting * imgPixelsPerInch; |
| 120 | + ctx.fillStyle = hslToHex((path.points2[i].velocity/maxSpeed)*180, |
| 121 | + 100, 50); |
| 122 | + ctx.strokeStyle = ctx.fillStyle; |
| 123 | + ctx.beginPath(); |
| 124 | + ctx.arc(p1.x, p1.y, radius, 0, 2 * Math.PI); |
| 125 | + ctx.stroke(); |
| 126 | + ctx.fill(); |
| 127 | + ctx.closePath(); |
| 128 | + // draw the lines |
| 129 | + if (i < path.points2.length - 1) { |
| 130 | + const p2 = coordToPx(path.points2[i + 1]); |
96 | 131 | ctx.beginPath();
|
97 | 132 | ctx.moveTo(p1.x, p1.y);
|
98 | 133 | ctx.lineTo(p2.x, p2.y);
|
99 | 134 | ctx.stroke();
|
100 | 135 | ctx.closePath();
|
101 |
| - ctx.beginPath(); |
102 |
| - ctx.moveTo(p3.x, p3.y); |
103 |
| - ctx.lineTo(p4.x, p4.y); |
104 |
| - ctx.stroke(); |
105 |
| - ctx.closePath(); |
106 |
| - // draw the control points |
107 |
| - ctx.beginPath(); |
108 |
| - ctx.arc(p1.x, p1.y, controlPointRadius*imgPixelsPerInch, 0, 2*Math.PI); |
109 |
| - ctx.arc(p2.x, p2.y, controlPointRadius*imgPixelsPerInch, 0, 2*Math.PI); |
110 |
| - ctx.fill(); |
111 |
| - ctx.closePath(); |
112 |
| - ctx.beginPath(); |
113 |
| - ctx.arc(p3.x, p3.y, controlPointRadius*imgPixelsPerInch, 0, 2*Math.PI); |
114 |
| - ctx.arc(p4.x, p4.y, controlPointRadius*imgPixelsPerInch, 0, 2*Math.PI); |
115 |
| - ctx.fill(); |
116 |
| - ctx.closePath(); |
117 | 136 | }
|
| 137 | + } |
| 138 | +}; |
| 139 | + |
118 | 140 |
|
119 |
| - // draw spline |
120 |
| - for (let i = 0; i < path.points2.length; i++) { |
121 |
| - const p1 = coordToPx(path.points2[i]); |
122 |
| - // draw the points |
123 |
| - const radiusSetting = 0.5; |
124 |
| - const radius = radiusSetting * imgPixelsPerInch; |
125 |
| - ctx.fillStyle = hslToHex((path.points2[i].velocity/maxSpeed)*180, |
126 |
| - 100, 50); |
127 |
| - ctx.strokeStyle = ctx.fillStyle; |
| 141 | +/** |
| 142 | + * @brief render for debug mode |
| 143 | + */ |
| 144 | +function renderDebug() { |
| 145 | + // render the path |
| 146 | + for (let i = 0; i < debugPath.length; i++) { |
| 147 | + const p1 = coordToPx(debugPath[i]); |
| 148 | + // draw the points |
| 149 | + const radiusSetting = 0.5; |
| 150 | + const radius = radiusSetting * imgPixelsPerInch; |
| 151 | + ctx.fillStyle = hslToHex((debugPath[i].velocity/maxSpeed)*180, |
| 152 | + 100, 50); |
| 153 | + ctx.strokeStyle = ctx.fillStyle; |
| 154 | + ctx.beginPath(); |
| 155 | + ctx.arc(p1.x, p1.y, radius, 0, 2 * Math.PI); |
| 156 | + ctx.stroke(); |
| 157 | + ctx.fill(); |
| 158 | + ctx.closePath(); |
| 159 | + // draw the lines |
| 160 | + if (i < debugPath.length - 1) { |
| 161 | + const p2 = coordToPx(debugPath[i + 1]); |
128 | 162 | ctx.beginPath();
|
129 |
| - ctx.arc(p1.x, p1.y, radius, 0, 2 * Math.PI); |
| 163 | + ctx.moveTo(p1.x, p1.y); |
| 164 | + ctx.lineTo(p2.x, p2.y); |
130 | 165 | ctx.stroke();
|
131 |
| - ctx.fill(); |
132 | 166 | ctx.closePath();
|
133 |
| - // draw the lines |
134 |
| - if (i < path.points2.length - 1) { |
135 |
| - const p2 = coordToPx(path.points2[i + 1]); |
136 |
| - ctx.beginPath(); |
137 |
| - ctx.moveTo(p1.x, p1.y); |
138 |
| - ctx.lineTo(p2.x, p2.y); |
139 |
| - ctx.stroke(); |
140 |
| - ctx.closePath(); |
141 |
| - } |
142 | 167 | }
|
143 | 168 | }
|
| 169 | + |
| 170 | + // draw the robot at the current time stamp |
| 171 | + const robotPos = new Point(); |
| 172 | + let robotPosPx = new Point(); |
| 173 | + const heading = Math.PI/2 - degToRad(debugDataList[debugDataTime].heading); |
| 174 | + robotPos.x = debugDataList[debugDataTime].x; |
| 175 | + robotPos.y = debugDataList[debugDataTime].y; |
| 176 | + robotPosPx = coordToPx(robotPos); |
| 177 | + ctx.beginPath(); |
| 178 | + ctx.fillStyle = hslToHex(0, 0, 0); |
| 179 | + ctx.strokeStyle = hslToHex(0, 0, 0); |
| 180 | + ctx.arc(robotPosPx.x, robotPosPx.y, 2*imgPixelsPerInch, 0, 2 * Math.PI); |
| 181 | + ctx.fill(); |
| 182 | + ctx.closePath(); |
| 183 | + ctx.beginPath(); |
| 184 | + ctx.arc(robotPosPx.x, robotPosPx.y, 9*imgPixelsPerInch, 0, 2 * Math.PI); |
| 185 | + ctx.stroke(); |
| 186 | + ctx.closePath(); |
| 187 | + |
| 188 | + // draw the robot's heading |
| 189 | + const headingVec = new Point(); |
| 190 | + headingVec.x = robotPos.x + 5*Math.cos(heading); |
| 191 | + headingVec.y = robotPos.y + 5*Math.sin(heading); |
| 192 | + const headingVecPx = coordToPx(headingVec); |
| 193 | + ctx.strokeStyle = hslToHex(184, 100, 63); |
| 194 | + ctx.beginPath(); |
| 195 | + ctx.lineWidth = 0.5*imgPixelsPerInch; |
| 196 | + ctx.moveTo(robotPosPx.x, robotPosPx.y); |
| 197 | + ctx.lineTo(headingVecPx.x, headingVecPx.y); |
| 198 | + ctx.stroke(); |
| 199 | + ctx.closePath(); |
| 200 | +}; |
| 201 | + |
| 202 | + |
| 203 | +/** |
| 204 | + * @brief draw the spline |
| 205 | + */ |
| 206 | +function render() { |
| 207 | + // clear the canvas |
| 208 | + ctx.clearRect(0, 0, canvas.width, canvas.height); |
| 209 | + // draw the field |
| 210 | + ctx.drawImage(img, 0, 0, img.width, img.height, // source rectangle |
| 211 | + 0, 0, canvas.width, canvas.height); // destination rectangle |
| 212 | + // reset line width |
| 213 | + ctx.lineWidth = 1.0; |
| 214 | + |
| 215 | + // create mode render |
| 216 | + if (mode == 0) { |
| 217 | + renderCreate(); |
| 218 | + } else if (debugSet) { |
| 219 | + renderDebug(); |
| 220 | + } |
144 | 221 | };
|
0 commit comments