// ---------------------------------------------------------------------------------------------------------------- // Sensor // ---------------------------------------------------------------------------------------------------------------- class Sensor { // variables int i_mysensorType, i_mysensorRelation; float f_myinputX, f_myinputY, f_myoutputX, f_myoutputY, f_myneckLength, f_mysensorReading = 0, f_myRotation = 0; color c_myColor; // Constructor (input sensor head coordinates, length of neck, coordinates for the wire end, what type of stimulus it can read, what is the sensor relation [positive or negative]) Sensor (float f_inputX, float f_inputY, float f_neckLength, float f_outputX, float f_outputY, int i_sensorType, int i_sensorRelation) { // take user values and put into class variables f_myinputX = f_inputX; f_myinputY = f_inputY; f_myneckLength = f_neckLength; f_myoutputX = f_outputX; f_myoutputY = f_outputY; i_mysensorType = i_sensorType; i_mysensorRelation = i_sensorRelation; } // ---------------------------------------------------------------------------------------------------------------- UPDATE METHOD (usually called every time draw is calculated) void update() { // choose approriate color depending on sensortype if (i_mysensorType == 1) { c_myColor = color(100, 100, 100); } if (i_mysensorType == 2) { c_myColor = color(200, 100, 100); } // stroke color depends on sensor type, and the alpha depends on the sensor reading noFill(); stroke(c_myColor, f_mysensorReading*3+20); // draw the sensor relationship as + or - pushMatrix(); translate(f_myinputX, f_myinputY); rotate(radians(f_myRotation)); rectMode(CENTER); if (i_mysensorRelation == 1) { rect(0, -10, 5, .1); rect(0, -10, .1, 5); } else { rect(0, -10, 5, .1); } // draw the rest of the sensor // sensor tips (usless cosmetic effect) line(0, 0, 5, -5); line(0, 0, -5, -5); // sensor neck line(0, 0, 0, 0+f_myneckLength); // sensor wire line(0, 0+f_myneckLength, f_myoutputX, f_myoutputY); popMatrix(); } // every variable in this class has a method that can read it and likewise change it // ---------------------------------------------------------------------------------------------------------------- SENSOR HEAD COORDINATES METHODS // get sensor head x coordinate float getlocationinX() { return f_myinputX; } // get sensor head y coordinate float getlocationinY() { return f_myinputY; } // set sensor head x coordinate void setlocationinX(float f_newinputX) { f_myinputX = f_newinputX; } // set sensor head y coordinate void setlocationinY(float f_newinputY) { f_myinputY = f_newinputY; } // ---------------------------------------------------------------------------------------------------------------- WIRE END COORDINATES METHODS // get wire end x coordinate float getlocationoutX() { return f_myoutputX; } // get wire end y coordinate float getlocationoutY() { return f_myoutputY; } // set wire end x coordinate void setlocationoutX(float f_newoutputX) { f_myoutputX = f_newoutputX; } // set wire end y coordinate void setlocationoutY(float f_newoutputY) { f_myoutputY = f_newoutputY; } // ---------------------------------------------------------------------------------------------------------------- MORE GET/SET METHODS // get sensor type int getsensorType() { return i_mysensorType; } // set sensor type void setsensorType(int i_newsensorType) { i_mysensorType = i_newsensorType; } // get sensor relationship int getsensorRelation() { return i_mysensorRelation; } // set sensor relationship void setsensorRelation(int i_newsensorRelation) { i_mysensorRelation = i_newsensorRelation; } // get neck length float getneckLength() { return f_myneckLength; } // set neck length void setneckLength(float f_newneckLength) { f_myneckLength = f_newneckLength; } // ---------------------------------------------------------------------------------------------------------------- IMPORTANT METHODS // get the sensor reading of all stimulus that this sensor can read float getsensorReading() { // reset previous sensor reading f_mysensorReading = 0; // find all stimulus for (int i = G_totalStimuli.sizeStimulus()-1; i >= 0; i--) { // check if current stimulus matches sensor type if (i_mysensorType == G_totalStimuli.getstimulusType(i)) { // calculate value of current stimulus depending on distance from stimulus and strength of stimulus f_mysensorReading += (G_totalStimuli.getstimulusStrength(i))/dist(f_myinputX, f_myinputY, G_totalStimuli.getlocationX(i), G_totalStimuli.getlocationY(i)); //print(f_mysensorReading + "\n"); } } return f_mysensorReading; } // get angle the sensor is rotated float getRotation() { return f_myRotation; } // set the angle the sensor is rotated void setRotation(float f_newRotation) { f_myRotation = f_newRotation; } // ---------------------------------------------------------------------------------------------------------------- EXTRA METHODS // this method is here mainly for future versions where I will have a vehicle editor, but at the moment these sensors never die boolean dead() { return false; } } // ---------------------------------------------------------------------------------------------------------------- // SensorSystem // ---------------------------------------------------------------------------------------------------------------- // keeps tracks of all sensors and allows to easily add and remove sensors realtime class SensorSystem { // Variables ArrayList sensors; // Constructor (if you want you can start a sensor system with a set number of sensors) SensorSystem(int num) { sensors = new ArrayList(); for (int i = 0; i < num; i++) { sensors.add(new Sensor(50, 50, 10.0, 100, 100, 1, 1)); } } // ---------------------------------------------------------------------------------------------------------------- RUN METHOD (usually run at draw) // run all existing sensors and remove any that are 'dead' from the list void run() { // look at all sensors for (int i = sensors.size()-1; i >= 0; i--) { // store current sensor in s Sensor s = (Sensor) sensors.get(i); // update that sensor s.update(); // check if the sensor is dead if (s.dead()) { // if dead remove it from sensorsystem sensors.remove(i); } } } // ---------------------------------------------------------------------------------------------------------------- SENSOR HEAD COORDINATES METHOD (needed to specify which sensor is being worked on) // get x coordinate of sensor head i float getlocationinX(int i) { Sensor s = (Sensor) sensors.get(i); return s.getlocationinX(); } // get y coordinate of sensor head i float getlocationinY(int i) { Sensor s = (Sensor) sensors.get(i); return s.getlocationinY(); } // set x coordinate of sensor head i void setlocationinX(int i, float f_newinputX) { Sensor s = (Sensor) sensors.get(i); s.setlocationinX(f_newinputX); } // set y coordinate of sensor head i void setlocationinY(int i, float f_newinputY) { Sensor s = (Sensor) sensors.get(i); s.setlocationinY(f_newinputY); } // ---------------------------------------------------------------------------------------------------------------- WIRE END COORDINATES METHOD (needed to specify which sensor is being worked on) // get x coordinate of wire end i float getlocationoutX(int i) { Sensor s = (Sensor) sensors.get(i); return s.getlocationoutX(); } // get y coordinate of wire end i float getlocationoutY(int i) { Sensor s = (Sensor) sensors.get(i); return s.getlocationoutY(); } // set x coordinate of wire end i void setlocationoutX(int i, float f_newoutputX) { Sensor s = (Sensor) sensors.get(i); s.setlocationoutX(f_newoutputX); } // set y coordinate of wire end i void setlocationoutY(int i, float f_newoutputY) { Sensor s = (Sensor) sensors.get(i); s.setlocationoutY(f_newoutputY); } // ---------------------------------------------------------------------------------------------------------------- EXTRA GET/SET METHODS (needed to specify which sensor is being worked on) // get sensor type of sensor i int getsensorType(int i) { Sensor s = (Sensor) sensors.get(i); return s.getsensorType(); } // set sensor type of sensor i void setsensorType(int i, int i_newsensorType) { Sensor s = (Sensor) sensors.get(i); s.setsensorType(i_newsensorType); } // get sensor relationship of sensor i int getsensorRelation(int i) { Sensor s = (Sensor) sensors.get(i); return s.getsensorRelation(); } // set sensor relationship of sensor i void setsensorRelation(int i, int i_newsensorRelation) { Sensor s = (Sensor) sensors.get(i); s.setsensorRelation(i_newsensorRelation); } // get sensor neck length of sensor i float getneckLength(int i) { Sensor s = (Sensor) sensors.get(i); return s.getneckLength(); } // set sensor neck length of sensor i void setneckLength(int i, float f_newneckLength) { Sensor s = (Sensor) sensors.get(i); s.setneckLength(f_newneckLength); } // get sensor reading of sensor i float getsensorReading(int i) { Sensor s = (Sensor) sensors.get(i); //print(s.getsensorReading() + "\n"); return s.getsensorReading(); } // get angle the sensor i is rotated float getRotation(int i) { Sensor s = (Sensor) sensors.get(i); return s.getRotation(); } // set the angle the sensor i is rotated void setRotation(int i, float f_newRotation) { Sensor s = (Sensor) sensors.get(i); s.setRotation(f_newRotation); } // ---------------------------------------------------------------------------------------------------------------- SPECIFIC ARRAYLIST MEHTHODS // fuction for adding a sensor without any input void addSensor() { sensors.add(new Sensor(50, 50, 10.0, 100, 100, 1, 1)); } // function for adding a sensor with input void addSensor(Sensor s) { sensors.add(s); } // function for removing specific sensor i void removeSensor(int i) { //Sensor s = (Sensor) sensors.get(i); sensors.remove(i); } // get the number of sensors in sensor system int sizeSensors() { return sensors.size(); } // function to tell if there are any sensors left boolean dead() { if (sensors.isEmpty()) { return true; } else { return false; } } }