help-octave
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Octave/C++


From: Mike Morley
Subject: Re: Octave/C++
Date: 25 Jul 2005 10:50:45 +0100

I tried the code you provided this morning linking to the header files, and it appears to work.

The problem I now have is how do I run commands to the robot?

I've looked at make_int.cc but I don't really understand the syntax I need to use to make things work.

Ideally, what I need is some way of issuing a command in octave like:

robotcommand (robot, motor1go, speed) that would run the C++ command robot.command (MOTOR_1_GO, speed)

So what I guess I need is some switch statement to convert the string into the appropriate robot command statement, and then pick up the other variable to pass to the robot. However, I'm not sure how to implement this.

Mike


On Jul 22 2005, John W. Eaton wrote:

On 22-Jul-2005, Mike Morley wrote:

| It occured to me that I needed to allocate some memory to my robot, | otherwise it would cease to be when the C++ subroutine exited. I now | have the code: | | #include<iostream>
| #include<octave/oct.h>
| #include<octave/dynamic-ld.h>

You probably don't need to include dynamic-ld.h.

| #include<string>
| | using namespace std;

I would recommend avoiding this and instead prefixing all the names
you actually use from the std namespace with "std::".

| extern "C" {
|   #include "robot_instr.h"
|   #include "robot_link.h"
| }

This works, but it would be better if the robot_instr.h and
robot_link.h files could be mad C++ friendly.

| class Bot: public octave_base_value
| {
|  public:
|   //constructor
|   Bot(void): octave_base_value()
|   {
|     robot_link robot;
|   }

This doesn't look right.  It simply creates a local variable in the
constructor, which will disappear when the constructor exits.  Perhaps
you want to have a robot_link object as a class data member, and
then a constructor like

  Bot (void) : robot (), octave_base_value () { }

(assuming the robot_link object is called robot)?


|   //destructor
|   ~Bot(void)
|   {
|       delete this;
|   }

It is almost certainly an error to "delete this".  Your constructor
doesn't use new, so your destructor should not use delete.

|   DEFUN_DLD(BotMake,,nargouts,
|           "BotMake creates a new robot to link in to")
| {
|     Bot *b = new Bot();
|   return octave_value(b);
| }

|   DEFUN_DLD(BotKill,args,,
|           "BotKill destroys the Bot")
| {
|   if (args.length() == 1) {
|     delete ((Bot*)&args(0));
|   }
| }

I dont' think you really want this function.  When the octave_value
object that holds your Bot object is deleted, it will call the
destructor for its Rep object, which will delete the memory.

Here is a complete working example (but using a simple typedef for the
robot_link object, so that I could avoid having to find the files for
that):

#include<iostream>
#include<octave/oct.h>

typedef int robot_link;

class Bot : public octave_base_value
{
public:

  Bot (void) : robot (), octave_base_value () { }

  bool is_defined (void) const { return true; }

private:

  robot_link robot;

  DECLARE_OCTAVE_ALLOCATOR

  DECLARE_OV_TYPEID_FUNCTIONS_AND_DATA
};

DEFINE_OCTAVE_ALLOCATOR (Bot);

DEFINE_OV_TYPEID_FUNCTIONS_AND_DATA (Bot, "Bot", "Bot");

DEFUN_DLD (BotMake, , ,
           "BotMake creates a new robot to link in to")
{
  static bool type_loaded = false;

  if (! type_loaded)
    {
      Bot::register_type ();
      mlock ("BotMake");

      // Install Bot ops here...
    }

  octave_value retval;

  return octave_value (new Bot ());
}

Compiling this with

  mkoctfile BotMake.cc

and running

  while true, x = BotMake; end

in Octave does not result in a memory leak.

You will need a few more functions in your Bot class to make it
useful.  See the example make_int.cc that is included in the Octave
sources.

jwe



-------------------------------------------------------------
Octave is freely available under the terms of the GNU GPL.

Octave's home on the web:  http://www.octave.org
How to fund new projects:  http://www.octave.org/funding.html
Subscription information:  http://www.octave.org/archive.html
-------------------------------------------------------------



--
Mike Morley - Trinity College Part IB Natural Sciences

E1 Burrell's Field | address@hidden | 07862 282608

   First & Third Trinity Boat Club May Ball 2005
address@hidden || address@hidden

   Cambridge Union Society -- External Committee






-------------------------------------------------------------
Octave is freely available under the terms of the GNU GPL.

Octave's home on the web:  http://www.octave.org
How to fund new projects:  http://www.octave.org/funding.html
Subscription information:  http://www.octave.org/archive.html
-------------------------------------------------------------



reply via email to

[Prev in Thread] Current Thread [Next in Thread]