MapNavigator.js 9.5 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37
import polyval from "compute-polynomial";
import { UTMToWGS84 } from "utils/coordinate_converter";

class MapNavigator {
    constructor() {
        this.WS = null;
        this.mapAdapter = null;

        this.routingPaths = [];
        this.plannedPath = null;
        this.vehicleMarker = null;
        this.rightLaneMarker = null;
        this.leftLaneMarker = null;
        this.destinationMarker = null;
        this.centerVehicle = true;
    }

    initialize(WS, mapAdapter) {
        this.WS = WS;
        this.mapAdapter = mapAdapter;

        const initLatlng = { lng: -122.014487, lat: 37.415885 };
        const point = this.mapAdapter.createPoint(initLatlng);
        const divElementName = "map_canvas";
        this.mapAdapter.loadMap(point, divElementName);
        this.vehicleMarker = this.mapAdapter.createMarker(point, null, false);
        this.createControls();

        this.mapAdapter.addEventHandler("click", clickedLatLng => {
            if (!this.destinationMarker) {
                this.destinationMarker = this.mapAdapter.createMarker(clickedLatLng, "D");
            } else {
                this.destinationMarker.setPosition(clickedLatLng);
            }
        });
    }

38 39 40 41
    isInitialized() {
        return this.WS && this.mapAdapter && this.mapAdapter.isInitialized();
    }

42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108
    createControls() {
        this.mapAdapter.createControl({
            text: "Center Vehicle is ON",
            tip: "Click to recenter the vehicle",
            color: "#FFFFFF",
            offsetX: 430,
            offsetY: 0,
            onClickHandler: textElementDiv => {
                if (this.centerVehicle) {
                    this.centerVehicle = false;
                    textElementDiv.innerHTML = "Center Vehicle is OFF";
                    this.mapAdapter.setZoom(15);
                } else {
                    this.centerVehicle = true;
                    textElementDiv.innerHTML = "Center Vehicle is ON";
                    this.mapAdapter.setZoom(20);
                }
            },
        });

        this.mapAdapter.createControl({
            text: "Routing Request",
            tip: "Click to send routing request",
            color: "#CD5C5C",
            offsetX: 298,
            offsetY: 0,
            onClickHandler: textElementDiv => {
                if (!this.destinationMarker) {
                    alert("please select a destination point.");
                    return;
                }

                const start = this.mapAdapter.getMarkerPosition(this.vehicleMarker);
                const end = this.mapAdapter.getMarkerPosition(this.destinationMarker);
                this.requestRouting(start.lat, start.lng, end.lat, end.lng);
            },
        });

        this.mapAdapter.createControl({
            text: "TO Cananda West",
            tip: "Click to send routing request",
            color: "#FF8C00",
            offsetX: 152,
            offsetY: 0,
            onClickHandler: textElementDiv => {
                const start = this.mapAdapter.getMarkerPosition(this.vehicleMarker);
                const endLat = 37.50582457077844;
                const endLng = -122.34000922633726;
                this.requestRouting(start.lat, start.lng, endLat, endLng);
            },
        });

        this.mapAdapter.createControl({
            text: "TO Cananda East",
            tip: "Click to send routing request",
            color: "#00BFFF",
            offsetX: 10,
            offsetY: 0,
            onClickHandler: textElementDiv => {
                const start = this.mapAdapter.getMarkerPosition(this.vehicleMarker);
                const endLat = 37.464198;
                const endLng = -122.298453;
                this.requestRouting(start.lat, start.lng, endLat, endLng);
            },
        });
    }

109 110 111 112 113 114 115 116
    disableControls() {
        this.mapAdapter.disableControls();
    }

    enableControls() {
        this.mapAdapter.enableControls();
    }

117
    update(data) {
118
        if (!this.isInitialized()) {
119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166
            return;
        }

        const adc = data.autoDrivingCar;
        this.updateCenterVehicle(adc);
        this.updateNavigationPath(data.navigationPath);
        this.updateLaneMarkers(adc, data.laneMarker);
        this.updatePlanningPath(adc, data.planningTrajectory);
    }

    updateCenterVehicle(autoDrivingCar) {
        if (!autoDrivingCar) {
            return;
        }

        const x = autoDrivingCar.positionX;
        const y = autoDrivingCar.positionY;
        const heading = autoDrivingCar.heading;

        const [longitude, latitude] = UTMToWGS84(x, y);
        const latLng = this.mapAdapter.createPoint({
            lat: latitude,
            lng: longitude,
        });
        if (this.centerVehicle) {
            this.mapAdapter.setCenter(latLng);
        }
        this.vehicleMarker.setPosition(latLng);
    }

    calculateLaneMarkerPath(autoDrivingCar, laneMarkerData) {
        if (!autoDrivingCar || !laneMarkerData) {
            return;
        }

        const adcX = autoDrivingCar.positionX;
        const adcY = autoDrivingCar.positionY;
        const heading = autoDrivingCar.heading;

        const c0 = laneMarkerData.c0Position;
        const c1 = laneMarkerData.c1HeadingAngle;
        const c2 = laneMarkerData.c2Curvature;
        const c3 = laneMarkerData.c3CurvatureDerivative;
        const markerRange = laneMarkerData.viewRange;
        const markerCoef = [c3, c2, c1, c0];

        const lane = [];
        for (let x = 0; x < markerRange; ++x) {
167
            const y = polyval(markerCoef, x);
168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281
            const newX = x * Math.cos(heading) - y * Math.sin(heading);
            const newY = y * Math.cos(heading) + x * Math.sin(heading);
            const [plon, plat] = UTMToWGS84(adcX + newX, adcY + newY);
            lane.push(this.mapAdapter.createPoint({ lat: plat, lng: plon }));
        }
        return lane;
    }

    updateLaneMarkers(autoDrivingCar, laneMarker) {
        if (!autoDrivingCar || !laneMarker) {
            return;
        }

        const rightLane = this.calculateLaneMarkerPath(autoDrivingCar, laneMarker.rightLaneMarker);
        if (!this.rightLaneMarker) {
            this.rightLaneMarker = this.mapAdapter.createPolyline(rightLane, "#0000FF");
        } else {
            this.mapAdapter.updatePolyline(this.rightLaneMarker, rightLane);
        }

        const leftLane = this.calculateLaneMarkerPath(autoDrivingCar, laneMarker.leftLaneMarker);
        if (!this.leftLaneMarker) {
            this.leftLaneMarker = this.mapAdapter.createPolyline(leftLane, "#0000FF");
        } else {
            this.mapAdapter.updatePolyline(this.leftLaneMarker, leftLane);
        }
    }

    updatePlanningPath(autoDrivingCar, trajectory) {
        if (!autoDrivingCar || !trajectory) {
            return;
        }

        const adcX = autoDrivingCar.positionX;
        const adcY = autoDrivingCar.positionY;
        const heading = autoDrivingCar.heading;

        const path = trajectory.map(point => {
            const x = point.positionX;
            const y = point.positionY;
            const newX = x * Math.cos(heading) - y * Math.sin(heading);
            const newY = y * Math.cos(heading) + x * Math.sin(heading);
            const [plon, plat] = UTMToWGS84(adcX + newX, adcY + newY);
            return this.mapAdapter.createPoint({ lat: plat, lng: plon });
        });

        if (!this.plannedPath) {
            this.plannedPath = this.mapAdapter.createPolyline(path, "#00FF00");
        } else {
            this.mapAdapter.updatePolyline(this.plannedPath, path);
        }
    }

    updateNavigationPath(navigationPaths) {
        if (!navigationPaths) {
            return;
        }

        const paths = navigationPaths.map(navigationPath => {
            return navigationPath.pathPoint.map(point => {
                const [lng, lat] = UTMToWGS84(point.x, point.y);
                return this.mapAdapter.createPoint({ lat: lat, lng: lng });
            });
        });

        if (this.routingPaths.length < paths.length) {
            // adding more polyline(s)
            while (this.routingPaths.length < paths.length) {
                this.routingPaths.push(this.mapAdapter.createPolyline(null, "#CD5C5C", 0.7, 6));
            }
        } else if (this.routingPaths.length > paths.length) {
            // removing extra polyline(s)
            while (this.routingPaths.length > paths.length) {
                this.mapAdapter.removePolyline(this.routingPaths[this.routingPaths.length - 1]);
                this.routingPaths.pop();
            }
        }

        this.routingPaths.forEach((polyline, index) => {
            this.mapAdapter.updatePolyline(polyline, paths[index]);
        });
    }

    requestRouting(startLat, startLng, endLat, endLng) {
        if (!startLat || !startLng || !endLat || !endLng) {
            return;
        }

        const url =
            "http://navi-env.axty8vi3ic.us-west-2.elasticbeanstalk.com" +
            "/dreamview/navigation" +
            `?origin=${startLat},${startLng}` +
            `&destination=${endLat},${endLng}` +
            "&heading=0";
        fetch(url, {
            method: "GET",
            mode: "cors",
        }).then(response => {
            return response.arrayBuffer();
        }).then(response => {
            if (!response.byteLength) {
                console.warn("No navigation info received.");
                return;
            }

            this.WS.publishNavigationInfo(response);
        }).catch(error => {
            console.error("Failed to retrieve navigation data:", error);
        });
    }
}

const MAP_NAVIGATOR = new MapNavigator();
export default MAP_NAVIGATOR;