Trouble with scanning pixels[]
in
Programming Questions
•
1 year ago
Hi everyone! I've begun writing a crude AI for my robot now. My robot scans the room with sonar and an Arduino sends the data back up to a computer on the robot and it slowly builds a map of the room. The sonar window starts as a blank white screen, and when it receives a distance measurement from a sonar, it draws an arc filled green with a red stroke. Below is my AI so far, but I'm running it only in a "simulated" environment until I get it more complete. Left clicking in the sonar window will draw a large green ellipse, and right clicking will draw a small red ellipse. My problem: The scanner isn't finding the green pixels, but if I set the background to solid green (background(0, 255, 0);), the scanner works. My thoughts are somehow the green ellipse is drawing green on another layer, and the scanner is still only scanning the white background, for those of you who might have worked a little with Photoshop. Do I need to be using PImage? The scanner will eventually be looking for green areas for the robot to safely move to, while staying at least 6 to 12 inches away from any red area. It will do this by giving each pixel (1 pixel = 1 square inch in real world) a rank or grade from 0 to 255 based on if the robot can move there or not. After it makes it's full scan, it will tell the robot to move to the pixel with the highest rank. I've been working on this robot for a little over a year now and has been lots of fun. I'm open to any tips too! Appreciate any answer someone can give as to why the scanner isn't finding the green pixels when I draw them.
- int [][][] myArray = new int [600][600][2];
- int drawgray;
- int SPx, SPy, SPrad;
- int AIscanx, AIscany;
- boolean SPscanreset, SPradreset;
- AIWindow aiWindow;
- int SPstartrad, SPendrad;
- float SPang, SPatan;
- int scloc;
- float scr, scg, scb;
- int rank;
- //variables already initialized by outside
- float r, g, b;
- float ardposx, ardposy, ardheading, ardcompass, ardheaddraw, ardheaddraw2;
- int mpl; //remove other mpl when merging to main System
- //end outside variables
- void setup () {
- //variables set by outside of AI
- ardposx = 0;
- ardposy = 0;
- size(600, 600);
- background(255, 255, 255);
- //end outside variables
- SPstartrad = 2;//inner start radius for scanning in inches
- SPendrad = 300;//how far out in inches to scan
- SPrad = SPstartrad;
- aiWindow = new AIWindow();
- SPscanreset = false;
- SPradreset = false;
- translate(300,300);
- loadPixels();
- }
- void draw () {
- translate(300,300);
- mpl = millis();//remove when merging
- println("start");
- if (SPscanreset == true){
- for (int i = 0; i < 600; i++) {
- for (int j = 0; j < 600; j++) {
- myArray[i][j][1] = 0;
- }
- }
- SPscanreset = false;
- }
- if (mousePressed && mouseButton == LEFT) {
- stroke(0,255,0,63); fill(0,255,0,63);
- ellipse(mouseX - 300, mouseY - 300, 50, 50);
- //loadPixels();
- }
- if (mousePressed && mouseButton == RIGHT) {
- stroke(255,0,0,63); fill(255,0,0,63);
- ellipse(mouseX - 300, mouseY - 300, 3, 3);
- //loadPixels();
- }
- //start main spiral scan
- SPrad++;//increment through radius
- if (SPrad >= SPendrad) {
- SPrad = SPstartrad;
- loadPixels();
- SPradreset = true;
- }
- SPatan = atan2(1, SPrad);
- for (SPang = 0; SPang < TWO_PI; SPang = SPang + SPatan) {//increment through angle
- SPx = int(cos(SPang) * SPrad + ardposx);
- SPy = int(sin(SPang) * SPrad + ardposy);
- if (SPx < 300 && SPx > -300 && SPy < 300 && SPy > -300) {
- if (myArray[SPx + 300][SPy + 300][1] == 0) {
- AIscan();
- }
- myArray[SPx + 300][SPy + 300][1] = 1;
- }
- }
- println("MPL: " + (millis() - mpl));//remove when merging
- println("SPrad: " + SPrad);
- println("scr: " + scr);
- println("scg: " + scg);
- println("scb: " + scb);
- println("rank: " + rank);
- println("end");
- }//end sonar draw
- void AIscan() {
- scloc = SPx + (SPy * 600);
- scloc = constrain(scloc,0,359999);
- scr = red(pixels[scloc]);
- scg = green(pixels[scloc]);
- scb = blue(pixels[scloc]);
- //decide rank value for this location
- rank = int(scg - scr);
- if (scr == 255 && scg == 255 && scb == 255) rank = 0;
- //record rank
- myArray[SPx + 300][SPy + 300][0] = rank;
- }
- public class AIWindow extends PApplet {
- Frame frame;
- int width = 600;
- int height = 600;
- //data applet init
- AIWindow () {
- width = 600;
- height = 600;
- frame = new Frame();
- frame.setBounds(0, 0, 600, 600);
- frame.add(this);
- this.init();
- frame.show();
- frameRate(30);
- frame.pack();
- }
- void setup() {
- size(600, 600);
- //frameRate(30);
- frame.setTitle("AI");
- background(255, 255, 255);
- smooth();
- }
- void draw() {
- //if (SPradreset = true) {
- for (int i = 0; i < 600; i++) {
- for (int j = 0; j < 600; j++) {
- drawgray = int(255 - myArray[i][j][0]);
- stroke(drawgray,drawgray,drawgray);
- point(i,j);
- }
- }
- //}
- SPradreset = false;
- }//end AI draw
- }//end AI window
1