View Full Version : Lego Robotics
crenn
10-23-2007, 10:47 PM
If anyone is curious what I'm doing, this may help:
int pc=0;
void motcon(int a, int b, int c)
{
if(a==1 || b==1)
{
if(a==1 && b==1)
{
motor(0,c);
motor(1,c);
}
if(a==1 && b==0)
{
motor(0,c);
motor(1,0);
}
if(a==0 && b==1)
{
motor(0,0);
motor(1,c);
}
if(a==-1 && b==1)
{
motor(0,-c);
motor(1,c);
}
if(a==1 && b==-1)
{
motor(0,c);
motor(1,-c);
}
}
if(a==-1 || b==-1)
{
if(a==-1 && b==-1)
{
motor(0,-c);
motor(1,-c);
}
if(a==-1 && b==0)
{
motor(0,-c);
motor(1,0);
}
if(a==0 && b==-1)
{
motor(0,0);
motor(1,-c);
}
}
if(a==0 && b==0)
{
motor(0,0);
motor(1,0);
}
}
void findgoal()
{
int a;
printf("\nFound pucks.\nSearching for goal");
for( ; ; )
{
bump();
motcon(1,1,60);
a=analog(3)-analog(2);
if(analog(0)<50 || analog (1)<50)
{
if((a>0 && a<50) || (a<0 && a>-50))
{
motcon(1,1,60);
}
if(a>0 && a>50)
{
motcon(1,1,60);
}
if(a<0 && a<-50)
{
motcon(1,1,60);
}
}
if(analog(5)==255 || analog(6)==255)
{
ao();
break;
}
}
for( ; ; )
{
a=analog(3)-analog(2);
bump();
if(analog(5)>200 && analog(6)<200)
{
motcon(0,-1,60);
}
if(analog(5)<200 && analog(6)>200)
{
motcon(-1,0,60);
}
if(analog(5)>200 && analog(6)>200)
{
if(analog(3)<50 || analog(2)<50)
{
if((a>0 && a<50) || (a<0 && a>-50))
{
motcon(-1,-1,60);
msleep(300L);
motcon(0,0,100);
fire();
break;
}
if(a>0 && a>50)
{
motcon(-1,-1,60);
msleep(300L);
motcon(0,0,100);
fire();
break;
}
if(a<0 && a<-50)
{
motcon(-1,-1,60);
msleep(300L);
motcon(0,0,100);
fire();
break;
}
}
}
}
}
void fire()
{
//Function is designed to fire pucks when on the conveyor belt
printf("\nFiring pucks...\n");
motor(2,-100);
msleep(500L);
motor(2,0);
motor(3, 100);
msleep(1500L);
motor(2,100);
msleep(10000L); //Change depending on average time it takes to move pucks
ao();
pc=0;
}
void findpuck()
{
printf("\nFinding pucks...\n%d pucks found", pc);
motcon(-1,-1,100);
bump();
if(analog(4)>10 && analog(4)<25 && analog(1)<50)
{
motcon(-1,-1,60);
msleep(250L);
motcon(0,0,0);
if(analog(4)>10 && analog(4)<25 && analog(1)<50)
{
motcon(1,1,60);
msleep(750L);
motcon(1,-1,60);
msleep(500L);
}
}
if(analog(4)>100 && analog(4)<125 && analog(1)>50)
{
motcon(-1,-1,60);
msleep(250L);
motcon(0,0,0);
if(analog(4)>100 && analog(4)<125 && analog(1)>50)
{
motcon(1,1,60);
msleep(750L);
motcon(1,-1,60);
msleep(500L);
}
}
if(analog(4)<10 && analog(1)<50)
{
puppuck();
}
if(analog(4)<50 && analog(1)>50)
{
puppuck();
}
if(pc==3)
{
findgoal();
}
}
void bump()
{
if(digital(7)==1 || digital(8)==1)
{
if(digital(7)==1 && digital(8)==1)
{
motcon(1,1,60);
msleep(1000L);
motcon(1,-1,100);
msleep(250L);
}
if(digital(7)==1)
{
motcon(1,1,60);
msleep(1000L);
motcon(-1,1,100);
msleep(250L);
}
if(digital(8)==1)
{
motcon(1,1,60);
msleep(1000L);
motcon(1,-1,100);
msleep(250L);
}
}
if(digital(9)==1)
{
motcon(-1,-1,100);
msleep(1000L);
motcon(1,-1,100);
msleep(250L);
}
}
void puppuck()
{
printf("Picking up puck...\n");
motor(2,100);
msleep(500L);
motcon(-1,-1,60);
msleep(750L);
motcon(0,0,0);
msleep(1000L);
motor(2,0);
pc=pc+1;
}
void main()
{
while(!start_button())
{
printf("Press Start");
}
while(!stop_button())
{
findpuck();
}
}
I should mention this code hasn't been finished and is still under development. I'll give more information another time.
NightrainSrt4
10-23-2007, 11:46 PM
Been staring at that code for like 10 minutes. edit: by the time i got through this post it was almost 50 minutes lmao. Looks good. I don't know C++ so I can't help you with everything, but I do know a decent amount of Java which is close enough.
A few things I noticed. Don't take any of this for much of anything. Just taking this as practice for me.
1.) Your first if statements seem a little disorganized to me. I just would have organized them a little different, but thats probably just the way I am used to doing things. If it works then great! Just looks that way to me for some reason.
2.) I don't know if the value can ever assume 200 but if analog 5 and 6 can assume a value of 200, I don't see anything for that value. Completely ignore this if what your doing can't get a value of 200, or I missed it.
3.) This might just be the java coming out here, because i don't know how C++ calls on methods, but... Are all those other methods other than main going to be called from another class or something? Because you don't make any calls to the other methods in your main. This could really just be me thinking in java. In java, main is your entry point. You would have to call on those other methods to make any use of them. If you ran that program in java(assuming it understood it magically) and went to run it the only thing it would look at would be this.
void main()
{
while(!start_button())
{
printf("Press Start");
}
while(!stop_button())
{
findpuck();
}
}
ehhhh it through off the formatting and i don't have time to figure it out now. you get the point.
You would have to call on those other methods for anything to happen.
I know you said it wasn't finished. Just pointing out things I saw and trying to learn. For the most part things look very similar, structure wise. Some things look different and such but everything looks similar. So thats good. I understood most of that. yay. :banana:
Ohh, just a question. In C++ do you have to initialize all your variables? I just don't see anywhere where c is given a value. Was just trying to figure out what value that was and couldn't. Does C++ assume a value for an uninitialized variable. Like 0? or maybe 1? I would have thought if it did that then it would be 0, but you have c having positive and negative values, but not initiallized, so it wouldnt make sense if it assigned the value of 0 to it. So maybe its 1? Hmmm... idk.
Thanks for getting my mind rolling on this stuff. I have a big midterm in a couple days. Atleast this is getting me thinking in code. The whole time looking at that I was going " what would this be in java"... Gosh... I get all excited over the nerdy things. Oooo code... lmao
crenn
10-24-2007, 12:11 AM
It's not in C++. It's only C (I have limited knowledge in C++ but it's not too hard to switch once you know the structure).
1.) It's designed to run quickly, I could do else if cascades, but the compiler doesn't allow that.
2.) Analog sensors vary from 0-255 (0-FF) in value. analog(6) or analog(5) call the values and from the sensors for use. I'm having trouble with the sensors as their positioning seems to be causing problems with the light beacon (Sensors 5 and 6 are IR emitters/detectors which I'm using to find a black line)
3.) The robot knows that it has other functions ("puppuck", etc) but it will run main first. Once the start has been pressed, it will call the function "findpuck". This is the basis of my program because in the function "findpuck", it calls other functions if needed and if conditions are met. If you look at the first few lines of "findpuck":
void findpuck()
{
printf("\nFinding pucks...\n%d pucks found", pc);
motcon(-1,-1,100);
bump();
.
.
.
.
}
You'll notice it actually calls another function, "bump". However once it finds a red puck, it will call "puppuck". For me, it's easier to program in that way.
Most programming languages have similar structures, just different calls/functions.
In C and C++, you do have to initialize your variables. For example:
void main()
{
int a;
printf("%d", a);
}
It initializes a variable "a" and reserves the space for an integer number.
During when motcon is called, it gives 3 numbers. The first 2 relate to directions of the motors, the last relates to the speed. The speed is the variable "c". "c" only exists when motcon is called. It's not required for other times.
xRyokenx
10-24-2007, 12:46 AM
I have (had) the original Mindstorms kit... damn brother stole a lot of the parts to make schtuff though... seems cool though.
crenn
10-24-2007, 12:53 AM
This is a little more advanced than the mindstorms RCX unit (I have an RCXv2). It has 4 motor outputs, 7 analog, and 6 digital inputs.
Wasabisam
10-24-2007, 02:20 AM
I was using the RCX for acouple of years, doing robotic soccer. It was awesome, easy to program, because i used robolab, But you could make really complicated programs. Never did use C though, looks cool.
Sam
NightrainSrt4
10-24-2007, 09:59 AM
Ahhh thats cool. Ya I only thought C++ because of the printf. Now that I understand a bit about what your doing that makes more sense. Damn...like a week ago someone was giving a whole big set of those away on Craigslist, but I was like ehhh its probably like the other legos and I have plenty of those. Had I known it does cool stuff like that I would have grabbed it. :(
Variables: Ahhh I see now. C reserves values for the variable. by int c, ya the variable is initialized. But C can reserve values you said. That is cool. I meant that once it was initialized that you needed to have somewhere where a value is assigned to the variable. Thats what I meant, but C reserves values. That is cool. Less to worry about when originally setting up your variables maybe. Java you have to assign a value to the variable somewhere before the variable is used in the code or the compiler freaks. Thats why it seemed different.
Like you would have:
int a = 1;
or
int a;
a=1;
or even have the assigned value burried in a loop or something. It just had to be given a value before you did anything with it. You could always reassign a value to it, but it had to be assigned a value somewhere.
Thats cool. I'm learning new stuff. Thanks.
xRyokenx
10-24-2007, 12:46 PM
I'd love to buy and use the newer kit, but I haven't got the cash and what cash I do have is going to be used for other things... can't wait to see what you do with all this though, robotics kick ass.
crenn
11-01-2007, 06:26 PM
Well, my robot is competing today, and this is the code so far.
int pc=0,count=0;
void motcon(int a, int b, int c)
{
if(a==1 || b==1)
{
if(a==1 && b==1)
{
motor(0,c);
motor(1,c);
}
if(a==1 && b==0)
{
motor(0,c);
motor(1,0);
}
if(a==0 && b==1)
{
motor(0,0);
motor(1,c);
}
if(a==-1 && b==1)
{
motor(0,-c);
motor(1,c);
}
if(a==1 && b==-1)
{
motor(0,c);
motor(1,-c);
}
}
if(a==-1 || b==-1)
{
if(a==-1 && b==-1)
{
motor(0,-c);
motor(1,-c);
}
if(a==-1 && b==0)
{
motor(0,-c);
motor(1,0);
}
if(a==0 && b==-1)
{
motor(0,0);
motor(1,-c);
}
}
if(a==0 && b==0)
{
motor(0,0);
motor(1,0);
}
}
void findgoal()
{
int a,b;
printf("\nFound pucks.\nSearching for goal");
for( ; ; )
{
bump();
a=analog(2); //left
b=analog(3); //right
if(analog(1)>10)
{
if((analog(5)>230) || (analog(6)>230))
{
ao();
break;
}
if(a<b)
{
motcon(1,0,100);
}
if(a>b)
{
motcon(0,1,100);
}
msleep(50L);
}
if(analog(1)<10)
{
motcon(-1,-1,100);
msleep(3000L);
motcon(0,0,100);
}
if(a<10 && b<10)
{
motcon(-1,1,100);
msleep(700L);
motcon(0,0,100);
}
}
for( ; ; )
{
bump();
motcon(0,0,0);
if((analog(5)>230) && (analog(6)>230))
{
motcon(-1,-1,60);
msleep(2000L);
motcon(0,0,100);
fire();
break;
}
if((analog(5)>230) && (analog(6)<230))
{
motcon(1,0,60);
msleep(400L);
motcon(0,0,0);
}
if((analog(5)<230) && (analog(6)>230))
{
motcon(0,1,60);
msleep(400L);
motcon(0,0,0);
}
}
}
void fire()
{
//Function is designed to fire pucks when on the conveyor belt
printf("\nFiring pucks...\n");
motor(2,-60);
msleep(500L);
motor(2,0);
motor(3,100);
msleep(1500L);
motor(2,30);
msleep(8000L); //Change depending on average time it takes to move pucks
ao();
pc=0;
count=0;
}
void findpuck()
{
printf("\nFinding pucks...\n%d pucks found", pc);
motcon(-1,-1,100);
motor(2,0);
bump();
if(analog(4)>60 && analog(4)<78 && analog(1)<50)
{
motcon(-1,-1,60);
msleep(300L);
motcon(0,0,0);
if(analog(4)>60 && analog(4)<78 && analog(1)<50)
{
motcon(1,1,60);
motor(2,30);
msleep(750L);
motcon(1,-1,60);
msleep(500L);
}
}
if(analog(4)>100 && analog(4)<125 && analog(1)>50)
{
motcon(-1,-1,60);
msleep(300L);
motcon(0,0,0);
if(analog(4)>100 && analog(4)<125 && analog(1)>50)
{
motcon(1,1,60);
motor(2,30);
msleep(750L);
motcon(1,-1,60);
msleep(500L);
}
}
if(analog(4)<15 && analog(1)<50)
{
puppuck();
}
if(analog(4)<50 && analog(1)>50)
{
puppuck();
}
if(pc==3)
{
findgoal();
}
}
void bump()
{
if(digital(7)==1 || digital(8)==1)
{
if(digital(7)==1 && digital(8)==1)
{
motor(2,30);
motcon(1,1,60);
msleep(1000L);
if((analog(2))<(analog(3)))
{
motcon(1,0,100);
}
if((analog(2))>(analog(3)))
{
motcon(0,1,100);
}
msleep(350L);
motcon(1,-1,100);
msleep(450L);
motor(2,0);
}
if(digital(7)==1)
{
motor(2,30);
motcon(1,1,60);
msleep(1000L);
if((analog(2))<(analog(3)))
{
motcon(1,0,100);
}
if((analog(2))>(analog(3)))
{
motcon(0,1,100);
}
msleep(350L);
motcon(-1,1,100);
msleep(450L);
motor(2,0);
}
if(digital(8)==1)
{
motor(2,30);
motcon(1,1,60);
msleep(1000L);
if((analog(2))<(analog(3)))
{
motcon(1,0,100);
}
if((analog(2))>(analog(3)))
{
motcon(0,1,100);
}
msleep(350L);
motcon(1,-1,100);
msleep(450L);
motor(2,0);
}
}
if(digital(9)==1)
{
motor(2,30);
motcon(-1,-1,100);
msleep(1000L);
if((analog(2))<(analog(3)))
{
motcon(1,0,100);
}
if((analog(2))>(analog(3)))
{
motcon(0,1,100);
}
msleep(350L);
motcon(1,-1,100);
msleep(450L);
motor(2,0);
}
if((((analog(6))>140) && ((analog(6))<160)) || (((analog(5))>140) && ((analog(5))<160)))
{
while(analog(1)>15)
{
motcon(1,-1,100);
}
motcon(1,1,100);
if((analog(5)>230) || (analog(6)>230))
{
msleep(750L);
motcon(1,-1,60);
msleep(350L);
motcon(0,0,100);
}
}
}
void puppuck()
{
printf("Picking up puck...\n");
motor(2,100);
msleep(500L);
motcon(-1,-1,60);
msleep(750L);
motcon(0,0,0);
msleep(1000L);
motor(2,0);
pc=pc+1;
count=0;
}
void time()
{
msleep(50L);
count++;
if((count>=600) && (pc>0))
{
findgoal();
}
}
void main()
{
printf("Press Start\n");
while(!start_button());
while(!stop_button())
{
findpuck();
time();
}
ao();
}
D1337
11-01-2007, 07:35 PM
video/pictures of competition?
crenn
11-01-2007, 08:50 PM
I'll have a video of the competion later. I have a good miniDV camera with me. I'll be recording most matches.
btw, the competition is in about 40-50 minutes.
crenn
11-01-2007, 11:01 PM
WE'RE IN THE FINALS!
Wasabisam
11-01-2007, 11:42 PM
Congrats :up: can't wait to see the video of it or pictures :)
Good luck with the finals
Sam
crenn
11-02-2007, 05:17 AM
Sadly, the finals didn't go well. I wasn't able to diagnose or retry, but I believe I had a handyboard issue (the handyboard is the controller for the robot). I know it was not my code as that part of the code, I had not touched.
So in the first rounds of the finals, I lost by default.
Oh..... it just occurred to me, part of what happened. My robot found the white line (it didn't seem to be picking it up) and was heading back to the black line when I grabbed it, I fixed it (it broke itself) and put it behind the black line. Part of the code triggers when it sees the black line, and that's what happened. 1 sec after it saw the black line it started to actively seek the light again and looking for the black line (which was behind it). However it still seems there was another problem which I'll have to check by slowly going through the video and code. I'll let you guys know what I find. I'll also be showing you guys videos and photos when I've finished.
rendermandan
11-02-2007, 09:41 AM
I can't believe nobody has posted this yet.
:pics:
LOL. I couldn't resist!
crenn
11-02-2007, 07:07 PM
Several pictures showing the robot the monday before the competition: http://i3.photobucket.com/albums/y81/crenn/Robot/Lego/Weekbefore/th_DSC00320.jpg (http://i3.photobucket.com/albums/y81/crenn/Robot/Lego/Weekbefore/DSC00320.jpg) http://i3.photobucket.com/albums/y81/crenn/Robot/Lego/Weekbefore/th_DSC00319.jpg (http://i3.photobucket.com/albums/y81/crenn/Robot/Lego/Weekbefore/DSC00319.jpg) http://i3.photobucket.com/albums/y81/crenn/Robot/Lego/Weekbefore/th_DSC00318.jpg (http://i3.photobucket.com/albums/y81/crenn/Robot/Lego/Weekbefore/DSC00318.jpg) http://i3.photobucket.com/albums/y81/crenn/Robot/Lego/Weekbefore/th_DSC00317.jpg (http://i3.photobucket.com/albums/y81/crenn/Robot/Lego/Weekbefore/DSC00317.jpg) http://i3.photobucket.com/albums/y81/crenn/Robot/Lego/Weekbefore/th_DSC00316.jpg (http://i3.photobucket.com/albums/y81/crenn/Robot/Lego/Weekbefore/DSC00316.jpg)
http://i3.photobucket.com/albums/y81/crenn/Robot/Lego/Weekbefore/th_DSC00314.jpg (http://i3.photobucket.com/albums/y81/crenn/Robot/Lego/Weekbefore/DSC00314.jpg) http://i3.photobucket.com/albums/y81/crenn/Robot/Lego/Weekbefore/th_DSC00313.jpg (http://i3.photobucket.com/albums/y81/crenn/Robot/Lego/Weekbefore/DSC00313.jpg) http://i3.photobucket.com/albums/y81/crenn/Robot/Lego/Weekbefore/th_DSC00310.jpg (http://i3.photobucket.com/albums/y81/crenn/Robot/Lego/Weekbefore/DSC00310.jpg) http://i3.photobucket.com/albums/y81/crenn/Robot/Lego/Weekbefore/th_DSC00309.jpg (http://i3.photobucket.com/albums/y81/crenn/Robot/Lego/Weekbefore/DSC00309.jpg) http://i3.photobucket.com/albums/y81/crenn/Robot/Lego/Weekbefore/th_DSC00308.jpg (http://i3.photobucket.com/albums/y81/crenn/Robot/Lego/Weekbefore/DSC00308.jpg)
http://i3.photobucket.com/albums/y81/crenn/Robot/Lego/Weekbefore/th_DSC00307.jpg (http://i3.photobucket.com/albums/y81/crenn/Robot/Lego/Weekbefore/DSC00307.jpg) http://i3.photobucket.com/albums/y81/crenn/Robot/Lego/Weekbefore/th_DSC00306.jpg (http://i3.photobucket.com/albums/y81/crenn/Robot/Lego/Weekbefore/DSC00306.jpg)
A small animation of it firing:
http://i3.photobucket.com/albums/y81/crenn/Robot/Lego/Weekbefore/th_firingsmall.gif (http://i3.photobucket.com/albums/y81/crenn/Robot/Lego/Weekbefore/firingsmall.gif)
EDIT: Why can't I put it into a grid?
Wasabisam
11-02-2007, 09:11 PM
Looks like a cool robot. I think i might of missed it in this thread, but what is your robot competing in? I saw the puck so im thinking hockey, correct me if im wrong.
Sam
crenn
11-02-2007, 10:13 PM
It will become clear when the video is shown.
crenn
11-03-2007, 01:45 AM
1st Match:
http://i3.photobucket.com/albums/y81/crenn/Robot/Lego/Videos/th_1stmatch.jpg (http://s3.photobucket.com/albums/y81/crenn/Robot/Lego/Videos/?action=view¤t=1stmatch.flv)
I have more on my camera.
EDIT: btw, it's only 5 minutes, there is a minute missing.
D1337
11-03-2007, 02:02 AM
you totally raped his ass. Even though he started to cross the black tape, which was supposed to be a boundary?
crenn
11-03-2007, 02:10 AM
There was no rules against crossing the black line, however you had to be behind the black line when firing.
btw, during that match, I shot 5 red pucks and 1 blue puck into the goal (it picked up a blue puck by accident).
Powered by vBulletin® Version 4.2.1 Copyright © 2025 vBulletin Solutions, Inc. All rights reserved.