chjno / MEDP349-Fall17

MEDP 349 Physical Computing, Hunter College, Fall 2017
3 stars 3 forks source link

Sliding pot and sensor not working #21

Open stephanielha opened 6 years ago

stephanielha commented 6 years ago

My two sensors are connected and values come up for both of them on the console through serial and arduino but the movement isn't registered on p5. I haven't changed the code or anything so I'm not sure what's wrong.

chjno commented 6 years ago

can you link your code? did you remember to uncomment the part in parseData() that we commented out?

stephanielha commented 6 years ago

Yes I did. The sub actually moves now but the gyroscope is acting up, the sub does not steer in a fluid way. There's a lot of jumping and jittering.

Here's the p5 sketch code:

var serial; var serial2;

// store the sub graphic var subGraphic; var subNobubbleGraphic;

var bgImage;

// store the player object var player;

// should re-size sub depending on window size var subSize;

// hold projectiles var projectiles = [];

// squids var squids = []; var startingSquidCount = 10; var lastSquidSpawn = 0; var squidSpawnInterval = 1000; // add a new squid every second?

// explosion effects var explosions = [];

// game over stuff var gameOver = false; var timeAlive = 0;

//preload array

var sounds = [];

function preload() { subGraphic = loadImage("assets/bubble.png"); subNobubbleGraphic = loadImage("assets/ship1.png"); squidGraphic = loadImage("assets/squid.png"); bgImage = loadImage("assets/water.jpg"); sounds[0] = loadSound("assets/water.mp3"); sounds[1] = loadSound("assets/explosion.wav"); }

function setup() { createCanvas(windowWidth, windowHeight); background(0); //bgm sounds[0].loop();

// how big should sub be?
subSize = 200;

// create player object
player = new Sub();

// create some squid
for (var i = 0; i < startingSquidCount; i++) {
    squids.push(new Squid());
}

serial = new p5.SerialPort(); serial.open('COM4'); serial.on('data', parseData);

serial2 = new p5.SerialPort(); serial2.open('COM3'); serial2.on('data', parseData2); }

function draw() { background(0);

// background image
push();
imageMode(CORNERS);
image(bgImage, 0, 0, width, height);
pop();

push();
textSize(20);
fill(255);
text( + int(timeAlive) + " seconds", 10, 20);
pop();

if(!gameOver) {

timeAlive = millis() / 1000;

// update and display player
player.update();
player.display();

// did player collide w squid?
for (var i = 0; i < squids.length; i++) {
var distToSquid = dist(squids[i].x, squids[i].y, player.x, player.y);
        if(distToSquid < subSize / 2.5) {

// EXPLOSION!
explosions.push(new Explosion(player.x, player.y));
sounds[1].play();

gameOver = true;

fill(255,255,0);
noStroke();
ellipse(player.x, player.y, 100, 100);
        }

    }

} else {

// game is over
push();
textSize(100);
textAlign(CENTER, CENTER);
fill(255);
text("GAME OVER", width/2, height/3);
pop();

textSize(50);
textAlign(CENTER, CENTER);
fill(255);
text("Refresh to try again?", width/2, height/2);

}

// update all squids
for (var i = 0; i < squids.length; i++) {
    squids[i].update();
    squids[i].display();
}

// and explosions
for (var i = 0; i < explosions.length; i++) {
    explosions[i].updateAndDisplay();

    if(explosions[i].life <= 0)
        explosions.splice(i,1);
}

}

function parseData(){

// uncomment this when you connect the sliding pot

var data = serial.readLine(); if (data.length > 0){ print(data); var values = data.split(','); player.rot = map(int(values[0]), 0, 1023, TWO_PI, 0);

var acc = map(int(data), 0, 1023, 10, 0);
if (acc > 0.1){
  player.isMoving = true;
} else {
  player.isMoving = false;
}
player.speed = acc;

console.log(player.rot + ' ' + player.speed);

} }

function parseData2(){ var data = serial2.readLine(); print(data) if (data.length > 0){ if (data == 'L'){ player.rot -= 1; } else { player.rot += 1; } } }

// construct a Sub class function Sub() { this.x = width/2; this.y = height/2;

this.rot = 0.0; // rotation/orientation of player
this.rotSpeed = .1; // speed of rotation

// acceleration!
this.acc = 0.1;
this.topSpeed = 4.0;
this.speed = 0.0;   // current speed of sub

this.isMoving = false;  // true if pressing forward

// need to keep track of size for collisions
this.size = subSize;

// method to update player
this.update = function() {

    // since we now have acceleration, we always need to
    // update the sub position
    this.x += cos(this.rot) * this.speed;
    this.y += sin(this.rot) * this.speed;

    // check keys

// if(keyIsDown(UP_ARROW)) { // if(this.speed < this.topSpeed) // this.speed += this.acc;

// // draw sub bubbles if pressing up // this.isMoving = true;

// } else { // // up arrow NOT pressed // this.isMoving = false;

// // de-accelerate // if(this.speed > 0) // this.speed -= this.acc; // }

// if(keyIsDown(LEFT_ARROW)) { // // LEFT = forward motion // this.rot -= this.rotSpeed; // }

// if(keyIsDown(RIGHT_ARROW)) { // // RIGHT = forward motion // this.rot += this.rotSpeed; // }

}

// method to draw the sub
this.display = function() {
    imageMode(CENTER);
    // dont effect the rest of my sketch
    push();
    // redefine zero as the sub location
    translate(this.x, this.y);
    // rotate around its center
    rotate(this.rot);
    // draw the actual sub image...
    if(this.isMoving) {
        // if moving, draw bubble graphic
        image(subGraphic,0,0);
    } else {
        // if not moving dont draw bubble graphic
        image(subNobubbleGraphic,0,0);
    }
    pop();
}

}

function Squid() { this.x = 0; this.y = 0; this.speed = 4.0; // speed of overall sub

this.rot = random(TWO_PI);  // rotation/orientation of player

this.size = 50;

// method to update player
this.update = function() {
    // move forward!
    this.x += cos(this.rot) * this.speed;
    this.y += sin(this.rot) * this.speed;

    if(this.x > width)
        this.x = 0;
    if(this.x < 0)
        this.x = width;
    if(this.y > height)
        this.y = 0;
    if(this.y < 0)
        this.y = height;

}

this.display = function() {
    push();
    translate(this.x, this.y);
    // rotate(this.rot);
    rectMode(CENTER);
    fill(0, 100);
    noStroke();
    imageMode(CENTER);
    image(squidGraphic,this.size/2, this.size/2, this.size * 2, this.size * 2)
    pop();
}

}

function Explosion(x, y) {

this.x = x; // placeholder
this.y = y;
this.life = 20; // how many frames long?

this.updateAndDisplay = function() {

    // want explosion to fade out
    this.life--;

    // map opacity to life of explosion
    var op = map(this.life, 20, 0, 255, 0);

    // draw the thing
    push();
    translate(this.x, this.y);
    fill(255, 0, 0, op);
    strokeWeight(subSize);
    stroke(255, 255, 0, op);    // yellow and red, explosion!
    ellipse(0, 0, subSize*2, subSize*2);
    pop();

}

}

function keyPressed() { // show key codes in console if (keyCode == 32 && !gameOver) { console.log("Player X: " + player.x); console.log("Player Y: " + player.y); } }

stephanielha commented 6 years ago

Here's a video that shows the weird movement

chjno commented 6 years ago

did you change the arduino code?

stephanielha commented 6 years ago

No I didn't. I can't load new code into the arduino anyway, it gives me an error.

This is the Gyroscope code that's inside

int turnThreshold = 5000;

// MPU-6050 Short Example Sketch // By Arduino User JohnChi // August 17, 2014 // Public Domain

include

const int MPU_addr = 0x68; // I2C address of the MPU-6050 int16_t AcX, AcY, AcZ, Tmp, GyX, GyY, GyZ; void setup() { Wire.begin(); Wire.beginTransmission(MPU_addr); Wire.write(0x6B); // PWR_MGMT_1 register Wire.write(0); // set to zero (wakes up the MPU-6050) Wire.endTransmission(true); Serial.begin(9600); } void loop() { Wire.beginTransmission(MPU_addr); Wire.write(0x3B); // starting with register 0x3B (ACCEL_XOUT_H) Wire.endTransmission(false); Wire.requestFrom(MPU_addr, 14, true); // request a total of 14 registers AcX = Wire.read() << 8 | Wire.read(); // 0x3B (ACCEL_XOUT_H) & 0x3C (ACCEL_XOUT_L) AcY = Wire.read() << 8 | Wire.read(); // 0x3D (ACCEL_YOUT_H) & 0x3E (ACCEL_YOUT_L) AcZ = Wire.read() << 8 | Wire.read(); // 0x3F (ACCEL_ZOUT_H) & 0x40 (ACCEL_ZOUT_L) Tmp = Wire.read() << 8 | Wire.read(); // 0x41 (TEMP_OUT_H) & 0x42 (TEMP_OUT_L) GyX = Wire.read() << 8 | Wire.read(); // 0x43 (GYRO_XOUT_H) & 0x44 (GYRO_XOUT_L) GyY = Wire.read() << 8 | Wire.read(); // 0x45 (GYRO_YOUT_H) & 0x46 (GYRO_YOUT_L) GyZ = Wire.read() << 8 | Wire.read(); // 0x47 (GYRO_ZOUT_H) & 0x48 (GYRO_ZOUT_L)

// Serial.println(GyX);

if (GyX > turnThreshold){ Serial.println('R'); } else if (GyX < -turnThreshold){ Serial.println('L'); }

delay(200); }

chjno commented 6 years ago

What kind of error?

What readings do you get when you view the output in the arduino serial monitor? Make sure you select the right port in your arduino settings.

stephanielha commented 6 years ago

It just says problem uploading to board. The readings are what they should be and the ports are correct but for some reason it isn't working right in the game