Components and supplies
Asuro Robot Kit by Arexx
Apps and platforms
Windows 10
Asuro Flash
ASURINO Libary
Arduino IDE
HTerm
Project description
Code
Example 1 - The "Wall Hater"
c_cpp
This code will make the Robot drive forwards until it hits a wall, then it will drive backwards a bit, turn and continue driving. Requires the Functions form "Code to Copy
1// The 'Wall-Hater': This code will make the Robot drive forwards until it hits a wall, then it will drive backwards a bit, turn and continue driving 2void loop() 3{ 4 if(!switchesPressed()){ //If no switch is pressed 5 motorSpeed(255, 255); //drive forwards full speed 6 }else{ //If a switch is pressed 7 motorSpeed(-255, -255); // revese full speed 8 delay(500); //for half a second 9 motorSpeed(255, -255); //left motor full forward, right motor full backward 10 delay(250); //pause code for half a second while turning 11 } // then continue (go to top of loop), if no switch is pressed with driving straight 12} 13
Failed Speed Test Code
c_cpp
If you want to use this code directly (not recomended) you will have to integrate it with the functions from "Code to Copy" as they are needed to make it work. I recomend to not use this code but to make your own, improved version of it.
1//set variables for the filter 2int prevMillis = 0; const float filterSetting = 0.5; 3int countR = 0; int nowFreqR = 0; int smoothFreqR = 0; boolean encoderStateR = false; boolean prevEncoderStateR = false; int angleRawR = 0; 4int countL = 0; int nowFreqL = 0; int smoothFreqL = 0; boolean encoderStateL = false; boolean prevEncoderStateL = false; int angleRawL = 0; 5 6void runFreq(){ // !IMPORTANT! call as often as possible (loop) 7 //read encoder changes //reading Code commented, done with interrupts, see below 8 //Right Side 9 /*encoderStateR = trueEncoderR(); 10 if(encoderStateR == !prevEncoderStateR){ 11 prevEncoderStateR = encoderStateR; 12 countR++; 13 angleRawR++; 14 15 } 16 //Left Side 17 encoderStateL = trueEncoderL(); 18 if(encoderStateL == !prevEncoderStateL){ 19 prevEncoderStateL = encoderStateL; 20 countL++; 21 angleRawL++; 22 }*/ 23 24 if((millis() - prevMillis) > 250){ 25 //this function is called every loop, but still will only read every 250 ms 26 //it is meant caculate a smoothd freqency value from the interrupt count 27 // here a modification if the function is not called often enough to 28 //to compensate for the excess time that has passed may be an idea 29 //Right Side 30 prevMillis = millis(); //reset the counter 31 nowFreqR = countR; //make the value from the readings the now point 32 smoothFreqR = filterSetting * nowFreqR + (1 - filterSetting) * smoothFreqR; //filter the nowFreq value (exponential filter) 33 countR = 0; 34 //Left Side //same as right side 35 nowFreqL = countL; 36 smoothFreqL = filterSetting * nowFreqL + (1 - filterSetting) * smoothFreqL; 37 countL = 0; 38 } 39} 40 41int getFreqR(){ //Simple functions to get the smoothed value as the run function gets called every loop and and does not output the 42//working with the global variables only also works 43 return smoothFreqR; 44} 45int getFreqL(){ 46 return smoothFreqL; 47} 48 49/*void setLedStatusRed(boolean onoff){ //this needs to be commented out of the original functions as PD2 becomes an interupt IN 50 if(onoff){ 51 digitalWrite(2, HIGH); 52 }else{ 53 digitalWrite(2, LOW); 54 } 55}*/ 56 57void interruptR(){ 58 countR++; 59} 60void interruptL(){ 61 countL++; 62} 63 64void setup(){ 65 //Status LED 66 //pinMode(2, OUTPUT); //pin output disabled as it becomes an interrupt in (as mentioned ^) 67 pinMode(8, OUTPUT); 68 //Interrupts 69 pinMode(2, INPUT_PULLUP); //set pin as an input 70 attachInterrupt(digitalPinToInterrupt(2), interruptL, RISING); //attach an interrupt to the rising side of the digitalIN 71 pinMode(3, INPUT_PULLUP); //same as the other one 72 attachInterrupt(digitalPinToInterrupt(3), interruptR, RISING); 73} 74 75void loop(){ 76 runFreq(); //call this fuction every loop, loop should not have any delays 77} 78
Example 2 - The "Line Follower"
c_cpp
This code uses the right light sensor to follow a line (black line on white background). Requires the Functions from "Code to Copy
1//The 'Line Follower': this code uses the right light sensor to follow a line (black line on white background) 2void loop() 3{ 4 setLedFront(true); //enables the front LED to make reading values possible. 5 Serial.println(readLineSensorR()); //prints the linesensor value to serial (read with HTerm) 6 7 if(readLineSensorR() > 200){ //if it sees white 8 motorSpeed_(0, 100); //it turns the right motor on to turn towards the line 9 }else{ //if it sees black 10 motorSpeed_(100, 0); //it turns the left motor on to turn towards the white again 11 } 12} 13
Functions to Import
c_cpp
Copy this code in your arduino sketch, then you can start writing programs for asuro. (replace the standard code) Refer to "Functions" for instructions how to use the functions
1#include <Asuro.h> 2Asuro asuro = Asuro(); 3//FUNCTIONS----------------------------------------------------------- 4int currentSpeedR = 0; 5int currentSpeedL = 0; 6 7void motorSpeedR_(int speedR){ 8 //speed input 9 analogWrite(10, abs(speedR)); 10 11 //forward/backward 12 if (speedR >= 0) { 13 digitalWrite(12, LOW); 14 digitalWrite(13, HIGH); 15 } else { 16 digitalWrite(12, HIGH); 17 digitalWrite(13, LOW); 18 } 19} 20void motorSpeedL_(int speedL){ 21 //speed input 22 analogWrite(9, abs(speedL)); 23 24 //forward/backward 25 if (speedL >= 0) { 26 digitalWrite(4, LOW); 27 digitalWrite(5, HIGH); 28 } else { 29 digitalWrite(4, HIGH); 30 digitalWrite(5, LOW); 31 } 32} 33void motorSpeed_(int L, int R){ 34 motorSpeedR_(R); 35 motorSpeedL_(L); 36} 37 38void motorSpeedR(int speedR){ 39 int speedBefore = currentSpeedR; 40 currentSpeedR = speedR; 41 42 if(speedBefore == 0){ 43 if(speedR > 0){ 44 motorSpeedR_(255); 45 delay(75); 46 }else if(speedR < 0){ 47 motorSpeedR_(-255); 48 delay(75); 49 } 50 } 51 motorSpeedR_(speedR); 52} 53void motorSpeedL(int speedL){ 54 int speedBefore = currentSpeedL; 55 currentSpeedL = speedL; 56 57 if(speedBefore == 0){ 58 if(speedL > 0){ 59 motorSpeedL_(255); 60 delay(75); 61 }else if(speedL < 0){ 62 motorSpeedL_(-255); 63 delay(75); 64 } 65 } 66 motorSpeedL_(speedL); 67} 68void motorSpeed(int L, int R){ 69 motorSpeedR(R); 70 motorSpeedL(L); 71} 72 73int readEncoderR(){ 74 return (analogRead(A0)); 75} 76int readEncoderL(){ 77 return (analogRead(A1)); 78} 79 80boolean readEncoderRdigital(){ 81 return digitalRead(A0); 82} 83boolean readEncoderLdigital(){ 84 return digitalRead(A1); 85} 86 87int readSwitchesRaw(){ 88 int Average = 0; 89 int MeasurementsToAverage = 16; 90 for(int i = 0; i < MeasurementsToAverage; ++i) 91 { 92 Average += analogRead(A4); 93 delay(1); 94 } 95 Average /= MeasurementsToAverage; 96 return(Average); 97} 98int getSwitches(){ 99 int s = readSwitchesRaw(); 100 if(s > 500){ 101 return 0; 102 }else if(s >= 50){ 103 return 1; 104 }else if(s >= 25){ 105 return 2; 106 }else if(s >= 12){ 107 return 3; 108 }else if(s >= 4){ 109 return 4; 110 }else if(s >= 2){ 111 return 5; 112 }else if(s >= 0){ 113 return 6; 114 }else{ 115 return 0; 116 } 117} 118boolean switchesPressed(){ 119 if(readSwitchesRaw() <= 700){ 120 return true; 121 }else{ 122 return false; 123 } 124} 125 126int readLineSensorR(){ 127 return analogRead(A2); 128} 129int readLineSensorL(){ 130 return analogRead(A3); 131} 132 133void setLedFront(boolean onoff){ 134 if(onoff){ 135 digitalWrite(6, HIGH); 136 }else{ 137 digitalWrite(6, LOW); 138 } 139} 140 141void setLedStatusGreen(boolean onoff){ 142 if(onoff){ 143 digitalWrite(8, HIGH); 144 }else{ 145 digitalWrite(8, LOW); 146 } 147} 148void setLedStatusRed(boolean onoff){ 149 if(onoff){ 150 digitalWrite(2, HIGH); 151 }else{ 152 digitalWrite(2, LOW); 153 } 154} 155 156//Setup--------------------------------------------------------------- 157void setup() 158{ 159 asuro.Init(); 160 Serial.begin(2400); 161 162 //left motor 163 pinMode(4, OUTPUT); 164 pinMode(5, OUTPUT); 165 pinMode(9, OUTPUT); 166 //right motor 167 pinMode(10, OUTPUT); 168 pinMode(12, OUTPUT); 169 pinMode(13, OUTPUT); 170 //encoder LEDs 171 pinMode(7, OUTPUT); 172 digitalWrite(7, HIGH); 173 //Linesensor LED 174 pinMode(6, OUTPUT); 175 //Status LED 176 pinMode(2, OUTPUT); 177 pinMode(8, OUTPUT); 178} 179 180//LOOP---------------------------------------------------------------- 181void loop(){ 182 183} 184
Example 1 - The "Wall Hater"
c_cpp
This code will make the Robot drive forwards until it hits a wall, then it will drive backwards a bit, turn and continue driving. Requires the Functions form "Code to Copy
1// The 'Wall-Hater': This code will make the Robot drive forwards until 2 it hits a wall, then it will drive backwards a bit, turn and continue driving 3void 4 loop() 5{ 6 if(!switchesPressed()){ //If no switch is pressed 7 motorSpeed(255, 8 255); //drive forwards full speed 9 }else{ //If a 10 switch is pressed 11 motorSpeed(-255, -255); // revese full speed 12 delay(500); 13 //for half a second 14 motorSpeed(255, -255); 15 //left motor full forward, right motor full backward 16 delay(250); //pause 17 code for half a second while turning 18 } // then 19 continue (go to top of loop), if no switch is pressed with driving straight 20} 21
Functions to Import
c_cpp
Copy this code in your arduino sketch, then you can start writing programs for asuro. (replace the standard code) Refer to "Functions" for instructions how to use the functions
1#include <Asuro.h> 2Asuro asuro = Asuro(); 3//FUNCTIONS----------------------------------------------------------- 4int 5 currentSpeedR = 0; 6int currentSpeedL = 0; 7 8void motorSpeedR_(int speedR){ 9 10 //speed input 11 analogWrite(10, abs(speedR)); 12 13 //forward/backward 14 15 if (speedR >= 0) { 16 digitalWrite(12, LOW); 17 digitalWrite(13, HIGH); 18 19 } else { 20 digitalWrite(12, HIGH); 21 digitalWrite(13, LOW); 22 } 23} 24void 25 motorSpeedL_(int speedL){ 26 //speed input 27 analogWrite(9, abs(speedL)); 28 29 30 //forward/backward 31 if (speedL >= 0) { 32 digitalWrite(4, LOW); 33 digitalWrite(5, 34 HIGH); 35 } else { 36 digitalWrite(4, HIGH); 37 digitalWrite(5, LOW); 38 39 } 40} 41void motorSpeed_(int L, int R){ 42 motorSpeedR_(R); 43 motorSpeedL_(L); 44 45} 46 47void motorSpeedR(int speedR){ 48 int speedBefore = currentSpeedR; 49 50 currentSpeedR = speedR; 51 52 if(speedBefore == 0){ 53 if(speedR > 0){ 54 55 motorSpeedR_(255); 56 delay(75); 57 }else if(speedR < 0){ 58 59 motorSpeedR_(-255); 60 delay(75); 61 } 62 } 63 motorSpeedR_(speedR); 64} 65void 66 motorSpeedL(int speedL){ 67 int speedBefore = currentSpeedL; 68 currentSpeedL 69 = speedL; 70 71 if(speedBefore == 0){ 72 if(speedL > 0){ 73 motorSpeedL_(255); 74 75 delay(75); 76 }else if(speedL < 0){ 77 motorSpeedL_(-255); 78 79 delay(75); 80 } 81 } 82 motorSpeedL_(speedL); 83} 84void motorSpeed(int 85 L, int R){ 86 motorSpeedR(R); 87 motorSpeedL(L); 88} 89 90int readEncoderR(){ 91 92 return (analogRead(A0)); 93} 94int readEncoderL(){ 95 return (analogRead(A1)); 96} 97 98boolean 99 readEncoderRdigital(){ 100 return digitalRead(A0); 101} 102boolean readEncoderLdigital(){ 103 104 return digitalRead(A1); 105} 106 107int readSwitchesRaw(){ 108 int Average = 109 0; 110 int MeasurementsToAverage = 16; 111 for(int i = 0; i < MeasurementsToAverage; 112 ++i) 113 { 114 Average += analogRead(A4); 115 delay(1); 116 } 117 Average 118 /= MeasurementsToAverage; 119 return(Average); 120} 121int getSwitches(){ 122 int 123 s = readSwitchesRaw(); 124 if(s > 500){ 125 return 0; 126 }else if(s >= 50){ 127 128 return 1; 129 }else if(s >= 25){ 130 return 2; 131 }else if(s >= 12){ 132 133 return 3; 134 }else if(s >= 4){ 135 return 4; 136 }else if(s >= 2){ 137 138 return 5; 139 }else if(s >= 0){ 140 return 6; 141 }else{ 142 return 143 0; 144 } 145} 146boolean switchesPressed(){ 147 if(readSwitchesRaw() <= 700){ 148 149 return true; 150 }else{ 151 return false; 152 } 153} 154 155int readLineSensorR(){ 156 157 return analogRead(A2); 158} 159int readLineSensorL(){ 160 return analogRead(A3); 161 162} 163 164void setLedFront(boolean onoff){ 165 if(onoff){ 166 digitalWrite(6, 167 HIGH); 168 }else{ 169 digitalWrite(6, LOW); 170 } 171} 172 173void setLedStatusGreen(boolean 174 onoff){ 175 if(onoff){ 176 digitalWrite(8, HIGH); 177 }else{ 178 digitalWrite(8, 179 LOW); 180 } 181} 182void setLedStatusRed(boolean onoff){ 183 if(onoff){ 184 185 digitalWrite(2, HIGH); 186 }else{ 187 digitalWrite(2, LOW); 188 } 189} 190 191//Setup--------------------------------------------------------------- 192void 193 setup() 194{ 195 asuro.Init(); 196 Serial.begin(2400); 197 198 //left motor 199 200 pinMode(4, OUTPUT); 201 pinMode(5, OUTPUT); 202 pinMode(9, OUTPUT); 203 //right 204 motor 205 pinMode(10, OUTPUT); 206 pinMode(12, OUTPUT); 207 pinMode(13, OUTPUT); 208 209 //encoder LEDs 210 pinMode(7, OUTPUT); 211 digitalWrite(7, HIGH); 212 //Linesensor 213 LED 214 pinMode(6, OUTPUT); 215 //Status LED 216 pinMode(2, OUTPUT); 217 pinMode(8, 218 OUTPUT); 219} 220 221//LOOP---------------------------------------------------------------- 222void 223 loop(){ 224 225} 226
Failed Speed Test Code
c_cpp
If you want to use this code directly (not recomended) you will have to integrate it with the functions from "Code to Copy" as they are needed to make it work. I recomend to not use this code but to make your own, improved version of it.
1//set variables for the filter 2int prevMillis = 0; const float filterSetting 3 = 0.5; 4int countR = 0; int nowFreqR = 0; int smoothFreqR = 0; boolean encoderStateR 5 = false; boolean prevEncoderStateR = false; int angleRawR = 0; 6int countL = 0; 7 int nowFreqL = 0; int smoothFreqL = 0; boolean encoderStateL = false; boolean prevEncoderStateL 8 = false; int angleRawL = 0; 9 10void runFreq(){ // !IMPORTANT! call 11 as often as possible (loop) 12 //read encoder changes //reading Code commented, 13 done with interrupts, see below 14 //Right Side 15 /*encoderStateR = trueEncoderR(); 16 17 if(encoderStateR == !prevEncoderStateR){ 18 prevEncoderStateR = encoderStateR; 19 20 countR++; 21 angleRawR++; 22 23 } 24 //Left Side 25 encoderStateL 26 = trueEncoderL(); 27 if(encoderStateL == !prevEncoderStateL){ 28 prevEncoderStateL 29 = encoderStateL; 30 countL++; 31 angleRawL++; 32 }*/ 33 34 if((millis() 35 - prevMillis) > 250){ 36 //this function is called every loop, but still will 37 only read every 250 ms 38 //it is meant caculate a smoothd freqency value from 39 the interrupt count 40 // here a modification if the function is not called often 41 enough to 42 //to compensate for the excess time that has passed may be 43 an idea 44 //Right Side 45 prevMillis = millis(); //reset the counter 46 47 nowFreqR = countR; //make the value from the readings the now point 48 smoothFreqR 49 = filterSetting * nowFreqR + (1 - filterSetting) * smoothFreqR; //filter the nowFreq 50 value (exponential filter) 51 countR = 0; 52 //Left Side //same as right 53 side 54 nowFreqL = countL; 55 smoothFreqL = filterSetting * nowFreqL + (1 56 - filterSetting) * smoothFreqL; 57 countL = 0; 58 } 59} 60 61int getFreqR(){ 62 //Simple functions to get the smoothed value as the run function gets called every 63 loop and and does not output the 64//working with the global variables only also 65 works 66 return smoothFreqR; 67} 68int getFreqL(){ 69 return smoothFreqL; 70 71} 72 73/*void setLedStatusRed(boolean onoff){ //this needs to be commented 74 out of the original functions as PD2 becomes an interupt IN 75 if(onoff){ 76 77 digitalWrite(2, HIGH); 78 }else{ 79 digitalWrite(2, LOW); 80 } 81}*/ 82 83void 84 interruptR(){ 85 countR++; 86} 87void interruptL(){ 88 countL++; 89} 90 91void 92 setup(){ 93 //Status LED 94 //pinMode(2, OUTPUT); //pin output disabled as 95 it becomes an interrupt in (as mentioned ^) 96 pinMode(8, OUTPUT); 97 //Interrupts 98 99 pinMode(2, INPUT_PULLUP); //set pin as an input 100 attachInterrupt(digitalPinToInterrupt(2), 101 interruptL, RISING); //attach an interrupt to the rising side of the digitalIN 102 103 pinMode(3, INPUT_PULLUP); //same as the other one 104 attachInterrupt(digitalPinToInterrupt(3), 105 interruptR, RISING); 106} 107 108void loop(){ 109 runFreq(); //call this fuction 110 every loop, loop should not have any delays 111} 112
Comments
Only logged in users can leave comments