xxxxxxxxxx
244
/*
//Simple structure forked from Andreas Refsgaard.
//requires this code uploaded to the Arduino -->
https://github.com/j3nsykes/creativeML2020/tree/master/PhysicalTeachableMachines/ML5_neuralNet/Arduino/AnalogInput_Serial/Serial_arrayInputs
//Adapted by j3nsykes to train an array of incoming inputs.
*/
// The neural network is the brain
let brain;
let label;
// Declare a "SerialPort" object
let serial;
let latestData = "waiting for data"; // you'll use this to write incoming data to the canvas
let inputs = []; // all your inputs in an array
let totalInputs = 8; //how many incoming inputs? scale to your delight.
let x = 0.0;
let y = 0.0;
let z = 0.0;
let f;
let cresult = '';
let displayResults=false;
function preload() {
f = loadFont('SourceCodePro-Medium.ttf');
// f = loadFont('Nunito-Bold.ttf');
}
function setup() {
let canvas = createCanvas(500, 500);
// Only when clicking on the canvas
//canvas.mousePressed(addData);
// Create the model
const options = {
inputs: totalInputs, //update the variable to scale
outputs: ['label'], // amount of label outputs
debug: true,
task: 'classification'
}
brain = ml5.neuralNetwork(options);
// Train Model button
select('#train').mousePressed(trainModel);
background(0);
// Instantiate our SerialPort object
serial = new p5.SerialPort();
// Get a list the ports available
// You should have a callback defined to see the results
serial.list();
// Assuming our Arduino is connected, let's open the connection to it
//!!!!!!! CHECK THIS !!!!
// Change this to the name of your arduino's serial port
//serial.open("/dev/tty.usbmodem144101");
//serial.open("/dev/tty.usbmodem21956001");
serial.open("/dev/tty.SLAB_USBtoUART");
// Here are the callbacks that you can register
// When we connect to the underlying server
serial.on('connected', serverConnected);
// When we get a list of serial ports that are available
serial.on('list', gotList);
// OR
//serial.onList(gotList);
// When we some data from the serial port
serial.on('data', getInputs);
// OR
//serial.onData(gotData);
// When or if we get an error
serial.on('error', gotError);
// OR
//serial.onError(gotError);
// When our serial port is opened and ready for read/write
serial.on('open', gotOpen);
// OR
//serial.onOpen(gotOpen);
serial.on('close', gotClose);
// Callback to get the raw data, as it comes in for handling yourself
//serial.on('rawdata', gotRawData);
// OR
//serial.onRawData(gotRawData);
}
// Add a data record
function addData() {
// Get frequency
label = select('#label').value();
// Add data
//retirve array of data
let data = getItem('myData');
// console.log(inputs); //this slows process down significantly use with caution!
brain.addData(data, {
label
});
}
// Train the model
function trainModel() {
// ml5 will normalize data to a range between 0 and 1 for you.
brain.normalizeData();
// Train the model
// Epochs: one cycle through all the training data
brain.train({
epochs: 100
}, finishedTraining);
}
// Classify
function classify() {
console.log('classify');
let data = getItem('myData');
brain.classify(data, gotResults);
}
// When the model is trained
function finishedTraining() {
classify();
}
// Got a result
function gotResults(error, results) {
if (error) {
console.error(error);
return;
}
// Show classification
select('#classification').html(results[0].label);
cresult=results[0].label;
displayResults=true;
// Predict again
classify();
}
function serverConnected() {
print("Connected to Server");
}
// Got the list of ports
function gotList(thelist) {
print("List of Serial Ports:");
// theList is an array of their names
for (let i = 0; i < thelist.length; i++) {
// Display in the console
print(i + " " + thelist[i]);
}
}
// Connected to our serial device
function gotOpen() {
print("Serial Port is Open");
}
function gotClose() {
print("Serial Port is Closed");
latestData = "Serial Port is Closed";
}
//Oh, here is an error, let's log it
function gotError(theerror) {
print(theerror);
}
// There is data available to work with from the serial port
function getInputs() {
let currentString = serial.readLine(); // read the incoming string
trim(currentString); // remove any trailing whitespace
if (!currentString) return; // if the string is empty, do no more
//console.log(currentString); // print the string
//seperate values in the array
let splitVal = splitTokens(currentString, ',');
//parse strings into floats.
splitVal = float(splitVal);
//console.log(splitVal);
//get the individual values for visualisation
x = splitVal[0];
y = splitVal[1];
z = splitVal[2];
//TODO make this an array so it can be scaled
//only push totalInputs at one a time.
let inputs = [];
for (let i = 0; i < totalInputs; i++) {
inputs.push(splitVal[i]);
}
//console.log(inputs);
storeItem('myData', inputs); //store array info
}
// We got raw from the serial port
function gotRawData(thedata) {
print("gotRawData" + thedata);
}
function draw() {
if (mouseIsPressed) {
addData();
let label = select('#label').value();
background('#99d8d0');
textSize(32);
textAlign(LEFT);
text("Adding training data for " + label, 10, 50);
} else {
background(0);
}
if(displayResults){
background('#99d8d0');
textSize(72);
textAlign(LEFT);
text(cresult, width/2-25, height/2);
}
}