Skip to content

Instantly share code, notes, and snippets.

@hazkaz
Created January 18, 2018 17:23
Show Gist options
  • Save hazkaz/ca25935525dc44c7a3c422fc02b8f485 to your computer and use it in GitHub Desktop.
Save hazkaz/ca25935525dc44c7a3c422fc02b8f485 to your computer and use it in GitHub Desktop.
zigzag PID bot
const { vector, comm } = require("bytearena-sdk");
const Vector2 = vector.Vector2;
const agent = comm.connect();
let count = 1
const countLimit = 55
let countDirection = 1
let first = true
let angle = 0
agent.on("perception", perception => {
const actions = []
let p = 1
let x = 0
let y = 1
let steering
if (count === countLimit) {
if (first === true) {
if (angle != 0 ){
angle = 0
// console.log("zero")
}
else {
// console.log("pi")
angle = Math.PI
}
first=false
}
steering = navigate(perception, angle)
// console.log(steering)
if (steering.x == 0) {
count = 1
first = true
}
}
else {
count += countDirection
steering = new Vector2(0, 1)
}
// console.log(count)
actions.push({ method: "steer", arguments: steering.toArray() });
agent.do(actions);
});
const navigate = (perception, angle) => {
let steering = null
let x, y
let p = 1
let error=angle-perception.azimuth
if (Math.abs(error) > .01) {
x =p * (angle - perception.azimuth)
y = 1
}
else {
x = 0
}
steering = new Vector2(x, y)
return steering
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment