181114KA
Hello!
The first goal in this project was to get the neurorobot_reboot to perform a simple autonomous “Roomba” function, ie. move around without bumping into anything. I accomplished this more or less successfully, so the code is pasted in below. One of the main issues is that I hard-coded in timings, so as the battery depletes, the code works less-well; currently (hehe), I’m looking into using motor encoders to solve this problem.
Here’s a video file of it working (will download): 181114KA_NeuralRobot_Progression
Edit 190122KA: and here’s a document I just typed up that quickly runs over different parts of the code: A Quick Rundown of Roomba
//11/14/18 //created 9/2118 by Kanwal Ahmad //not original code; this is a remix of Filename: NeuroRobot_Original_Code provided by Alex Kaiser //plus some other stuff I guess :) class Motor{ private: int pin_enable; //this is the thing that you set the speed with int pin_forward; //basically each motor has two of these pins, int pin_backward; //and you use them to reverse polarity and switch direction public: Motor(int enable, int one, int two){ pin_enable = enable; pin_forward = one; pin_backward = two; //in the old code they essentially combined these last two steps with something fancy //oh well, this is easier to understand lol } void motorSpeed(int Speed){ if(Speed > 0 || Speed == 0){ //here, I'm asking which way to set the polarity, with the idea being that I can input pos. or neg. speeds into the function digitalWrite(pin_forward, HIGH); digitalWrite(pin_backward, LOW); analogWrite(pin_enable, Speed); } else{ digitalWrite(pin_forward, LOW); digitalWrite(pin_backward, HIGH); analogWrite(pin_enable, -Speed); } } }; //Reverse Polarity protector is plugged into Pin 12 on the MEGA shield Motor right_motor(5, 42, 43); Motor left_motor(6, 44, 45); char SIDE; int right_speed; int left_speed; void setSpeeds(int bothSpeed){ right_motor.motorSpeed(bothSpeed); left_motor.motorSpeed(bothSpeed); } void turn(int bothSpeed) { right_motor.motorSpeed(bothSpeed); left_motor.motorSpeed(-bothSpeed); } //now let's start working with the sensors //set up the LongRange sensor class "Sensor" class Sensor{ private: int trigPin; int echoPin; long duration; int distance; void trigger(){ //clears the trigpin digitalWrite(trigPin, LOW); delayMicroseconds(2); digitalWrite(trigPin, HIGH); delayMicroseconds(2); digitalWrite(trigPin, LOW); } public: Sensor(int trig, int echo){ trigPin = trig; echoPin = echo; pinMode(trigPin, OUTPUT); pinMode(echoPin, INPUT); } int dist() { trigger(); //i literally floundered for the longest time BC I FORGOT THIS AAAAH //reads the echopin and returns the sound wave travel time in ms duration = pulseIn(echoPin, HIGH); //calculate the distance distance = duration*.034/2; return distance; } }; //finished setting up the class //now let's instantiate the actual sensors (yay!) //it's (trig, echo) Sensor bl(20, 21); //ie back right Sensor bm(18, 19); Sensor br(16, 17); Sensor fl(36, 37); Sensor fm(35, 34); // Sensor fr(32, 33); //out of comission void adjust(){ int tooclose = 20; int justright = 27; int toofar = 50; int goaway = 10; //since one of the front sensors is broken, I'm going to code this with the back if(bm.dist() > tooclose && bl.dist() > goaway && br.dist() > goaway) { setSpeeds(-70); } else if (bm.dist() < tooclose) { if(bl.dist() > br.dist() && bl.dist() > tooclose) { turn(-90); delay(250); } else if(br.dist() > tooclose) { turn(90); delay(250); } else if(bl.dist() < tooclose && br.dist() < tooclose) { setSpeeds(70); delay(400); } } } void readSpeed() { char SIDE = Serial.read(); //read a character that will determine which motor speed to set in next if-loops int in = Serial.parseInt(); //now read the motor speed if (SIDE == 'R'){ //R designates the right motor right_motor.motorSpeed(in); right_speed = in; } else if (SIDE == 'L') { //L designates the left motor left_motor.motorSpeed(in); left_speed = in; } else if (SIDE == 'B'){ setSpeeds(in); right_speed = in; left_speed = in; } Serial.print("Right: "); Serial.println(right_speed, DEC); //print out the new speeds (ie. check that the correct one was changed) Serial.print("Left: "); Serial.println(left_speed, DEC); Serial.println("--------------"); delay(10); } void setup() { Serial.begin(9600); Serial.println("Starting up..."); digitalWrite(40, LOW); //interruptPin } void loop() { //this is the loop that checks Serial availability and then for motor speed inputs if (Serial.available() > 0) { //check that the Serial Monitor works } adjust(); Serial.println("adjust!"); //readSpeed(); }