Giter VIP home page Giter VIP logo

Comments (5)

jmpenn avatar jmpenn commented on August 25, 2024 1

Wouldn't original functions(with void param) still make the calculations?

How would the function know what to update?

from trick.

Fjolnirr avatar Fjolnirr commented on August 25, 2024

Well, I have managed to find a workaround that works for my case but I don't know if it is the best practice.

I have added wrappers functions to the models folder which I use it in S_define file.

  • Here is my directory tree :
├── models                                                                                                                                                                                      
│   ├── WRAPPERS                                                                                                                                                                                    
│   │   ├── include                                                                                                                                                                             
│   │   │   ├── CANNON_WRAPPER.h 
│   │   └── src                                                                                                                                                                                 
│   │       ├── CANNON_WRAPPER.c 
│   └── cannon                                                                                                                                                                               
│       ├── include                                                                                                                                                                             
│       │   ├── rtw_continuous.h                                                                                                                                                                
│       │   ├── rtw_solver.h                                                                                                                                                                    
│       │   ├── rtwtypes.h                                                                                                                                                                      
│       │   └── CANNON.h                                                                                                                                                                     
│       └── src                                                                                                                                                                                 
│           ├── CANNON.c                                                                                                                                                                     
│           └── CANNON_data.c                                                                                                                                                                
├── Modified_data                                                                                                                                                                               
│   └── realtime.py                                                                                                                                                                             
├── RUN_test                                                                                                                                                                                    
│   └── input.py                                                                                                                                                                                
├── S_define                                                                                                                                                                                    
└── S_overrides.mk   
  • Here is my S_define ;
class cannonSimObject: public Trick::SimObject {
   Ext_CANON CANNON_A; 
    public:
        cannonSimObject(){
        ("initialization") cannon_initialize_wrapper(&CANNON_A);
        (0.01, "scheduled") cannon_step_wrapper(&CANNON_A);
        }
}

cannonSimObject cannonSO;
  • And this is my CANNON_WRAPPER.c ;
#include "../include/CANNON_WRAPPER.h";
#include "../../cannon/include/CANNON.h" 
 
void cannon_initialize_wrapper(Ext_CANON* cannon_a){
    *cannon_a = CANNON_A ; /*CANNON_A which is already declared in CANNON.c and CANNON.h*/
    cannon_initialize();
}

void cannon_step_wrapper(Ext_CANON* cannon_a){
    *cannon_a = CANNON_A ; /*CANNON_A which is already declared in CANNON.c and CANNON.h*/
    cannon_step();
}

What I am doing here is that passing the value of CANNON_A from model source file to the one from S_define. This solved my issue but my question still keeping on. I wonder if trick has another way around?

from trick.

jmpenn avatar jmpenn commented on August 25, 2024

First off we don’t know anything about “Embedded Coder auto generated code.” That’s apparently a Simulink / Mathworks thing.

However, from what I can see, you're trying to update an external object (namely "Ext_CANON CANNON_A") from within a Trick sim object. I can show you how I would do that by way of a little example sim (below). Hope this helps.

S_define

/************************************************************
PURPOSE:
    ()
LIBRARY DEPENDENCIES:
    ((cannon/src/cannon.c))
*************************************************************/
#include "sim_objects/default_trick_sys.sm"
##include "cannon/include/cannon.h"

class CannonSimObject : public Trick::SimObject {
    public:
    CannonSimObject() {
        ("initialization") cannon_initialize( &CANNON_B) ;
        (0.1, "scheduled") cannon_step( &CANNON_B) ;
    }
};

CannonSimObject cannonSO;

cannon.h
(put this in ./models/cannon/include/ )

/*************************************************************************
PURPOSE: (Example)
LIBRARY DEPENDENCIES:
    ((cannon/src/cannon.o))
**************************************************************************/
#ifndef _cannon_hh_
#define _cannon_hh_

typedef struct cannon {
        double pos[2] ;
} Ext_CANON;

#ifdef __cplusplus
extern "C" {
#endif
int cannon_initialize(Ext_CANON* cannon);
int cannon_step(Ext_CANON* cannon);
#ifdef __cplusplus
}
#endif

extern Ext_CANON CANNON_A;  // This is a "promise" that the objects are out there.
extern Ext_CANON CANNON_B;

#endif

cannon.c
(put this in ./models/cannon/src/ )

/********************************* TRICK HEADER *******************************
PURPOSE: ( EXAMPLE )
LIBRARY DEPENDENCY:
    ((cannon.o))
*******************************************************************************/
#include "../include/cannon.h"
#include <math.h>
#include <stdio.h>

Ext_CANON CANNON_A = {0.1, 0.2};     // Here is where the objects are actually instanciated.
Ext_CANON CANNON_B = {0.3, 0.4};

int cannon_initialize(Ext_CANON* cannon) {
    printf("INIT: pos = < %g, %g >\n", cannon->pos[0], cannon->pos[1]);
    return (0);
}

int cannon_step(Ext_CANON* cannon) {
    printf("STEP: pos = < %g, %g >\n", cannon->pos[0], cannon->pos[1]);
    return(0);
}

realtime.py
(put this in ./Modified_data/ )

trick.real_time_enable()
trick.exec_set_software_frame(0.1)
trick.itimer_enable()

input.py
(put this in ./RUN_test/ )

exec(open("Modified_data/realtime.py").read())
trick.stop(5);

S_overrides.mk
(Same dir as S_define)

TRICK_CFLAGS += -Imodels
TRICK_CXXFLAGS += -Imodels

from trick.

jmpenn avatar jmpenn commented on August 25, 2024

I really don't think the wrapper functions are necessary.

from trick.

Fjolnirr avatar Fjolnirr commented on August 25, 2024

Well, I thought about your answer already however, I avoid changes in source files because it is auto generated. Probably the best option to handle such a problem finding a way to create trick friendly source code. Wrappers might keep my works generic. Thank you.

I got a question about your answer. You didn't declare &CANNON_B in the S_define so it is reading from the cannon.h but why did you change the functions if the &CANNON_B is accesible from the S_define? Wouldn't original functions(with void param) still make the calculations?

I mean this :

/************************************************************
PURPOSE:
    ()
LIBRARY DEPENDENCIES:
    ((cannon/src/cannon.c))
*************************************************************/
#include "sim_objects/default_trick_sys.sm"
##include "cannon/include/cannon.h"

class CannonSimObject : public Trick::SimObject {
    public:
    CannonSimObject() {
        ("initialization") cannon_initialize( /* &CANNON_B*/ ) ; 
        (0.1, "scheduled") cannon_step( /* &CANNON_B*/ ) ;
    }
};

CannonSimObject cannonSO;

from trick.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.