How do I rebuild micropython firmware adding my own C++ code - c++

I want to run my C++ code no micropython
For that I referred https://github.com/stinos/micropython-wrap this wrapper.
Uploaded required wrapper files on the board and tried to run
#include <micropython-wrap-master/functionwrapper.h>
//function we want to call from within a MicroPython script
std::vector< std::string > FunctionToBeCalled ( std::vector< std::string > vec )
{
for( auto& v : vec )
v += "TRANSFORM";
return vec;
}
//function names are declared in structs
struct CppFunction
{
func_name_def( TransformList )
};
extern "C"
{
void RegisterMyModule(void)
{
//register a module named 'foo'
auto mod = upywrap::CreateModule( "foo" );
//register our function with the name 'TransformList'
//conversion of a MicroPython list of strings is done automatically
upywrap::FunctionWrapper wrapfunc( mod );
wrapfunc.Def< CppFunction::TransformList >( FunctionToBeCalled );
}
}
Run it using
import foo
print(foo.TransformList(['a', 'b'])) # Prints ['aTRANSFORM', 'bTRANSFORM']
But later I found this will not help. Because I need to integrate my C++ code into micropython code and rebuild the firmware to get it run.
I am not able to figure out
How to integrate my C++ in existing micropython code
How to recompile the firmware ( Because when I try to use make command, it does not seem working )
Any help highly appreciated.

MicroPython offers two options of adding C/C++ code to it.
Note that both require cross-compilation of that code on a PC.
AFAIK there is no option to compile on a microcontroller (due to obvious constraints)
1) MicroPython Native Module
One of the main advantages of using native .mpy files is that native machine code can be imported by a script dynamically, without the need to rebuild the main MicroPython firmware.
Essentially you compile C to MicroPython bytecode and store that in a module.
Then you import that module into Python
http://docs.micropython.org/en/latest/develop/natmod.html#minimal-example
2) MicroPython C Module
You add you C/C++ code as a custom module, and then compile and link that together into a new firmware image.
http://docs.micropython.org/en/latest/develop/cmodules.html#micropython-external-c-modules
The approach you refer to, is based on the 2nd method, and requires cross-compilation.
C or C++ support will depend on the compiler/languages supported for you port/hardware.

You don't need necessarily need to recompile, you can build a C++ user module. Which has been done for ESP32 already, see discussion at https://github.com/stinos/micropython-wrap/issues/5#issuecomment-704328111.
Specifically if you run make USER_C_MODULES=../../../micropython-wrap CFLAGS_EXTRA="-DMODULE_UPYWRAPTEST_ENABLED=1" from the ESP32 port directory, this should build a user C++ module containing micropython-wrap's unittests, so that should be a good starting point: copy the relevant files (cmodule.c, module.cpp and micropython.mk from the tests directory) and modify the code.

Related

Unable to create DLLs: Getting DLL "is not a valid Win32 application"

As the title says, I can't create a simple DLL. I'm using VS 2017 Community Edition Version 15.8.0. Here is the .dll code:
#include "stdafx.h"
#include "InvWin32App.h"
#include "$StdHdr.h"
void Prc1()
{
printf("ran procedure 1\n");
}
Here is the code for the header, per the MS way to do things:
#ifdef INVWIN32APP_EXPORTS
#define INVWIN32APP_API __declspec(dllexport)
#else
#define INVWIN32APP_API __declspec(dllimport)
#endif
#ifdef __cplusplus
extern "C" {
#endif
INVWIN32APP_API void Prc1();
#ifdef __cplusplus
}
#endif
Here is the driver code: (Update: The driver program is an .exe program.)
#include "pch.h"
#include "InvWin32App.h"
int main()
{
Prc1();
}
It can't get any simpler than this. I get the following error message box when I try to run the code:
Unable to start program
program name.dll
program name.dll is not
a valid Win32 application
I can create .exe programs okay. I also got the error earlier this morning when I was running VS 2017 Version 15.7.5. Upgrading VS didn't do any good. I also tried to compile these as .c programs, but it didn't make any difference.
I had this problem on a couple occasions creating .exe programs using VS 2015. I don't recall what I did, but the problem disappeared. Any help would be appreciated.
TIA.
Right click the project in your solution explorer that is the project for the executable and click "Set as startup project."
Note that "is not a valid Win32 application" is not a compile error or a link error, it is the message you get when you tried to debug something that is not executable.
You can only start executables. Executables consume dlls. These should be two seperate projects with two sets of corresponding project settings.
I doubt that you are using a 32 bit dll. If you have your 64-bit Windows OS then you must have 64 bit version of this dll too. Sometimes, even you have 64-bit dll already then it could not run, so try the other version of this dll i.e. 32 bit version of this dll.
Along with this, make a small change in your unicurse.py file too.
PFB the code to add:
import ctypes
pdlib = ctypes.CDLL("pdcurses.dll")
though this line of code is somewhere inside some condition, but bringing on top will help you to check, if the dll has been loaded or not.
For any one joining the party at this time, I found that this error is caused when you try to run a 32bit dll using a 64bit python.
Now I won't go into the details as to why that won't work as above responses clearly describes that, but there are 2 solutions for this.
Install and 32bit python and use that to communicate with the dll.
Interprocess communication using the msl-loadlib library documentation
I used the second step which seemed more sustainable. The steps I am using below can be found in the documentation link above.
First is to create a server module that communicates with the 32 bit module
#server.py
from msl.loadlib import Server32
class Server(Server32):
# the init takes mandatory host and port as arguments
def __init__(self, host, port, **kwargs):
# using windll since this application is being run in windows, other options such as cdll exists
# this assumes that the dll file is in the same directory as this file
super(Server, self).__init__('tresdlib.dll', 'windll', host, port)
# define a function that is to be called with the required arguments
def fsl_command(self, com, doc):
#the server32 exposes the loaded dll as lib, which you can then use to call the dll functions and pass the required arguments
return self.lib.FSL_Command(com,doc)
Then we create a client module, that sends python requests to the server module
#client.py
from msl.loadlib import Client64
class Client(Client64):
def __init__(self):
# pass the server file we just created in module32=, the host am using is localhost, leave port as none for self assigning
super(Client, self).__init__(module32='server', host="127.0.0.1", port=None)
# define a function that calls the an existing server function and passes the args
def fsl_command(self, com, doc):
return self.request32('fsl_command', com, doc)
We are going to run this in a terminal but you can choose to call in another application
>>> from client import Client
>>> c = Client()
>>> c.fsl_command(arg1,arg2)

Use a C++ compiled code within a R Shiny app in shinyapp.io

I have developed a ShinyApp that is built around a C++ program. In short, what the app does is :
provides a nice interface to setup the parameters (in a text file) for the C++ app
runs the C++ compiled code using the system(...) command
displays the output of the C++ code using ggplot2
The C++ compiled code is stored into the www folder. Locally it works fine, but when I load the app to the shinyapp website (I have a free subscription), I got the following error:
sh: 1: ./a.out: Permission denied
with a.out being my compile c++ code. Any idea if
I am doing something wrong?
It is possible call a compiled c++ code within shinyapp.io?
This is a super old question, but since I stumbled on it looking for an answer for my identical problem, I would share what worked for me.
I didn't try the .bat suggestion mentioned in the comments, because that seemed to be tied to Windows OS and Shiny uses Linux.
Instead, I used R's Sys.chmod() function. In your case, if you are calling system("a.out"), before that line, put Sys.chmod("a.out", mode="777"). Note that you may want to look more into what chmod does with regards to permissions. But the code would look like:
// ...
Sys.chmod("a.out", mode="777")
system("a.out")
// ... remaining code

Eclipse Neon build errors despite successful build

I'm trying to use Eclipse to do the development for a project that involves Gazebo (a popular robotics simulator). Gazebo provides a plugin system to allow external interaction with the simulator and a series of tutorials on how to write plugins.
Having followed the tutorials successfully, I tried migrating the code to Eclipse, using cmake -G "Eclipse CDT4 - Unix Makefiles" [buildpath] to generate an eclipse rpoject, then importing it into my Eclipse workspace.
Everything generally went well, but I've run into a problem that is a bit odd:
When I compile my project, Eclipse comes back with "Member declaration not found" error referring to an SDFormat data type used in the signature to the ModelPush::Load function (see code snippets below). SDFormat, incidnetally is a robotics XML used for describing how a robot is put together.
Despite this error (which should result in nothing being built), the resulting shared library is built anyway.
I guess I can live with it, but I'd obviously like to resolve this issue, which appears to be internal to Eclipse / CDT...
TO CLARIFY:
I'm trying to determine why Eclipse gives me the error: "Member declaration not found" on the Load() function signature in model_push.cc. The guilty party is the sdf::ElementPtr _sdf parameter. Something's wrong with the SDFormat library or with the way that Eclipse / CDT looks at it. This isn't an include issue. And, even though Eclipse gives me the error, it still builds the .so file. Running make from the command line also generates the file, but without any errors.
Again, I can live with it, but I'd rather not. I just don't know where to start looking for a solution since this isn't a problem finding an include or the sdf library file.
Here's the class declaration (mode_push.hh):
#ifndef MODEL_PUSH_HH_
#define MODEL_PUSH_HH_
#include <boost/bind.hpp>
#include <gazebo/gazebo.hh>
#include <gazebo/physics/physics.hh>
#include <gazebo/common/common.hh>
#include <stdio.h>
#include <sdf/sdf.hh>
namespace gazebo
{
class ModelPush : public ModelPlugin
{
public:
void Load (physics::ModelPtr _parent, sdf::ElementPtr _sdf);
//Called by the world update start event
void OnUpdate (const common::UpdateInfo & /*_info*/);
//Pointer to the model
private:
physics::ModelPtr model;
//Pointer to the update event connection
private:
event::ConnectionPtr updateConnection;
};
}
#endif /* MODEL_PUSH_HH_ */
Here's the implementation file (model_push.cc):
#include "model_push.hh"
namespace gazebo
{
void ModelPush::Load(physics::ModelPtr _parent, sdf::ElementPtr _sdf)
//void ModelPush::Load (physics::ModelPtr _parent, sdf::ElementPtr /*sdf*/)
{
//Store the pointer to the model
this -> model = _parent;
//Listen to the update event. This event is broadcast every
//simulation iteration.
this -> updateConnection = event::Events::ConnectWorldUpdateBegin(
boost::bind (&ModelPush::OnUpdate, this, _1));
}
//Called by the world update start event
void ModelPush::OnUpdate (const common::UpdateInfo & /*_info*/)
{
//Apply a small linear velocity to the model.
this -> model -> SetLinearVel (math::Vector3 (0.03, 0.0, 0.0));
}
//Register this plugin with the simulator
//GZ_REGISTER_MODEL_PLUGIN(ModelPush)
}
I've been struggling with this exact problem. I've found a solution that works, but I still don't think is ideal. Instead of generating the eclipse project using cmake (or catkin_make) I'm generating it using the CDT project builder. Here's the process I'm using in Eclipse 2018-09.
Create a New C/C++ Project of type C++ Managed Build (A C++ Project build using the CDT's managed build system.)
Project name: ROSWorkspace
Location: /home/username/eclipse-workspace/ROSWorkspace
Project type: Makefile project / Empty Project
Toolchain: Linux GCC
Finish.
Right click on the project and select Properties.
C/C++ Build / Builder Stetings:
Uncheck "Use default build command"
Build command: catkin_make
Build directory: ${workspace_loc:/../catkin_ws}/
C/C++ General / Paths and Symbols / Includes tab
Add /usr/include/gazebo-8
Add /usr/include/sdformat-5.3
C/C++ General / Preprocessor Includes / Providers tab
CDT GCC Built-in Compiler Settings / Command to get compiler specs: ${COMMAND} ${FLAGS} -E -P -v -dD "${INPUTS}" -std=c++11
Click Ok, then from the drop down menu choose:
Project / C/C++ Index / Freshen all files
Ideally I'd make the time to dig in to figure out how to get the preprocessor to properly work with the generated project, but I just don't have the time right now. I hope this helps.

ldc2 vs ldmd2 whats the difference?

I recently installed ldc through hombrew on my Mac and was testing out running a code as a script from the D wiki when I noticed that using ldmd2 to compile my program doesn't also run my program after. Whats the difference then since this is the same behavior as running ldc2.
Heres my program
import std.stdio;
void main()
{
writeln("Hello, world without explicit compilations!");
}
EDIT:
The website states "For small projects it's handy to compile and run in a single step. Most (if not all) compiler packages, contain a tool named rdmd/gdmd/ldmd or similar. For instructional purposes we'll call it rdmd." What im taking from this is it depends on which compiler your using but in the case of ldc I should be using ldmd.
ldmd2 is just a wrapper script for ldc2 which converts argument formats from dmd style over to ldc style.
So it does exactly the same thing, just some options and flags on the compile command line have different names and stuff like that.
The link is talking about rdmd which is a separate program that recursively grabs dependencies, compiles, and automatically runs. rdmd works on top of the compiler and might have been packaged with it or might need to be downloaded separately.
Its source lives here: https://github.com/D-Programming-Language/tools/blob/master/rdmd.d
and it is compatible with ldmd2's option formats.

Importing ctype; embedding python in C++ application

I'm trying to embed python within a C++ based programming language (CCL: The compuatational control language, not that any of you have heard of it). Thus, I don't really have a "main" function to make calls from.
I have made a test .cc program with a main, and when I compile it and run it, I am able to import my own python modules and system modules for use.
When I embed my code in my CCL-based program and compile it (with g++), it seems I have most functionality, but I get a RUNTIME error:
ImportError: /usr/lib/python2.6/lib-dynload/_ctypes.so: undefined symbol: PyType_GenericNew
This is the code that is executed at Runtime error.
void FFSim::initCKBot (){
Py_Initialize();
PyRun_SimpleString("execfile('logical.py')");
}
logical.py imports modules, one of which attempts to execute 'from cytpes import *', which throws the runtime error.
Can someone explain this to me and how to go about solving it? It seems like I've linked the objects correctly when compiling the c++ aspect of the code.
Thanks.
The Python runtime is effectively a collection of libraries that your program uses. Those libraries take strings, convert them to Python bytecode and then interpret the bytecode. The error you're getting is that as part of interpreting the program, the Python runtime needs to call a function (PyType_GenericNew), but that function does not exist in the compiled Python runtime on your system. Going off the name of the function, this isn't something you can ignore or workaround. It's a fundamental part of the runtime.
Assuming your PATH is correct, your best solution is to reinstall or rebuild Python. Your installation is missing something important.