Skip to content

Commit 8aeca37

Browse files
amathamkamathamk
authored andcommitted
AP_Simulink: simulink support for ArduCopter-Renaming AC_Simulink to AP_Simulink
1 parent 7dbfa4b commit 8aeca37

19 files changed

+576
-0
lines changed
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
// @file AC_Simulink_AHRSHandle.cpp
2+
// @brief This class provides AP_AHRS_View class instance and enables access to AP_AHRS_View methods from Simulink-generated code.
3+
#include "AC_Simulink_AHRSHandle.h"
4+
5+
AC_Simulink_AHRS *AC_Simulink_AHRS::_singleton = nullptr;
6+
7+
void AC_Simulink_AHRS::initializeInterfaces(AP_AHRS_View *&ahrs)
8+
{
9+
if (_singleton == nullptr) {
10+
_singleton = new AC_Simulink_AHRS(ahrs);
11+
}
12+
}
13+
14+
AP_AHRS_View *&AC_Simulink_AHRS::getHandle_AP_AHRS_View() const
15+
{
16+
return _ahrs;
17+
}
18+
19+
// get singleton instance
20+
AC_Simulink_AHRS *AC_Simulink_AHRS::get_singleton()
21+
{
22+
return _singleton;
23+
}
24+
25+
AC_Simulink_AHRS::AC_Simulink_AHRS(AP_AHRS_View *&ahrs) : _ahrs(ahrs)
26+
{
27+
}
28+
void AC_Simulink_AHRS::init(void)
29+
{
30+
}
31+
Vector3f AC_Simulink_AHRS::getAngularVelocity(void)
32+
{
33+
return _ahrs->get_gyro_latest();
34+
}
35+
void AC_Simulink_AHRS::getAttitudeQuat(Quaternion &attQuat)
36+
{
37+
_ahrs->get_quat_body_to_ned(attQuat);
38+
}
39+
bool AC_Simulink_AHRS::getPositionNED(Vector3f &posNED)
40+
{
41+
return _ahrs->get_relative_position_NED_origin(posNED);
42+
}
43+
bool AC_Simulink_AHRS::getVelocityNED(Vector3f &velNED)
44+
{
45+
return _ahrs->get_velocity_NED(velNED);
46+
}
47+
AC_Simulink_AHRS *sl_ahrs_handle = AC_Simulink_AHRS::get_singleton();
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
#pragma once
2+
// @file AC_Simulink_AHRSHandle.h
3+
// @brief This class provides AP_AHRS_View class instance and enables access to AP_AHRS_View methods from Simulink-generated code.
4+
#include <AP_Common/AP_Common.h>
5+
#include <AP_AHRS/AP_AHRS_View.h>
6+
7+
class AC_Simulink_AHRS
8+
{
9+
public:
10+
static void initializeInterfaces(AP_AHRS_View *&ahrs);
11+
12+
AP_AHRS_View *&getHandle_AP_AHRS_View() const ;
13+
14+
// get singleton instance
15+
static AC_Simulink_AHRS *get_singleton();
16+
17+
// Functions specific to Simulink blocks.
18+
// The following functions are invoked by Simulink-generated code when the corresponding blocks are added to the model.
19+
20+
void init(void);
21+
// get estimated angular velocity
22+
Vector3f getAngularVelocity();
23+
// get estimated attitude in Quaternion
24+
void getAttitudeQuat(Quaternion &attQuat);
25+
// get estimated position in NED frame
26+
bool getPositionNED(Vector3f &posNED);
27+
// get estimated velocity in NED frame
28+
bool getVelocityNED(Vector3f &velNED);
29+
30+
protected:
31+
// References to external libraries
32+
AP_AHRS_View *&_ahrs;
33+
34+
private:
35+
AC_Simulink_AHRS(AP_AHRS_View *&ahrs) ;
36+
37+
static AC_Simulink_AHRS *_singleton;
38+
};
39+
40+
extern AC_Simulink_AHRS *sl_ahrs_handle;
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
// @file AC_Simulink_AttControlHandle.cpp
2+
// @brief This class provides AC_PosControl class instance and enables access to AC_PosControl methods from Simulink-generated code.
3+
#include "AC_Simulink_AttControlHandle.h"
4+
5+
AC_Simulink_AttControlHandle *AC_Simulink_AttControlHandle::_singleton = nullptr;
6+
7+
void AC_Simulink_AttControlHandle::initializeInterfaces(AC_AttitudeControl *&att_control)
8+
{
9+
if (_singleton == nullptr) {
10+
_singleton = new AC_Simulink_AttControlHandle(att_control);
11+
}
12+
}
13+
14+
AC_AttitudeControl *&AC_Simulink_AttControlHandle::getHandle_AC_AttControl() const
15+
{
16+
return _att_control;
17+
}
18+
19+
// get singleton instance
20+
AC_Simulink_AttControlHandle *AC_Simulink_AttControlHandle::get_singleton()
21+
{
22+
return _singleton;
23+
}
24+
25+
AC_Simulink_AttControlHandle::AC_Simulink_AttControlHandle(AC_AttitudeControl *&att_control) : _att_control(att_control)
26+
{
27+
}
28+
void AC_Simulink_AttControlHandle::init(void)
29+
{
30+
}
31+
Quaternion AC_Simulink_AttControlHandle::getAttitudeSetpointQuat(void)
32+
{
33+
return _att_control->get_attitude_target_quat();
34+
}
35+
Vector3f AC_Simulink_AttControlHandle::getAngularVelocitySetpoint(void)
36+
{
37+
return _att_control->get_attitude_target_ang_vel();
38+
}
39+
AC_Simulink_AttControlHandle *sl_attControl_handle = AC_Simulink_AttControlHandle::get_singleton();
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
#pragma once
2+
// @file AC_Simulink_AttControlHandle.h
3+
// @brief This class provides AC_AttitudeControl class instance and enables access to AC_AttitudeControl methods from Simulink-generated code.
4+
#include <AP_Common/AP_Common.h>
5+
#include <AC_AttitudeControl/AC_AttitudeControl.h>
6+
7+
class AC_Simulink_AttControlHandle
8+
{
9+
public:
10+
static void initializeInterfaces(AC_AttitudeControl *&att_control);
11+
12+
AC_AttitudeControl *&getHandle_AC_AttControl() const ;
13+
14+
// get singleton instance
15+
static AC_Simulink_AttControlHandle *get_singleton();
16+
17+
// Functions specific to Simulink blocks.
18+
// The following functions are invoked by Simulink-generated code when the corresponding blocks are added to the model.
19+
void init(void);
20+
// get attitude setpoint in quaternion
21+
Quaternion getAttitudeSetpointQuat(void);
22+
// get angular velocity setpoint in quaternion
23+
Vector3f getAngularVelocitySetpoint(void);
24+
25+
protected:
26+
// References to external libraries
27+
AC_AttitudeControl *&_att_control;
28+
29+
private:
30+
AC_Simulink_AttControlHandle(AC_AttitudeControl *&att_control) ;
31+
32+
static AC_Simulink_AttControlHandle *_singleton;
33+
};
34+
35+
extern AC_Simulink_AttControlHandle *sl_attControl_handle;
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
/// @file AC_Simulink_ControllerInterfaces.cpp
2+
/// @brief This class is responsible for storing instances of the AHRS, Attitude & Position Controller,
3+
/// and Motor classes. These instances are provided by the copter class and stored for access by Simulink-generated code.
4+
#include <AP_HAL/AP_HAL.h>
5+
#include "AC_Simulink_ControllerInterfaces.h"
6+
#include "AC_Simulink_AHRSHandle.h"
7+
#include "AC_Simulink_AttControlHandle.h"
8+
#include "AC_Simulink_PosControlHandle.h"
9+
#include "AC_Simulink_MotorsHandle.h"
10+
11+
AC_Simulink_ControllerInterfaces::AC_Simulink_ControllerInterfaces(AP_AHRS_View *&ahrs,
12+
AC_AttitudeControl *&att_control, AC_PosControl *&pos_control, AP_MotorsMulticopter *&motors)
13+
{
14+
AC_Simulink_AHRS::initializeInterfaces(ahrs);
15+
AC_Simulink_AttControlHandle::initializeInterfaces(att_control);
16+
AC_Simulink_PosControlHandle::initializeInterfaces(pos_control);
17+
AC_Simulink_MotorsHandle::initializeInterfaces(motors);
18+
}
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
#pragma once
2+
/// @file AC_Simulink_ControllerInterfaces.h
3+
/// @brief This class is responsible for storing instances of the AHRS, Attitude & Position Controller,
4+
/// and Motor classes. These instances are provided by the copter class and stored for access by Simulink-generated code.
5+
#include <AP_Common/AP_Common.h>
6+
#include <AP_AHRS/AP_AHRS_View.h>
7+
#include <AC_AttitudeControl/AC_AttitudeControl.h>
8+
#include <AC_AttitudeControl/AC_PosControl.h>
9+
#include <AP_Motors/AP_MotorsMulticopter.h>
10+
11+
class AC_Simulink_ControllerInterfaces
12+
{
13+
public:
14+
AC_Simulink_ControllerInterfaces(AP_AHRS_View *&ahrs,
15+
AC_AttitudeControl *&_att_control, AC_PosControl *&_pos_control,
16+
AP_MotorsMulticopter *&motors);
17+
18+
CLASS_NO_COPY(AC_Simulink_ControllerInterfaces);
19+
};
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
// @file AC_Simulink_MotorsHandle.cpp
2+
// @brief This class provides AC_PosControl class instance and enables access to AC_PosControl methods from Simulink-generated code.
3+
#include "AC_Simulink_MotorsHandle.h"
4+
5+
AC_Simulink_MotorsHandle *AC_Simulink_MotorsHandle::_singleton = nullptr;
6+
7+
void AC_Simulink_MotorsHandle::initializeInterfaces(AP_MotorsMulticopter *&motors)
8+
{
9+
if (_singleton == nullptr) {
10+
_singleton = new AC_Simulink_MotorsHandle(motors);
11+
}
12+
}
13+
14+
AP_MotorsMulticopter *&AC_Simulink_MotorsHandle::getHandle_AP_Motors() const
15+
{
16+
return _motors;
17+
}
18+
19+
// get singleton instance
20+
AC_Simulink_MotorsHandle *AC_Simulink_MotorsHandle::get_singleton()
21+
{
22+
return _singleton;
23+
}
24+
25+
AC_Simulink_MotorsHandle::AC_Simulink_MotorsHandle(AP_MotorsMulticopter *&motors) : _motors(motors)
26+
{
27+
}
28+
void AC_Simulink_MotorsHandle::init(void)
29+
{
30+
}
31+
void AC_Simulink_MotorsHandle::setTorque(float tauRoll,float tauPitch,float tauYaw)
32+
{
33+
_motors->set_roll(tauRoll);
34+
_motors->set_pitch(tauPitch);
35+
_motors->set_yaw(tauYaw);
36+
}
37+
void AC_Simulink_MotorsHandle::setThrust(float throttle)
38+
{
39+
_motors->set_throttle(throttle);
40+
}
41+
42+
AC_Simulink_MotorsHandle *sl_motors_handle = AC_Simulink_MotorsHandle::get_singleton();
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
#pragma once
2+
// @file AC_Simulink_MotorsHandle.h
3+
// @brief This class provides AP_MotorsMulticopter class instance and enables access to AP_MotorsMulticopter methods from Simulink-generated code.
4+
#include <AP_Common/AP_Common.h>
5+
#include <AP_Motors/AP_MotorsMulticopter.h>
6+
7+
class AC_Simulink_MotorsHandle
8+
{
9+
public:
10+
static void initializeInterfaces(AP_MotorsMulticopter *&motors);
11+
12+
AP_MotorsMulticopter *&getHandle_AP_Motors() const ;
13+
14+
// get singleton instance
15+
static AC_Simulink_MotorsHandle *get_singleton();
16+
17+
// Functions specific to Simulink blocks.
18+
// The following functions are invoked by Simulink-generated code when the corresponding blocks are added to the model.
19+
void init(void);
20+
// set roll, pitch and yaw torue setpoint
21+
void setTorque(float tauRoll,float tauPitch,float tauYaw);
22+
// set vertical thrust
23+
void setThrust(float throttle);
24+
25+
protected:
26+
// References to external libraries
27+
AP_MotorsMulticopter *&_motors;
28+
29+
private:
30+
AC_Simulink_MotorsHandle(AP_MotorsMulticopter *&motors) ;
31+
32+
static AC_Simulink_MotorsHandle *_singleton;
33+
};
34+
35+
extern AC_Simulink_MotorsHandle *sl_motors_handle;
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
// @file AC_Simulink_PosControlHandle.h
2+
// @brief This class provides AC_PosControl class instance and enables access to AC_PosControl methods from Simulink-generated code.
3+
#include "AC_Simulink_PosControlHandle.h"
4+
5+
AC_Simulink_PosControlHandle *AC_Simulink_PosControlHandle::_singleton = nullptr;
6+
7+
void AC_Simulink_PosControlHandle::initializeInterfaces(AC_PosControl *&pos_control)
8+
{
9+
if (_singleton == nullptr) {
10+
_singleton = new AC_Simulink_PosControlHandle(pos_control);
11+
}
12+
}
13+
14+
AC_PosControl *&AC_Simulink_PosControlHandle::getHandle_AC_PosControl() const
15+
{
16+
return _pos_control;
17+
}
18+
19+
// get singleton instance
20+
AC_Simulink_PosControlHandle *AC_Simulink_PosControlHandle::get_singleton()
21+
{
22+
return _singleton;
23+
}
24+
25+
AC_Simulink_PosControlHandle::AC_Simulink_PosControlHandle(AC_PosControl *&pos_control) : _pos_control(pos_control)
26+
{
27+
}
28+
void AC_Simulink_PosControlHandle::init(void)
29+
{
30+
}
31+
Vector3p AC_Simulink_PosControlHandle::getPositionSetPointNEU(void)
32+
{
33+
return _pos_control->get_pos_target_cm();
34+
}
35+
Vector3f AC_Simulink_PosControlHandle::getVelocitySetPointNEU(void)
36+
{
37+
return _pos_control->get_vel_desired_cms();
38+
}
39+
40+
AC_Simulink_PosControlHandle *sl_posControl_handle = AC_Simulink_PosControlHandle::get_singleton();
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
#pragma once
2+
// @file AC_Simulink_PosControlHandle.h
3+
// @brief This class provides AC_PosControl class instance and enables access to AC_PosControl methods from Simulink-generated code.
4+
#include <AP_Common/AP_Common.h>
5+
#include <AC_AttitudeControl/AC_PosControl.h>
6+
7+
class AC_Simulink_PosControlHandle
8+
{
9+
public:
10+
static void initializeInterfaces(AC_PosControl *&pos_control);
11+
12+
AC_PosControl *&getHandle_AC_PosControl() const ;
13+
14+
// get singleton instance
15+
static AC_Simulink_PosControlHandle *get_singleton();
16+
17+
// Functions specific to Simulink blocks.
18+
// The following functions are invoked by Simulink-generated code when the corresponding blocks are added to the model.
19+
void init(void);
20+
// get position setpoint in NEU frame
21+
Vector3p getPositionSetPointNEU(void);
22+
// get velocity setpoint in NEU frame
23+
Vector3f getVelocitySetPointNEU(void);
24+
protected:
25+
// References to external libraries
26+
AC_PosControl *&_pos_control;
27+
28+
private:
29+
AC_Simulink_PosControlHandle(AC_PosControl *&pos_control) ;
30+
31+
static AC_Simulink_PosControlHandle *_singleton;
32+
};
33+
34+
extern AC_Simulink_PosControlHandle *sl_posControl_handle;

0 commit comments

Comments
 (0)