Speed performance, penalties

64 views
Skip to first unread message

Valeriu Balaban

unread,
Nov 25, 2013, 1:25:18 PM11/25/13
to python-o...@googlegroups.com
Hey guys.

First of all thanks for such beautiful thing like Python on a MCU. At this moment I'm evaluating if it's suitable for my current project. The main feature of this project will be the possibility to update the logic on the fly, but without interrupting the C code that gets information from sensors and make some sort of smart history.

So with this in mind I will ask you for some advises and help.

The main function of the python will be to update a servo position, once at 20 ms. With my tests now I get at first run of the module 25.7 ms and afterwards around 15.6 ms. A temporary solution is to increase the MCU speed, now is 48 MHz and I can get a different one with 72 MHz.

The pmfeatures:

PM_FEATURES = {
"HAVE_PRINT": True,
"HAVE_GC": True,
"HAVE_FLOAT": True,
"HAVE_DEL": True,
"HAVE_IMPORTS": True,
"HAVE_DEFAULTARGS": True,
"HAVE_REPLICATION": True,
"HAVE_CLASSES": True,
"HAVE_ASSERT": True,
"HAVE_GENERATORS": True,
"HAVE_BACKTICK": True,
"HAVE_STRING_FORMAT": True,
"HAVE_CLOSURES": True,
"HAVE_BYTEARRAY": True,
"HAVE_DEBUG_INFO": True,
"HAVE_SNPRINTF_FORMAT": False,
"HAVE_AUTOBOX": True,
"HAVE_SLICE": True,
}

and the platform file is this one:

/*
* plat.c
*
* Created on: Nov 24, 2013
* Author: Valeriu
*/

#undef __FILE_ID__
#define __FILE_ID__ 0x70


/** PyMite platform-specific routines for AVR target */

#include "pm.h"

uint8_t buff[1024];
uint32_t pos;

PmReturn_t
plat_init(void)
{
pos = 0;
return PM_RET_OK;
}


PmReturn_t
plat_deinit(void)
{
return PM_RET_OK;
}


uint8_t
plat_memGetByte(PmMemSpace_t memspace, uint8_t const **paddr)
{
uint8_t b = 0;

switch (memspace)
{
case MEMSPACE_RAM:
case MEMSPACE_PROG:
b = **paddr;
*paddr += 1;
return b;

case MEMSPACE_EEPROM:
case MEMSPACE_SEEPROM:
case MEMSPACE_OTHER0:
case MEMSPACE_OTHER1:
case MEMSPACE_OTHER2:
case MEMSPACE_OTHER3:
default:
return 0;
}
}


PmReturn_t
plat_getByte(uint8_t *b)
{
PmReturn_t retval = PM_RET_OK;

return retval;
}


PmReturn_t
plat_putByte(uint8_t b)
{
buff[pos++] = b;
return PM_RET_OK;
}


PmReturn_t
plat_getMsTicks(uint32_t *r_ticks)
{
return PM_RET_OK;
}


void
plat_reportError(PmReturn_t result)
{

}

Because the python program uses only dict, if, loops, lists and number multiplication how should I optimize it.

I think to make the memgetbyte a inline one, also to remove the debug from the features. What will you advise me regarding your experience with PyMite.

Thanks.

Dean Hall

unread,
Nov 25, 2013, 7:19:42 PM11/25/13
to python-o...@googlegroups.com
If the main purpose of your program is to update a servo position, then my first question is if your microcontroller has a hardware timer peripheral.
If so, learn to use it. Then make a python native function that sets the timer register to adjust the servo pulse as needed.
If you let us know what hardware you're using, we might be able to help.

Don't worry about optimizing the code of memGetByte().
If you enable your compiler's optimizations, it will easily take care of it for you.

!!Dean
> --
> --
> You are subscribed to the "python-on-a-chip" (or p14p for short) Google Group.
> Site: http://groups.google.com/group/python-on-a-chip
>
> ---
> You received this message because you are subscribed to the Google Groups "python-on-a-chip" group.
> To unsubscribe from this group and stop receiving emails from it, send an email to python-on-a-ch...@googlegroups.com.
> For more options, visit https://groups.google.com/groups/opt_out.

Valeriu Balaban

unread,
Nov 26, 2013, 2:51:43 AM11/26/13
to python-o...@googlegroups.com
Ok, I didn't enabled the code optimization, I will try this. Thanks

Now I have a working C code that do the same thing, but to add a new feature I should stop the installation, reprogram the MCU, also start a new history, and then to wait for an event, which is costly.

At this moment the Python code is also updated by reprogramming and the execution of this code takes 15 ms, limit is 20 ms. For that purpose I try to optimize it.

The python code is called in a loop and after receiving data from sensors I have this line:

retval = pm_run((uint8_t *)"program");

Do you think that will be faster to block the execution in Python by calling a C function and to unblock after the data from sensor is arrived.

Thanks.

Dean Hall

unread,
Nov 26, 2013, 8:40:46 AM11/26/13
to python-o...@googlegroups.com
Yes, pm_run() has to do many things before execution, so creating and calling a C function (wrapped in Python) to block until the appropriate time would be a good idea.

!!Dean
Reply all
Reply to author
Forward
0 new messages