How to build your first robot - Part II - navigation

Hey, cool you may have found

Hey, cool you may have found an easter egg in my code :smiley:

Noā€¦ the way it works is that I am playing around, and sometimes I make something that I figure others would have interest in. Then I try to strip it down to the essentials, omit all irelevant parts etcā€¦ and then go public with it.

Aparently there was some left over with this variable, some function that I wanted to delete but not got rid of 100%ā€¦ but you can just omit it.

Actually you should be proud of yourself, as I think perhaps some 50 other people, at least have downloaded and used the codeā€¦ and you are the first one to realize this :slight_smile:

----- UPDATE

Now I had a look at the code, and what you say? There is plenty of use of the variable. The only thing wrong, as I see it just now is that it may overflow at some pointsā€¦ But it should not matter for the function of it.

As I read it (though I may be wrong) it is a variable used in a set of rules that ensures that the robot does not start spinning around for ever if it is placed in an open space.

You see, many beginners robots only avoid obstacles, but the robot using this code will also be curious, and find doorways etc. And so if ā€œeverything looks interestingā€ for it, it should not go crazy, but take a dessicionā€¦ and that is a part of what that variable should do.

However, Try to omit it everywhere, it will be a cool way to learn.

Just write ā€™ before every line / routine it is used in, and see what happens.

Try to see how much of the code you can omit and still get a working robot! That will be a great way to see what the parts does, and how :slight_smile:

Ā 

The problem is, that MayTurn
The problem is, that MayTurn is never initalized with a value. You only add and subtract to/from MayTurn, never assign an initial value to it to begin with. The reason it might work anyway may be because the Picaxe initializes all registers to 0 so your MayTurn has an initial value of 0, but it would have been nicer and more "clean" to have a "MayTurn = 0" line in the beginning.

Well, the code I have write

Well,

the code I have write for my robot itā€™s very comparable to yours.

I havenā€™t a code for avoiding obstacles near left and near right.But my bot bot ā€˜searchā€™ something interesting and go to it.

(interesting ? whatā€™s interesting? this is a Big questionā€¦ :wink: )

Your code is very very helpful; I think everyone must read you code carefully and after write its own program.

What I have notice is that MayTurn is used correctly but never initialized. Probably all registers are set to zeros and everything works, but itā€™s not correct/clean.

Also itā€™s never resetted to a start value:

look the subroutine ForwardLeft_middle:

If MayTurn < MaxNumberTurns then
MayTurn = MayTurn + 1
end if

Ok, what happen if MayTurn values reach 5 (==MaxNumberTurns) ? It never change to another value, isnā€™t it?

And look the subroutine Sharp_R:

Sharp_R:
If MayTurn > 0 then
ā€¦
end if
return


So robot EVER turn.

Ā 

As written to jka above, it

As written to jka above, it is NOT an error that I do not ā€œinitializeā€ the variable, god forbid! :slight_smile:

When that is said, I agree, there may be a ghost in the machine in the program - I guess when tidying up the code for public release I never got 100% finished with that partā€¦ but then again, it leaves up some fun for you :slight_smile:

You should have a look at the red/green illustration on this project to understand what I mean by ā€œinterestingā€.

Uh, no I disagree 100%!To me

Uh, no I disagree 100%!

To me the art is really using the chip, not to make long code. I have made many much "worse tricks" than this in my codes, I like it that way, but I guess it is a matter of taste :slight_smile:

Say instead of writing

-------

if bit0 = 1 then
bit0 = 0
else
bit0 = 1
end if

-------

I simply write

-------

inc bit0

-------

Which gives 100% the same result in every way!

Though it may be "ugly" to some, I myself find that to be more correct. The variable overflows, I know it, and I use it.

I find that setting a variable to 0 in the beginning of the code is a mess, absolutely pointless. It IS 0!

But I understand those who like to do it "the hard & correct way", it is just not the way I think, and not the way I like it, I feel more like I am mastering the chip & language the way I do it :slight_smile:

Uh, just read a little more

Uh, just read a little more through the code :slight_smile: THere are several little things that could be trimmed. But heck, it works :smiley:

Perhaps one day I will make the nicer version. This was made very fast :slight_smile:

I donā€™t know if at power on,

I donā€™t know if at power on, all register are set to zero. If itā€™s so, you are right, itā€™s not an error.

And I agree with you: if you know how it works, then use it!

Anyway, nice code, nice tutorial and nice robot fritsl!

Ā 

Ā 

Well, I would have written
Well, I would have written bit0 = !bit0. But Iā€™m using C, not BASIC. Is it documented that the PICAXE initializes everything to 0? If not, then itā€™s dangerous to just assume that your variable is 0. Working as a programmer, you learn that it is not a good idea to assume that a variable has some specific value. If you want solid code, you have to initialize your code to a known state and you have to test the state of your program, not just assume that something is as you think, because often it isnā€™t

It would be interesting if
It would be interesting if variables where not set to zero on "boot", but they are, which is logic to me. However, it would be cool if there where some sort of internal registers with self-writable memory, that would open up new posebilities, and you are right, then one should for sure always initialize everything in the beginning of the code, of course :slight_smile:

When programming PICs in C,
When programming PICs in C, for example, there is no guarantee that your variables are 0.

But this is basic code, not
But this is BASIC code, not C

If is stated somewhere that
If is stated somewhere that initial value is x, everything itā€™s ok.

Else, starting value is undefined. The chance that is zero is very high but remeber: variables are memory positions and if not initialized you can say nothing about its value.

I think the major problem is that when MayTurn reach MaxNumberTurns value never change,

but the BIGGEST problem is that no one read carefully your (nice and very helpful) code ;))

Ā 

Thanks for the nice words

Thanks for the nice words :slight_smile:

However, to end this silly talk once and for all:

Variables in the Picaxe are stored in RAM.

All RAM information is lost when the microcontroller is reset. Hence, all variables are set to a value of 0.

That is affirmative, and 100% sure, you can rely on it, that is how it works.

And so it is ONLY because you may think it looks nice, that you should ā€œresetā€ any variables to 0 before you do anything else with itā€™s array or value :slight_smile: Also, it could be used, if you want to write cross-platform code etc. But when using the Picaxe with the Picaxe basic, there is no reason to initialize any variables to a value of zeroā€¦ because that is what they are initialized with upon reset!

(End of Story)

Sorry but where is the code?

Sorry but where is the code? I donĀ“t find itā€¦

Just below the main imageā€¦
Just below the main imageā€¦ When you are logged in :slight_smile:

I Hope I Covered Everythingā€¦

Not meaning to bring an old debate back to life here but as a new member I would like my chance to input having missed out at the appropriate time.

(this is really for people who are donā€™t know how a compiler works and I hope it helps =) )

A compiler reads your high-level code (that being your coded program) and churns out object code that your chipset can understand. Object code is a version of assembly language that your computer (or microcontroller) can understand, it looks a bit like this: 10110000 01100001ā€¦ (Forgive me for my binary just isnā€™t what it used to be so I copied this code from somewhere online hahaā€¦)

This, translated into assembly, gives : MOV AL, 61h

Which means to you and me : initialize something (i.e. a variable representing my current level of tiredness) to the hexidecimal value 0x61 (which would probably be quite accurate).

Ā 

So the basic rundown,

Highlevel Code --> Assembly Language --> Machine Code (Object Code)

Ā 

Whatā€™s the importance of this I hear you say?? aha well here is the interesting and in some cases rather annoying partā€¦

Being a games programmer I have dealt with many different compilers and many different languages and I just want to state that although fritsl is 100% completely correct (and I think I can explain why in a minute) I can understand where jka is coming from as many times I have made this mistake (most of my work is done in languages based on C).

When the compiler does itā€™s job and makes our nice machine code it will (and only in the case of a few compilers) take all the variables in your program and pre initialise them to zero in your machine code for you. And yes obviously PICAXE BASIC is one of those few compilers, therefore, if you initialise variables to zero yourself in PICAXE BASIC, it makes your program look a bit like this (in a human understanding instruction set called psuedo-code) :

Ā 

Make a variable called ā€˜WASTEā€™ and give it the value 0
Make a variable called ā€˜WASTEā€™ and give it the value 0

Make a variable called ā€˜OFā€™ and give it the value 0
Make a variable called ā€˜OFā€™ and give it the value 0

Make a variable called ā€˜TIMEā€™ and give it the value 0
Make a variable called ā€˜TIMEā€™ and give it the value 0

Ā 

And now I have just shown you all how to double the length of your code :wink:

Seriously though, I would always recomend initializing to zero unless you know your compiler does it for you!!! It is good code practice and will (in some REALLY annoying cases) give you back a few extra days of your life you wouldā€™ve other-wise wasted

Another similar mistake is thinking that all arrays in all languages start at 0. Boy-oh-boyā€¦

Ā 

Conclusion!!!

In PICAXE (and other microcontrollers) save your space for code and dont init accumulators but in most other applications it canā€™t hurt.

Ā 

ā€œUnfortunately, you learn from your mistakesā€

Hmmmā€¦

Ok after reading that back I hope that helps some-one. As usual with me, Iā€™m good at explaining things to myself and not so much to other people. Despite that I was wondering if I should make that into a seperate blog as there arenā€™t many blogs which help the programming side of robotics (compared to blogs about building robots).

Ā 

ā€œUnfortunately, you learn from your mistakesā€

Gear Motor
Can i Know what is the model of the gear motor? hope u dont get annoyed :slight_smile:

I would assume they are the

I would assume they are the same ones in the start here project.

http://www.hobbyengineering.com/H1415.html

Just to make things clear before starting

What is the use of the Yellow LED down the SRF05?

If i use This servo motor http://www.hvwtech.com/products_view.asp?ProductID=862 shall i change the resistor?

Does the SRF05 comes with the wires and pins for the connection?

Ā 

Im From Asia and shipping cost more than the items so i like to order them all at once thank you for your helpā€¦

Ā 

Im soooo excited to do this hope u can reply soon :slight_smile:

Ā 

and thank you for bearing with meā€¦