Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
193 changes: 19 additions & 174 deletions main.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,22 +22,17 @@ I will be syncing with this repository on my computer and pasting the code into
*/

async function startProgram() {
//Jake' Movement Here (Stage 1):
await EMWStage1()
await EMWStage2()
await EMWStage3()
await EMWStage4()
await EMWStage5()
await EMWStage6()
await EMWStage7()

//Tung's Movement Here:
await TungStage1()
await TungStage2()
await TungStage3()
exitProgram()
}

async function EMWStage1(){
//Travel to (0,30) at a heading of 0 degrees
async function TungStage1(){
//Travel to (240,0) at a heading of 90 degrees

let setpoint = 30;
let setpoint = 240;
let k = 2.0;
let kD = 0.5;
let kI = 0.001;
Expand All @@ -53,7 +48,7 @@ async function EMWStage1(){

while(stageComplete != true){
//get the current location of the robot.
var location = getLocation().y;
var location = getLocation().x;

//Use a PID algorithm to set the position of the robot
var error = directionSign*(setpoint - location);
Expand All @@ -75,7 +70,7 @@ async function EMWStage1(){
}

//This function rolls the motors at a heading of 0, with a motor speed of output, for 0.2 seconds.
await roll(0,output,0.2);
await roll(90,output,0.2);

if(error < 2.0){
successTimer += 0.1;
Expand All @@ -94,38 +89,37 @@ async function EMWStage1(){

}

async function EMWStage2(){
async function TungStage2(){

//Turn to a heading of 90 degrees.
//Turn to a heading of 180 degrees.

//This function rolls the motors at a heading of 90, with a motor speed of 50, for 0.1 seconds.
await roll(90,50,0.1)
await roll(180,50,0.1)
//...and then this moves it back.
await roll(90,-50,0.1)
await roll(180,-50,0.1)
}


async function EMWStage3(){
async function TungStage3(){

//Travel to (60,30) at a heading of 90 degrees
//Make sure to change the heading at line 150!
//Travel to (240,-60) at a heading of 180 degrees

let setpoint = 60;
let setpoint = -60;
let k = 2.0;
let kD = 0.5;
let kI = 0.001;
var accumulatedError = 0;
var oldError = 0;
var successTimer = 0.0;
var maxSpeed = 100;
var directionSign = 1; //-1 for moving left or down, 1 for moving right or up
var directionSign = -1; //-1 for moving left or down, 1 for moving right or up
stageComplete = false;

await setMainLed({r:0,g:0,b:255})

while(stageComplete != true){
//get the current location of the robot. Note that this uses the x-coordinate this time, not y.
var location = getLocation().x;
var location = getLocation().y;

//Use a PID algorithm to set the position of the robot
var error = directionSign*(setpoint - location);
Expand All @@ -147,155 +141,7 @@ async function EMWStage3(){
}
//We want all of this to be happening at a heading of 90 degrees

await roll(90,output,0.2);

if(error < 2.0){
successTimer += 0.1;

}

//If the error has been less than 2.0 cm for more than half a second, finish the stage.
if(successTimer > 0.5){
stageComplete = true
}

await delay(0.025);
//If our error is less than 1.0 cm, keep track of how long that has been the case.

}

}
async function EMWStage4(){


//Turn to a heading of 0 degrees

//This function rolls the motors at a heading of 90, with a motor speed of 50, for 0.1 seconds.
await roll(0,50,0.1)
//...and then this moves it back.
await roll(0,-50,0.1)
}

async function EMWStage5(){
//Travel to (60,90) at a heading of 0 degrees
//Make sure to change the heading at line 223!

let setpoint = 90;

let k = 2.0;
let kD = 0.5;
let kI = 0.001;
var accumulatedError = 0;
var oldError = 0;
var successTimer = 0.0;
var maxSpeed = 100;
var directionSign = 1; //-1 for moving left or down, 1 for moving right or up

var stageComplete = false;

//Visual feedback to know which stage we are on
await setMainLed({r:255,g:0,b:0})

while(stageComplete != true){
//get the current location of the robot.
var location = getLocation().y;

//Use a PID algorithm to set the position of the robot
var error = directionSign*(setpoint - location);
var changeError = error - oldError;
accumulatedError = error + accumulatedError


var output = k*error - kD*changeError + kI*accumulatedError;
oldError = error

if(output >maxSpeed){

output = maxSpeed;

}
if(output < -255){

output = -maxSpeed;
}

//This function rolls the motors at a heading of 0, with a motor speed of output, for 0.2 seconds.
await roll(0,output,0.2);

if(error < 2.0){
successTimer += 0.1;

}

//If the error has been less than 2.0 cm for more than half a second, finish the stage.
if(successTimer > 0.5){
stageComplete = true
}

await delay(0.025);
//If our error is less than 1.0 cm, keep track of how long that has been the case.


}

}

async function EMWStage6(){

//Turn to a heading of 270 degrees

//This function rolls the motors at a heading of 90, with a motor speed of 50, for 0.1 seconds.
await roll(270,-50,0.1)
//...and then this moves it back.
await roll(270,50,0.1)
}

async function EMWStage7(){

//Travel to (0,90) at a heading of 270 degrees
//Make sure to change the heading at line 298!

let setpoint = 0; //drive for 3 tiles

let k = 2.0;
let kD = 0.5;
let kI = 0.001;
var accumulatedError = 0;
var oldError = 0;
var successTimer = 0.0;
var maxSpeed = 100;
var directionSign = -1; //-1 for moving left or down, 1 for moving right or up

var stageComplete = false;

//Visual feedback to know which stage we are on
await setMainLed({r:255,g:0,b:0})

while(stageComplete != true){
//get the current location of the robot.
var location = getLocation().x;

//Use a PID algorithm to set the position of the robot
var error = directionSign*(setpoint - location);
var changeError = error - oldError;
accumulatedError = error + accumulatedError


var output = k*error - kD*changeError + kI*accumulatedError;
oldError = error

if(output >maxSpeed){

output = maxSpeed;

}
if(output < -255){

output = -maxSpeed;
}

//This function rolls the motors at a heading of 0, with a motor speed of output, for 0.2 seconds.
await roll(270,output,0.2);
await roll(180,output,0.2);

if(error < 2.0){
successTimer += 0.1;
Expand All @@ -310,7 +156,6 @@ async function EMWStage7(){
await delay(0.025);
//If our error is less than 1.0 cm, keep track of how long that has been the case.


}

}