Society of Robots
     | Robot Forum | Robot Tutorials | Robot FAQ |
September 02, 2010, 09:41:31 PM *
Welcome, Guest. Please login or register.
Did you miss your activation email?

Login with username, password and session length
News: March 18th - Buy an Axon or Axon II, build a great robot, and support SoR.

Robot Forum
 
   Home   Help Search Login Register  
Pages: [1] 2
  Print  
Author Topic: C++ and AVR Studio  (Read 18872 times)
0 Members and 1 Guest are viewing this topic.
MarkBrownTopic starter
Full Member
***

Helpful? 0
Offline Offline

Posts: 77



View Profile WWW
« on: August 30, 2008, 06:40:43 PM »

What programming languages can I use with AVR Studio, or to program AVR chips?  I would really like to use C++ for the classes and try...catch exception handling but I only see info on C.  I tried searching the forums for a simular quesiton, but the search only returns posts with the letter C in it, which is allot of posts.

Thank you,

Mark
Logged

It is human nature to confuse genious with insanity

http://marksproject.blogspot.com/
Webbot
Expert Roboticist
Supreme Robot
*****

Helpful? 51
Offline Offline

Posts: 1,434



View Profile WWW
« Reply #1 on: August 30, 2008, 06:49:42 PM »

I've done a tutorial on it !!  http://www.societyofrobots.com/member_tutorials/node/108

I've added quite a lot of new stuff into the library recently - so I need to get round to uploading a new version.

I just build the code using a makefile from the command line.

Remember C++ still has restrictions - you cannot use the 'new' operator so you have to create all of your instances at the beginning.
Logged

Webbot Home: http://webbot.org.uk/
WebbotLib online docs: http://webbot.org.uk/WebbotLibDocs
If your in the neighbourhood: http://www.hovinghamspa.co.uk
MarkBrownTopic starter
Full Member
***

Helpful? 0
Offline Offline

Posts: 77



View Profile WWW
« Reply #2 on: August 30, 2008, 07:02:40 PM »

So if I understand this correctly, I can use C++ to program my AVR chips, I just can't use AVR Studio to compile it right?  I will have to compile it via command line.
Logged

It is human nature to confuse genious with insanity

http://marksproject.blogspot.com/
izua
Supreme Robot
*****

Helpful? 0
Offline Offline

Posts: 682


View Profile WWW
« Reply #3 on: August 30, 2008, 09:43:29 PM »

no dude, AVR is an IDE. even if you compile it via the command line, you will use the same compiler.
unless, you use another compiler which can instantiate in-code.

as a sidenote, C++ is bad for MCUs unless you have a ton of space, speed and ram.
Logged

Check out my homepage for in depth tutorials on microcontrollers and electronics.
MarkBrownTopic starter
Full Member
***

Helpful? 0
Offline Offline

Posts: 77



View Profile WWW
« Reply #4 on: August 30, 2008, 10:07:01 PM »

So classes, inheritance, and object oriented programming is a bit to much for these chips? 

I understand what you are saying about AVR Studio just being an IDE, I was just hoping that there was a flag I could set when trying to compile through command line that AVR Studio doesn't set.

Thanks,

Mark
Logged

It is human nature to confuse genious with insanity

http://marksproject.blogspot.com/
izua
Supreme Robot
*****

Helpful? 0
Offline Offline

Posts: 682


View Profile WWW
« Reply #5 on: August 31, 2008, 12:47:32 PM »

IMO, yes, I think it's too much for an 8 bit avr.
Well, why would such an important option (C++ compatibility) would be disabled by default?

However, there are also 32 bit avrs, which run linux kernels. Try those, you can login into them, run vim (on them), and compile on them a filethat you wrote in vim.
Logged

Check out my homepage for in depth tutorials on microcontrollers and electronics.
MarkBrownTopic starter
Full Member
***

Helpful? 0
Offline Offline

Posts: 77



View Profile WWW
« Reply #6 on: August 31, 2008, 02:46:43 PM »

Since I am very stubborn, i decided to do some more research.  I found out that if the compiler is switched from avr-gcc.exe to avr-c++.exe, AVR Studio will compile the C++ code.  I am working on making some changes to my Ping code and plan to test the difference between my C and C++ code.

Thanks,

Mark
Logged

It is human nature to confuse genious with insanity

http://marksproject.blogspot.com/
sotu
Supreme Robot
*****

Helpful? 0
Offline Offline

Posts: 350


ïBB - Eye Biped Bot


View Profile WWW
« Reply #7 on: August 31, 2008, 04:12:50 PM »

What about dev-c++? Any1 who can reccomend it, or is there any better compilers for c++?

And for you who know a bit about C++ code language, is this code suppose to show a result on the screen after compiling and running it, or is it ment to be showed on a mini LCD screen on a microcontroller?:

#include <iostream>
using namespace std;

int main ()
{
    cout << "Hallo";
return 0;
}
Logged

How to build a biped bot:
MarkBrownTopic starter
Full Member
***

Helpful? 0
Offline Offline

Posts: 77



View Profile WWW
« Reply #8 on: August 31, 2008, 04:44:44 PM »

What about dev-c++? Any1 who can reccomend it, or is there any better compilers for c++?

Dev-C++ is a great compiler.  The only one I tend to like better is Microsoft's Visual C++ Express.  There is alot of great things included in it, as well as some of the best code completion found in any compiler I have used.  The reason I am not using Dev-C++ though is that as Admin has said in a different post, it is used to make applications for computers, not for microcontrollers.

Quote
And for you who know a bit about C++ code language, is this code suppose to show a result on the screen after compiling and running it, or is it ment to be showed on a mini LCD screen on a microcontroller?:

#include <iostream>
using namespace std;

int main ()
{
    cout << "Hallo";
return 0;
}
Your code will show Hallo in a command window, although depending on the compiler it could show and hide faster than you can see the results.  It is possible to get the results to a mini LCD screed as there are functions for doing this in the AVRLib files.  You could also send this to a terminal window on your computer using UART if it is setup, although you would use rprintf() instead of cout.

Mark
Logged

It is human nature to confuse genious with insanity

http://marksproject.blogspot.com/
Webbot
Expert Roboticist
Supreme Robot
*****

Helpful? 51
Offline Offline

Posts: 1,434



View Profile WWW
« Reply #9 on: August 31, 2008, 05:24:46 PM »

So if I understand this correctly, I can use C++ to program my AVR chips, I just can't use AVR Studio to compile it right?  I will have to compile it via command line.
Correct

So with the avr compiler and Notepad you have everything you need to create C++ code.

When it comes to writing the code then you may prefer an IDE that can do all the usual colour coding, class hierarchies etc etc. To help make coding quicker, easier, more visual. Note that the IDE you choose doesn't then necessarily have to be able to compile the code - you can still do that via a makefile. IMHO this is better anyway - as you can give your code to someone else and they can compile it - rather than having to require the same IDE as you.

Ignore all the comments about 'C++ is not for microcontrollers'. Anything that the C guys do, I can do in C++ and it will be no bigger and run just as fast. But even better - when you compare a C program that uses, say, 3 sonar sensors then I'll bet you my code is smaller. The only way the C guys can compete is by using 'structures' - but then they are just spending time to write in C what the C++ compiler does for you automatically. But they still cant do method overloading etc.  I've yet to get a sensible explanation from anyone as to 'why' they think 'C++ is overkill' with some coding examples!!

Unlike the Java runtime, C++ doesn't require any runtime interpreter so the code can be just as tight as it is in C - if not better. And the benefits of encapsulation etc make your code robust and re-usable.

I do ALL of my AVR software projects in C++


Logged

Webbot Home: http://webbot.org.uk/
WebbotLib online docs: http://webbot.org.uk/WebbotLibDocs
If your in the neighbourhood: http://www.hovinghamspa.co.uk
MarkBrownTopic starter
Full Member
***

Helpful? 0
Offline Offline

Posts: 77



View Profile WWW
« Reply #10 on: August 31, 2008, 06:08:37 PM »

So if I understand this correctly, I can use C++ to program my AVR chips, I just can't use AVR Studio to compile it right?  I will have to compile it via command line.

Correct

So with the avr compiler and Notepad you have everything you need to create C++ code.


In case you are interested in actually using and compiling with AVR Studio, it is possible.  If you go into AVR Studio->Project Options->Custom Options, you can uncheck the "Use WinAVR" option, which defaults to avr-gcc.exe, and specify your own compiler, like avr-c++.exe.



Quote
Ignore all the comments about 'C++ is not for microcontrollers'. Anything that the C guys do, I can do in C++ and it will be no bigger and run just as fast. But even better - when you compare a C program that uses, say, 3 sonar sensors then I'll bet you my code is smaller. The only way the C guys can compete is by using 'structures' - but then they are just spending time to write in C what the C++ compiler does for you automatically. But they still cant do method overloading etc.  I've yet to get a sensible explanation from anyone as to 'why' they think 'C++ is overkill' with some coding examples!!

Unlike the Java runtime, C++ doesn't require any runtime interpreter so the code can be just as tight as it is in C - if not better. And the benefits of encapsulation etc make your code robust and re-usable.

I do ALL of my AVR software projects in C++


I was very happy to read this.  There are so many benifits I can see to using C++ over C with this, your example of sensors is a good example.  So for now I will continue to try to do this in C++.

Thank you,

Mark
« Last Edit: August 31, 2008, 06:15:53 PM by MarkBrown » Logged

It is human nature to confuse genious with insanity

http://marksproject.blogspot.com/
sotu
Supreme Robot
*****

Helpful? 0
Offline Offline

Posts: 350


ïBB - Eye Biped Bot


View Profile WWW
« Reply #11 on: September 01, 2008, 08:06:16 AM »


Your code will show Hallo in a command window, although depending on the compiler it could show and hide faster than you can see the results. 



This is exactly what happens.

I can see a small black window popping up for less then a second, so fast i cant read what it says. Any way to fix this ?
« Last Edit: September 01, 2008, 04:33:39 PM by sotu » Logged

How to build a biped bot:
sotu
Supreme Robot
*****

Helpful? 0
Offline Offline

Posts: 350


ïBB - Eye Biped Bot


View Profile WWW
« Reply #12 on: September 01, 2008, 04:35:46 PM »

never mind  Wink just found out about the: system ("pause") ;  Grin
Logged

How to build a biped bot:
MadMax
Full Member
***

Helpful? 0
Offline Offline

Posts: 58


View Profile
« Reply #13 on: September 01, 2008, 05:08:35 PM »

Ignore all the comments about 'C++ is not for microcontrollers'. Anything that the C guys do, I can do in C++ and it will be no bigger and run just as fast. But even better - when you compare a C program that uses, say, 3 sonar sensors then I'll bet you my code is smaller. The only way the C guys can compete is by using 'structures' - but then they are just spending time to write in C what the C++ compiler does for you automatically. But they still cant do method overloading etc.  I've yet to get a sensible explanation from anyone as to 'why' they think 'C++ is overkill' with some coding examples!!

Right, to a certain point. For example, in the library you showed me, you're handling it a little ineffecient. When reading through it I saw you created a LED class with a toggle function. This means you store the current state in a variable. Sometimes you don't need this variable to store whether the pin is high or low. I agree with you that 1 bit is a very small value, and it won't affect the memory usage a lot. But it still affects memory. I would create a base LED class, and a derived ToggableLed class.

Not attacking you, just giving you ideas for your library Wink
Logged
Webbot
Expert Roboticist
Supreme Robot
*****

Helpful? 51
Offline Offline

Posts: 1,434



View Profile WWW
« Reply #14 on: September 01, 2008, 06:16:33 PM »

Ignore all the comments about 'C++ is not for microcontrollers'. Anything that the C guys do, I can do in C++ and it will be no bigger and run just as fast. But even better - when you compare a C program that uses, say, 3 sonar sensors then I'll bet you my code is smaller. The only way the C guys can compete is by using 'structures' - but then they are just spending time to write in C what the C++ compiler does for you automatically. But they still cant do method overloading etc.  I've yet to get a sensible explanation from anyone as to 'why' they think 'C++ is overkill' with some coding examples!!

Right, to a certain point. For example, in the library you showed me, you're handling it a little ineffecient. When reading through it I saw you created a LED class with a toggle function. This means you store the current state in a variable. Sometimes you don't need this variable to store whether the pin is high or low. I agree with you that 1 bit is a very small value, and it won't affect the memory usage a lot. But it still affects memory. I would create a base LED class, and a derived ToggableLed class.

Not attacking you, just giving you ideas for your library Wink

Suggest you re-do your research. The 'toggle' function for the LED class is inherited from the OUT (ie outpin pin class) it just does a bit-wise exclusive-or with the current output value. Just like the C avrlib library does. So requires 'ZERO' memory.
« Last Edit: September 01, 2008, 06:17:11 PM by Webbot » Logged

Webbot Home: http://webbot.org.uk/
WebbotLib online docs: http://webbot.org.uk/WebbotLibDocs
If your in the neighbourhood: http://www.hovinghamspa.co.uk
brijesh
Full Member
***

Helpful? 0
Offline Offline

Posts: 55


View Profile
« Reply #15 on: September 04, 2008, 09:39:59 PM »

Here is related info.

http://www.nongnu.org/avr-libc/user-manual/FAQ.html#faq_cplusplus

"When programming C++ in space- and runtime-sensitive environments like microcontrollers, extra care should be taken to avoid unwanted side effects of the C++ calling conventions like implied copy constructors that could be called upon function invocation etc. These things could easily add up into a considerable amount of time and program memory wasted. Thus, casual inspection of the generated assembler code (using the -S compiler option) seems to be warranted."

If the program is so big and complicated that OOP's methodology provides significant and real benefits, then probably 8-bit microcontroller is not the right solution.
Logged
JesseWelling
Expert Roboticist
Supreme Robot
*****

Helpful? 0
Offline Offline

Posts: 707


Only You Can Build A Robot!


View Profile
« Reply #16 on: September 05, 2008, 01:33:38 AM »

If the program is so big and complicated that OOP's methodology provides significant and real benefits, then probably 8-bit microcontroller is not the right solution.

That's a +5 insightful. But you might want to also consider that OOP is easier to grok and refactor. So using OOP in C is a Good Thing™.

Also, on many 16/32 bit processors (ala ARM and ST10) C is still widely used even though C++ would be more ideal. Due to the OS and Application being compiled into one binary, I havn't seen many ARM or ST10 projects use C++  Undecided
Logged
Webbot
Expert Roboticist
Supreme Robot
*****

Helpful? 51
Offline Offline

Posts: 1,434



View Profile WWW
« Reply #17 on: September 05, 2008, 12:19:33 PM »

Thus, casual inspection of the generated assembler code (using the -S compiler option) seems to be warranted.

Yeah - I would recommend doing it with C as well. Compilers generate code to do what we've asked them to do. Sometimes we aren't very good at optimizing what 'we' tell the compiler to do. Hence GIGO - garbage in garbage out.
Logged

Webbot Home: http://webbot.org.uk/
WebbotLib online docs: http://webbot.org.uk/WebbotLibDocs
If your in the neighbourhood: http://www.hovinghamspa.co.uk
brijesh
Full Member
***

Helpful? 0
Offline Offline

Posts: 55


View Profile
« Reply #18 on: September 05, 2008, 06:49:19 PM »

So using OOP in C is a Good Thing™.

Agree 100%. The main concepts of data abstraction/hiding and programming to "interfaces" is valid no matter what methodology one uses.
Logged
MadMax
Full Member
***

Helpful? 0
Offline Offline

Posts: 58


View Profile
« Reply #19 on: September 06, 2008, 12:50:42 PM »

Suggest you re-do your research. The 'toggle' function for the LED class is inherited from the OUT (ie outpin pin class) it just does a bit-wise exclusive-or with the current output value. Just like the C avrlib library does. So requires 'ZERO' memory.

To be honest, I didn't do any research, I'm sorry... I will shut up and RTFM in the future Wink
Logged
Admin
Administrator
Supreme Robot
*****

Helpful? 110
Offline Offline

Posts: 9,833



View Profile WWW
« Reply #20 on: September 06, 2008, 04:19:13 PM »

Quote
Compilers generate code to do what we've asked them to do. Sometimes we aren't very good at optimizing what 'we' tell the compiler to do.
Anyone volunteering to write a tutorial for this? I made a post somewhere like a year ago requesting one for this. I'm not a professional programmer, more ad hoc programmer you may say . . . 'it works? ok, finished'

I'm sure 95% of the people on this forum are the same . . .
Logged

Webbot
Expert Roboticist
Supreme Robot
*****

Helpful? 51
Offline Offline

Posts: 1,434



View Profile WWW
« Reply #21 on: September 06, 2008, 07:45:16 PM »

Admin: do you mean a tutorial on programming from the ground up? Optimization etc?
Yeah - I could be up for it
Logged

Webbot Home: http://webbot.org.uk/
WebbotLib online docs: http://webbot.org.uk/WebbotLibDocs
If your in the neighbourhood: http://www.hovinghamspa.co.uk
Admin
Administrator
Supreme Robot
*****

Helpful? 110
Offline Offline

Posts: 9,833



View Profile WWW
« Reply #22 on: September 06, 2008, 07:55:24 PM »

Not a tutorial on programming (there are a million out there), but a tutorial on optimizing code.

Basically a tutorial that will make my code optimal like your code Tongue
Logged

Ro-Bot-X
Contest Winner
Supreme Robot
****

Helpful? 19
Offline Offline

Posts: 1,282


Current project: MiniEric robot.


View Profile
« Reply #23 on: September 06, 2008, 08:18:29 PM »

Admin: do you mean a tutorial on programming from the ground up? Optimization etc?
Yeah - I could be up for it

Ohh, I would love such a tutorial!
Logged

Check out my new blog!
 SeriousRobotics
Webbot
Expert Roboticist
Supreme Robot
*****

Helpful? 51
Offline Offline

Posts: 1,434



View Profile WWW
« Reply #24 on: September 10, 2008, 07:08:52 PM »

Not a tutorial on programming (there are a million out there), but a tutorial on optimizing code.

Basically a tutorial that will make my code optimal like your code Tongue

Ok - have been thinking about topics. So the broad brush is as follows:-

1. Optimisation - turned either: off, for speed, for size - and what it does in general terms
2. Compiler directives: #define, #ifdef etc - pros and cons
3. Keywords like: volatile, register, const - what do they mean and how do they effect compiler output
4. Defining code in .h files versus .c files - pros and cons
5. How to reduce your code by using structures without having to go to C++. Packing structures.
6. The dangers of multi-threading / interrupts - how to avoid issues
7. abstracting it via the makefile

Anyone got any comments / things to add?
Logged

Webbot Home: http://webbot.org.uk/
WebbotLib online docs: http://webbot.org.uk/WebbotLibDocs
If your in the neighbourhood: http://www.hovinghamspa.co.uk
Ro-Bot-X
Contest Winner
Supreme Robot
****

Helpful? 19
Offline Offline

Posts: 1,282


Current project: MiniEric robot.


View Profile
« Reply #25 on: September 10, 2008, 10:56:47 PM »

Yes, the use of pointers.
Logged

Check out my new blog!
 SeriousRobotics
stan
Jr. Member
**

Helpful? 0
Offline Offline

Posts: 28



View Profile
« Reply #26 on: September 11, 2008, 12:09:30 AM »

now the stupid question can the Axon handle c++ Grin
Logged

I have a brain tumor and whats your excuse?
Webbot
Expert Roboticist
Supreme Robot
*****

Helpful? 51
Offline Offline

Posts: 1,434



View Profile WWW
« Reply #27 on: September 11, 2008, 12:20:51 AM »

Yes, the use of pointers.
Ok thanks - sort of had that in my list.
Logged

Webbot Home: http://webbot.org.uk/
WebbotLib online docs: http://webbot.org.uk/WebbotLibDocs
If your in the neighbourhood: http://www.hovinghamspa.co.uk
Webbot
Expert Roboticist
Supreme Robot
*****

Helpful? 51
Offline Offline

Posts: 1,434



View Profile WWW
« Reply #28 on: September 11, 2008, 12:23:56 AM »

now the stupid question can the Axon handle c++ Grin

The hardware doesn't matter - its whether the compiler can do it. certainly the avr gnu compiler can do it for AtMega8/168 - so can't see why the controller on the Axon would be different.
Logged

Webbot Home: http://webbot.org.uk/
WebbotLib online docs: http://webbot.org.uk/WebbotLibDocs
If your in the neighbourhood: http://www.hovinghamspa.co.uk
stan
Jr. Member
**

Helpful? 0
Offline Offline

Posts: 28



View Profile
« Reply #29 on: September 11, 2008, 01:16:37 AM »

the only reason I ask I found a c++ book in a library book store for $2 written in 2006.... courtesy of our local college kids....

Logged

I have a brain tumor and whats your excuse?
Pages: [1] 2
  Print  
 
Jump to:  


Related Topics
Subject Started by Replies Views Last post
AT Mega8 has sufficient power but AVR Studio cant see it
Electronics
HDL_CinC_Dragon 16 3247 Last post July 24, 2007, 08:35:40 PM
by HDL_CinC_Dragon
AVR Studio Error - $50 Robut
Electronics
Stragen 5 3161 Last post November 02, 2008, 11:44:06 AM
by Stragen
AVRISPMkII/AVR Studio 4 + WinVista 64
Software
Stragen 4 4324 Last post November 02, 2008, 11:49:06 AM
by Stragen
new WinAVR and AVR Studio versions
Software
Admin 7 12126 Last post May 05, 2010, 11:57:39 PM
by Admin
Powered by MySQL Powered by PHP Powered by SMF | SMF © 2006-2009, Simple Machines LLC Valid XHTML 1.0! Valid CSS!


Advertise on this Forum