go_away

Author Topic: c++ robot code  (Read 8275 times)

0 Members and 1 Guest are viewing this topic.

Offline pomprockerTopic starter

  • Supreme Robot
  • *****
  • Posts: 1,430
  • Helpful? 16
  • Sorry miss, I was giving myself an oil-job.
c++ robot code
« on: March 11, 2008, 12:41:44 AM »
I am in a month long c++ class right now and we have to chose a final project. I have 3 weeks to do it. Any ideas for robot code?

Offline benji

  • Supreme Robot
  • *****
  • Posts: 832
  • Helpful? 0
Re: c++ robot code
« Reply #1 on: March 11, 2008, 07:51:55 AM »
embedded c is somthin different than c++
you have to find yourself a microcontroller compiler that compile c++ code into machine code
,by the way c++ is about classes and objects and ... stuff you wont need when programing a bot
unless you want the bot to be connected to a computer and do thinking over there
good ol' BeNNy

Offline Trumpkin

  • Supreme Robot
  • *****
  • Posts: 1,177
  • Helpful? 5
Re: c++ robot code
« Reply #2 on: March 11, 2008, 09:01:01 AM »
It'll be hard to program a robot in C++.
Robots are awesome!

Offline bootstrap

  • Full Member
  • ***
  • Posts: 61
  • Helpful? 0
Re: c++ robot code
« Reply #3 on: March 11, 2008, 09:32:18 AM »
I am in a month long c++ class right now and we have to chose a final project. I have 3 weeks to do it. Any ideas for robot code?
Coding in C using Winavr is the best way to start.Search up for tutorials in Winavr.

Offline sdk32285

  • Supreme Robot
  • *****
  • Posts: 424
  • Helpful? 0
Re: c++ robot code
« Reply #4 on: March 11, 2008, 10:31:03 AM »
Hi

If there is a computer (laptop,etc...) on board the robot than it can be easy to use c++, it depends on what motor controllers and sensors you have on board.

You might be able to use Microsoft's robotic studio to simulate a robot for your final project (i know its not as fun as building a real robot).
Robots for Roboticists Blog - http://robotsforroboticists.com/

Offline pomprockerTopic starter

  • Supreme Robot
  • *****
  • Posts: 1,430
  • Helpful? 16
  • Sorry miss, I was giving myself an oil-job.
Re: c++ robot code
« Reply #5 on: March 11, 2008, 12:33:32 PM »
oh thats fine. I don't really have time to build one anyway, I would just rather focus on the coding since thats all that matters here.

I would definitely have to use OOP since that is the whole point of c++

Offline benji

  • Supreme Robot
  • *****
  • Posts: 832
  • Helpful? 0
Re: c++ robot code
« Reply #6 on: March 11, 2008, 04:13:34 PM »
Quote
I would definitely have to use OOP
i dont think you can find an application in robotics if thats what you aim for
good ol' BeNNy

Offline siempre.aprendiendo

  • Jr. Member
  • **
  • Posts: 32
  • Helpful? 0
Re: c++ robot code
« Reply #7 on: March 12, 2008, 03:03:03 AM »
I am in a month long c++ class right now and we have to chose a final project. I have 3 weeks to do it. Any ideas for robot code?


There is no problem about programming robotics in c++, the problem is that to support some features (exceptions, stl,...) of c++ is necessary a lot more resources (specially memory) than programming in c.

Here you can find some information about c++ restrictions while programming avr with "c++":
http://www.nongnu.org/avr-libc/user-manual/FAQ.html#faq_cplusplus (no new/delete, no exceptions,...)

BrickOS, for Lego Mindstorms RCX, does support new/delete. There is an interesting new project to program in C/C++ the new Mindstorms NXT, Lejos OSEK.

There are some pretty interesting boards, like gumstix, that support c++ easily. But they are more expensive that the average microcontroller.
« Last Edit: March 12, 2008, 03:05:22 AM by siempre.aprendiendo »

Offline pomprockerTopic starter

  • Supreme Robot
  • *****
  • Posts: 1,430
  • Helpful? 16
  • Sorry miss, I was giving myself an oil-job.
Re: c++ robot code
« Reply #8 on: March 13, 2008, 11:04:27 AM »
i'd think that it would be a good thing to have OOP in robotics.


class bodypart {

};
class wheels : public bodypart {
public:
  void rotatewheels();
};
class sensors : public bodypart {
public:
  int detect(string smell, int distance, int temp);
};


etc..

Offline benji

  • Supreme Robot
  • *****
  • Posts: 832
  • Helpful? 0
Re: c++ robot code
« Reply #9 on: March 13, 2008, 04:47:35 PM »
yea but that wont get you extra stuff in robots , maybe you feel its easier for you to write code this way
plus it would consume too much flash as tyhis depends on how your compiler transform this stuff into machine code

i dont see no reason to use c++ while you can do everything in c
good ol' BeNNy

Offline paulstreats

  • Supreme Robot
  • *****
  • Posts: 1,381
  • Helpful? 21
Re: c++ robot code
« Reply #10 on: March 13, 2008, 10:23:35 PM »
Quote
I am in a month long c++ class right now and we have to chose a final project. I have 3 weeks to do it. Any ideas for robot code?

your in the same boat as all of us.......

You can take in sensor data, but what do you actually do with it????????

Offline JesseWelling

  • Expert Roboticist
  • Supreme Robot
  • *****
  • Posts: 707
  • Helpful? 0
  • Only You Can Build A Robot!
Re: c++ robot code
« Reply #11 on: March 14, 2008, 06:45:38 AM »
I think the Player/Stage project would be your best bet for a 'Robot' project in a hurry.
http://playerstage.sourceforge.net/index.php?src=stage

Offline Webbot

  • Expert Roboticist
  • Supreme Robot
  • *****
  • Posts: 2,150
  • Helpful? 110
Re: c++ robot code
« Reply #12 on: March 18, 2008, 12:47:43 PM »
i dont see no reason to use c++ while you can do everything in c

This misses the point  ;D - you can do ALL things in ALL programming languages but it comes down to the usual stuff re encapsulation, polymorphism, familiarity and ease of use etc. I can do everything in Assembler that can be done in C - but I dont because C is easier. Therefore if stuff is easier in C++ then why not use it?

So for example in C++ you could have an abstract class that represents an LCD display with methods like 'clear the screen', 'back light on/off'. You could then create concrete classes for specific displays that implement these methods in the relevant way.

As for memory constraints - this seems to be somewhat over-egged as a recent why C++ would be bad. Normally in C++ each class has a vtable with an address offset to where the implemented method is stored. So for a class with a handfull of over-ridable methods then this would only require a handfull of values (bytes) overhead.

Consider the case of the servos. In C++ you could have one class and then instantiate two implementations where one of the motors knows that it is 'back to front' compared with the other motor. ie forward on the left servo is the same as reverse on the right servo. The rest of your code could then just say 'go forwards'. Now in C you have to flatten this code out so that all of your code that controls the servos remembers that one of them is back to front compared with the other. So the result is that you end up writing more code in C. So the overhead of the vtables in C++ for each object would be offset by the amount of C code you could get rid of. The whole point of OO is that you can then abstract the implementation so if your servo class had a method:-
void setSpeed(int speed)
where the parameter is a percentage ie -100 = full speed reverse, +100 = full speed forward then you could change your motors to DC/Stepper etc without having to change the main part of your code.

Obviously the controller would freak out if you were trying to allocate and de-allocate thousands of objects per second. But what would be good is to create objects for each sensor/servo at the start of the code.

OO rules !! But its a big step for the beginner.




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

Offline pomprockerTopic starter

  • Supreme Robot
  • *****
  • Posts: 1,430
  • Helpful? 16
  • Sorry miss, I was giving myself an oil-job.
Re: c++ robot code
« Reply #13 on: March 18, 2008, 02:37:35 PM »
I was thinking of creating base classes and derived classes and member functions like arm ->left_arm->wrist->finger lol  then you could just tell the robot>  finger middleFinger; middleFinger.raise(3); //  for 3 seconds

Offline Webbot

  • Expert Roboticist
  • Supreme Robot
  • *****
  • Posts: 2,150
  • Helpful? 110
Re: c++ robot code
« Reply #14 on: March 18, 2008, 02:54:38 PM »
Cool. And thats where OO is great. Assume you had two arms (which I hope you do!!). Then your left arm does everything in reverse to your right arm - ie to put both arms up in the air then one goes clockwise and the other anti-clockwise. Much easier to handle this is OO classes rather than all your C code having to be done twice - once for the left and then again for the right. OO rocks. The advantage is magnified once you have inter-related classes like arm/elbow/wrist/finger.

The other great thing with OO is that classes are 'self-contained' and so could be published to a forum - such as this excellent one - which others could just plug into their own projects. Keep going with the C++ its great. But then Java is even cooler but thats a whole new ball game.

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

Offline pomprockerTopic starter

  • Supreme Robot
  • *****
  • Posts: 1,430
  • Helpful? 16
  • Sorry miss, I was giving myself an oil-job.
Re: c++ robot code
« Reply #15 on: March 18, 2008, 02:58:10 PM »
Java is my next class in pursuit of my CS degree  ;D

Maybe i'll start talking about robot JSPs for webapps  ::)

Offline Webbot

  • Expert Roboticist
  • Supreme Robot
  • *****
  • Posts: 2,150
  • Helpful? 110
Re: c++ robot code
« Reply #16 on: March 18, 2008, 03:07:51 PM »
You may also be interested in this link http://www.red3d.com/cwr/steer/ as it shows all sorts of behaviours as used in games, collections of robots etc. It also has a link to a free library (OpenSteer) which is all in C++ to help provide the behaviours that it shows. The animations on the site are cool - and it gives all the logic as to how each one works. I thoroughly recommend having a look (and NO - I didn't have anything to do with it!! ).
Webbot Home: http://webbot.org.uk/
WebbotLib online docs: http://webbot.org.uk/WebbotLibDocs
If your in the neighbourhood: http://www.hovinghamspa.co.uk

Offline benji

  • Supreme Robot
  • *****
  • Posts: 832
  • Helpful? 0
Re: c++ robot code
« Reply #17 on: March 19, 2008, 08:58:11 AM »
i have worked a lot with mcu programming and i dont think it can get any easier with c++
, i dont understand what is it in C that you dont like?
i dont think object oriented programming can add a lot to mcu's
anyways when you are in need to do advanced programming you r not goin to be able to use mcu's
you should implement your stuff over the pc, now we can use c++, matlab , java ,,,,,etc
but using it to program a mcu is quite uncommon
good ol' BeNNy

Offline pomprockerTopic starter

  • Supreme Robot
  • *****
  • Posts: 1,430
  • Helpful? 16
  • Sorry miss, I was giving myself an oil-job.
Re: c++ robot code
« Reply #18 on: March 19, 2008, 10:50:57 AM »
once you've gone to object oriented programming, you don't wanna go back to process oriented programming.  ;D

Offline benji

  • Supreme Robot
  • *****
  • Posts: 832
  • Helpful? 0
Re: c++ robot code
« Reply #19 on: March 19, 2008, 04:30:25 PM »
Quote
once you've gone to object oriented programming, you don't wanna go back to process oriented programming. 

what is it the big deal and ease provided by that so you dont wanna do anythin but object oriented for mcus?
good ol' BeNNy

Offline pomprockerTopic starter

  • Supreme Robot
  • *****
  • Posts: 1,430
  • Helpful? 16
  • Sorry miss, I was giving myself an oil-job.
Re: c++ robot code
« Reply #20 on: March 19, 2008, 05:11:56 PM »
Its just one of those debates that will go on forever  ;D

Offline JesseWelling

  • Expert Roboticist
  • Supreme Robot
  • *****
  • Posts: 707
  • Helpful? 0
  • Only You Can Build A Robot!
Re: c++ robot code
« Reply #21 on: March 19, 2008, 11:33:08 PM »
The big deal with object oriented design goes a couple ways,
  • OO is easier to read and think about because you abstract and take things to higher levels so instead of doing a bunch of bit oring and what not you see a representational function call on an object.
  • OO is easier to maintain because you can change how the insides of a function or class work if they need to without changing the code that uses it.
  • OO helps you to design your project by breaking down the problem and making you focus on one thing at a time.
« Last Edit: March 19, 2008, 11:34:14 PM by JesseWelling »

Offline benji

  • Supreme Robot
  • *****
  • Posts: 832
  • Helpful? 0
Re: c++ robot code
« Reply #22 on: March 20, 2008, 05:48:16 PM »
well any compilers for that? (microcontroller ones)
good ol' BeNNy

Offline JesseWelling

  • Expert Roboticist
  • Supreme Robot
  • *****
  • Posts: 707
  • Helpful? 0
  • Only You Can Build A Robot!
Re: c++ robot code
« Reply #23 on: March 20, 2008, 11:09:02 PM »
can't avr-gcc also do c++? google has the answers...

Offline paulstreats

  • Supreme Robot
  • *****
  • Posts: 1,381
  • Helpful? 21
Re: c++ robot code
« Reply #24 on: March 21, 2008, 03:53:56 AM »
It would be nice to use oo programming for certain things such as sensor objects etc... but it all depends on wether you can get away with it. When each object needs to be assigned a thread so it runs seemingly independantly, it reduces the overall speed for everything else to run, which is a big reason that oo programming isnt used in mcu's because they are not powerful enough. You can try to simulate an oo type of style by using typedef struct's in c:

Code: [Select]
#include <p18f2525.h>


typedef struct {         //define a new "type" structure

       unsigned char direction:1; //specify a bit field of 1 - allowing values of either 0 or 1
       
        unsigned char speed:7;  //specify a bit field of 7 to allow a speed between 0 and 128
//the above 2 only use 1 byte altogether

  } motor;          //the structure is called "motor"


void main(){
motor motor1;    //create a new instance of the 'motor' struct
motor motor2;

while(1){
motor1.direction=1;      //set the direction of motor1
motor2.direction=0;

}
}




As you can see, its quite easy to define an object with properties using c. But I havent worked on adding methods yet, I have a funny feeling that you would need to define them seperately from the typedef. For things like sensor reading and actuator control, I am working on a thread running system, but one that reserves a set speed for the main function.

Offline benji

  • Supreme Robot
  • *****
  • Posts: 832
  • Helpful? 0
Re: c++ robot code
« Reply #25 on: March 21, 2008, 05:03:37 AM »
yea that what i was talking about paul.. it seems that OO is not suitable for mcus , ....
good ol' BeNNy

Offline JesseWelling

  • Expert Roboticist
  • Supreme Robot
  • *****
  • Posts: 707
  • Helpful? 0
  • Only You Can Build A Robot!
Re: c++ robot code
« Reply #26 on: March 21, 2008, 12:23:19 PM »
Whoa guys,

Let me tell you what I do for a living. I work at John Deere in electro-hydraulic steering. You can look up AutoTrac and see what I work on half the time, the other half is under NDA so I can't talk about it. We have an embedded OS for task management (or threads if you will) and we use OO in C up the wazoo.

As for the function calls in OO and regular C do something like this (using the motor from the above example).

Code: [Select]
void SetMotorForward(motor* this)
{
   this->direction = 1;
}

void SetMotorBackward(motor* this)
{
   this->direction = 0;
}

void SetMotorSpeed(motor* this, uint8_t new_speed)
{
   if (new_speed >= 127) // note that 7 bytes can only represent 0 to 127
   {
      this->speed = new_speed;
   }
}

In this way the implementation of the motor driver (could be PWM to a servo, could be serial output, could  be a PWM to high current switch) doesn't matter to the application making the calls to change the motor, it just cares that it has a motor and a speed. As for the thread thing you need a scheduler of some sort. John Deere has made its own, at home I use Linux for my heavier robot tasks, and FreeRTOS on AVR's for every thing else.

One other thing, Instead of declaring those motors (and the storage scope) inside your main() block what you want to do is make an instances.c and an instances.h for all your classes. declare and define the variable of the motor structures in the .c file and then extern them in the .h file so that anywhere you #include the .h file, you will have access to those structures. So the above functions would be a class.c and the structure definition would be a class.h (you would also want to declare the functions in the class.h). but those motor structures would probably need more than just the bit field, they need to know what pins they hook up to and the current status (speed, direction). One of those should go in ROM and one should go in Ram. So what you do is create a Const sturcutres that go in to ROM and have a pointer to static variables in RAM. I'm in the middle of a rewrite on some of my lower level code and if I ever get it all cleaned up I'll show you exactly what I mean.
« Last Edit: March 21, 2008, 12:28:39 PM by JesseWelling »

Offline paulstreats

  • Supreme Robot
  • *****
  • Posts: 1,381
  • Helpful? 21
Re: c++ robot code
« Reply #27 on: March 21, 2008, 02:18:17 PM »
Thats definately useful. I started on my thread scheduler this afternoon, and have some basic threads running such as led's flashing at different rates. I just need to clean it up and work on making the implementation easier.

My idea is to make structured threads (eg. motor_runner) which is where you define the port number etc in. The motor_runner thread reads the type struct data and decides on what the port states should be.

A quick question:

If a create functions like you mention above, can they be called from anywhere or just from the object theyre related to?

Its amazing when you use c++ or java for a while just how much you take for granted.

Offline benji

  • Supreme Robot
  • *****
  • Posts: 832
  • Helpful? 0
Re: c++ robot code
« Reply #28 on: March 21, 2008, 03:36:36 PM »
i dont think c++ compilers for mcus are in this world
i did some search and ,,nada
good ol' BeNNy

Offline JesseWelling

  • Expert Roboticist
  • Supreme Robot
  • *****
  • Posts: 707
  • Helpful? 0
  • Only You Can Build A Robot!
Re: c++ robot code
« Reply #29 on: March 21, 2008, 07:59:29 PM »
If a create functions like you mention above, can they be called from anywhere or just from the object theyre related to?

Well in C there is no such thing as public and private functions, so if you want private functions declare them as file static and don't declare them in the .h file. Example:

MyClass.h
Code: [Select]
#ifndef MYCLASS_H
#define MYCLASS_H

typedef struct
{
   uint8_t var1;
   sint16_t var2;
} MyClass

sint16_t getSumOfVars(MyClass* this);  //decleration
void doSomeComplexThing(MyClass* this); //decleration

#endif

MyClass.C
Code: [Select]
#include "MyClass.h"
static void doPrivateFunction(MyClass* this);

sint16_t getSumOfVars(MyClass* this) // definition
{
   return (sint16_t)(this->var1 + this->var2);
}

void doSomeComplexThing(MyClass* this) //definition
{
   //do some complex stuff for this class
   doPrivateFunction(this);
   
}

static void doPrivateFunction(MyClass* this)
{
   //do some other stuff
}

Then you want your instances files

MyClass_Instances.h
Code: [Select]
extern MyClass ExampleInstanceOne;
extern MyClass ExampleInstanceTwo;

MyClass_Instances.c
Code: [Select]
MyClass ExampleInstanceOne = { 10, -10000};
MyClass ExampleInstanceTwo = { 5, 30000};

Then generally we make a .h file for the whole package

MyClass_Package.h
Code: [Select]
#ifndef MYCLASS_PACKAGE_H
#define MYCLASS_PACKAGE_H

#include "MyClass.h"
#include "MyClass_Instances.h"

#endif

And that's the basic model for classes in regular C.

 


Get Your Ad Here