Final Project Draft

Currently I have figured out the connection between arduino and p5 so that based on poseNet I can move the servo as if it is following me. I have done this on the basis of getting the position of nose pose and sending it to arduino mapped to the servo range. Currently it works as intended however I think it might be a bit off when I am at extreme edges of the canvas video. I am thinking of rectifying by getting a distance variable for my next draft where i will use the distance between eyes to maybe get a more accurate angle.

I will follow a cardboard tutorial to make the CCTV:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
let video;
let poseNet;
let pose;
let skeleton;
let loco= 0;
function setup() {
createCanvas(640, 480);
video = createCapture(VIDEO);
video.hide();
poseNet = ml5.poseNet(video, modelLoaded);
poseNet.on('pose', gotPoses);
}
function gotPoses(poses) {
//console.log(poses);
if (poses.length > 0) {
pose = poses[0].pose;
skeleton = poses[0].skeleton;
}
}
function modelLoaded() {
console.log('poseNet ready');
}
function draw() {
if (!serialActive) {
text("Press Space Bar to select Serial Port", 20, 30);
} else {
text("Connected", 20, 30);
}
image(video, 0, 0);
if (pose) {
fill(255, 0, 0);
ellipse(pose.nose.x, pose.nose.y, 20);
loco = int(pose.nose.x);
val = int(map(loco, 0, 640, 0, 180));
print(val)
}
}
function keyPressed() {
if (key == " ") {
// important to have in order to start the serial connection!!
setUpSerial();
}
}
function readSerial(data) {
////////////////////////////////////
//READ FROM ARDUINO HERE
////////////////////////////////////
if (data != null) {
// make sure there is actually a message
// split the message
let fromArduino = split(trim(data), ",");
// if the right length, then proceed
if (fromArduino.length == 2) {
// only store values here
// do everything with those values in the main draw loop
print("nice");
// We take the string we get from Arduino and explicitly
// convert it to a number by using int()
// e.g. "103" becomes 103
}
//////////////////////////////////
//SEND TO ARDUINO HERE (handshake)
//////////////////////////////////
let sendToArduino = val + "\n";
writeSerial(sendToArduino);
}
}
let video; let poseNet; let pose; let skeleton; let loco= 0; function setup() { createCanvas(640, 480); video = createCapture(VIDEO); video.hide(); poseNet = ml5.poseNet(video, modelLoaded); poseNet.on('pose', gotPoses); } function gotPoses(poses) { //console.log(poses); if (poses.length > 0) { pose = poses[0].pose; skeleton = poses[0].skeleton; } } function modelLoaded() { console.log('poseNet ready'); } function draw() { if (!serialActive) { text("Press Space Bar to select Serial Port", 20, 30); } else { text("Connected", 20, 30); } image(video, 0, 0); if (pose) { fill(255, 0, 0); ellipse(pose.nose.x, pose.nose.y, 20); loco = int(pose.nose.x); val = int(map(loco, 0, 640, 0, 180)); print(val) } } function keyPressed() { if (key == " ") { // important to have in order to start the serial connection!! setUpSerial(); } } function readSerial(data) { //////////////////////////////////// //READ FROM ARDUINO HERE //////////////////////////////////// if (data != null) { // make sure there is actually a message // split the message let fromArduino = split(trim(data), ","); // if the right length, then proceed if (fromArduino.length == 2) { // only store values here // do everything with those values in the main draw loop print("nice"); // We take the string we get from Arduino and explicitly // convert it to a number by using int() // e.g. "103" becomes 103 } ////////////////////////////////// //SEND TO ARDUINO HERE (handshake) ////////////////////////////////// let sendToArduino = val + "\n"; writeSerial(sendToArduino); } }
let video;
let poseNet;
let pose;
let skeleton;
let loco= 0;

function setup() {
  createCanvas(640, 480);
  video = createCapture(VIDEO);
  video.hide();
  poseNet = ml5.poseNet(video, modelLoaded);
  poseNet.on('pose', gotPoses);
}

function gotPoses(poses) {
  //console.log(poses); 
  if (poses.length > 0) {
    pose = poses[0].pose;
    skeleton = poses[0].skeleton;
  }
}


function modelLoaded() {
  console.log('poseNet ready');
}

function draw() {
  
   if (!serialActive) {
    text("Press Space Bar to select Serial Port", 20, 30);
  } else {
    text("Connected", 20, 30);
  }
  
  image(video, 0, 0);

  if (pose) {
    
    fill(255, 0, 0);
    ellipse(pose.nose.x, pose.nose.y, 20);
   
    loco = int(pose.nose.x);
    val = int(map(loco, 0, 640, 0, 180));

    print(val)
    
  
  }
}

function keyPressed() {
  if (key == " ") {
    // important to have in order to start the serial connection!!
    setUpSerial();
  }
}

function readSerial(data) {
  ////////////////////////////////////
  //READ FROM ARDUINO HERE
  ////////////////////////////////////

  if (data != null) {
    // make sure there is actually a message
    // split the message
    let fromArduino = split(trim(data), ",");
    // if the right length, then proceed
    if (fromArduino.length == 2) {
      // only store values here
      // do everything with those values in the main draw loop
      print("nice");
      // We take the string we get from Arduino and explicitly
      // convert it to a number by using int()
      // e.g. "103" becomes 103
    
    }

    //////////////////////////////////
    //SEND TO ARDUINO HERE (handshake)
    //////////////////////////////////
    let sendToArduino = val + "\n";
    writeSerial(sendToArduino);
    
  }
}

P5 👆

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
#include <Servo.h>
Servo myservo; // create servo object to control a servo
void setup() {
Serial.begin(9600);
myservo.attach(9);
// start the handshake
while (Serial.available() <= 0) {
digitalWrite(LED_BUILTIN, HIGH); // on/blink while waiting for serial data
Serial.println("0,0"); // send a starting message
delay(300); // wait 1/3 second
digitalWrite(LED_BUILTIN, LOW);
delay(50);
myservo.write(90); // sets the servo position according to the scaled value
}
}
void loop() {
// wait for data from p5 before doing something
while (Serial.available()) {
Serial.println("0,0");
digitalWrite(LED_BUILTIN, HIGH); // led on while receiving data
int value = Serial.parseInt();
if (Serial.read() == '\n') {
myservo.write(value); // sets the servo position according to the scaled value
}
}
}
#include <Servo.h> Servo myservo; // create servo object to control a servo void setup() { Serial.begin(9600); myservo.attach(9); // start the handshake while (Serial.available() <= 0) { digitalWrite(LED_BUILTIN, HIGH); // on/blink while waiting for serial data Serial.println("0,0"); // send a starting message delay(300); // wait 1/3 second digitalWrite(LED_BUILTIN, LOW); delay(50); myservo.write(90); // sets the servo position according to the scaled value } } void loop() { // wait for data from p5 before doing something while (Serial.available()) { Serial.println("0,0"); digitalWrite(LED_BUILTIN, HIGH); // led on while receiving data int value = Serial.parseInt(); if (Serial.read() == '\n') { myservo.write(value); // sets the servo position according to the scaled value } } }
#include <Servo.h>

Servo myservo;  // create servo object to control a servo


void setup() {

  Serial.begin(9600);

  myservo.attach(9);

  // start the handshake
  while (Serial.available() <= 0) {
    digitalWrite(LED_BUILTIN, HIGH); // on/blink while waiting for serial data
    Serial.println("0,0"); // send a starting message
    delay(300);            // wait 1/3 second
    digitalWrite(LED_BUILTIN, LOW);
    delay(50);
           myservo.write(90);                  // sets the servo position according to the scaled value

  }
}

void loop() {
  // wait for data from p5 before doing something
  
  while (Serial.available()) {
    Serial.println("0,0");
    digitalWrite(LED_BUILTIN, HIGH); // led on while receiving data
    int value = Serial.parseInt();
    if (Serial.read() == '\n') {
       myservo.write(value);                  // sets the servo position according to the scaled value
    }
  }
}

arduino 👆

trial video: https://youtube.com/shorts/h1etPCv24vA

 

Leave a Reply