Python Forum
There is something about functions that i do not know!
Thread Rating:
  • 1 Vote(s) - 5 Average
  • 1
  • 2
  • 3
  • 4
  • 5
There is something about functions that i do not know!
#1
My project is to use the MegaPi board to balance the "Balancing Robot" while a Raspberry Pi 3B+ is left available for other tasks. The balancing code is already on the board in the firmware. The firmware has a feature to change the "megapi_mode" in case "COMMON_COMMONCMD". However, there is no corresponding method in Python_for_MegaPi to access this case.
@allaincedric
allaincedric has respnoded that

> @clplaneguy Python_for_MegaPi is a set of .py file, you can write your own, edit the file installed on your machine copy it to change it or create new class from existing api. At the end if you know the bytes you need to send you can just add a function to the api creating a new class from the existing one.
>
> `python
> from megapi import *
>
> class YourMegaPi(MegaPi):
>
> def __init__(self):
> MegaPi.__init__(self)
>
> def MegaPi_Mode(self, megapi_mode,speed):
> deviceId = 60;
> self.__writePackage(bytearray([0xff, 0x55, 0x07, 0x00, 0x02, deviceId, 0x12, megapi_mode]+self.short2bytes(speed)))
>
> if __name__ == '__main__':
>
> bot=YourMegaPi()
> bot.start()
> bot.MegaPi_Mode(2,100);
> bot.close();
> `

allaincedric uses Python 3! I am firmly embedded in Python 2! His code may work for him, but it does not work for me. This code does not execute, never exits and does not produce an error message. I have added print statements to at least locate where execution halts.

`
from megapi import *

class YourMegaPi(MegaPi):

def __init__(self):
MegaPi.__init__(self)

def MegaPi_Mode(self, megapi_mode,speed):
deviceId = 60;
self.__writePackage(bytearray([0xff, 0x55, 0x07, 0x00, 0x02, deviceId, 0x12, megapi_mode]+self.short2bytes(speed)))

if __name__ == '__main__':

print("Program started");
bot=YourMegaPi()
print("bot=YourMegaPi()");
bot.start()
print("bot.start()");
bot.MegaPi_Mode(2,100);
print("bot.MegaPi_Mode(2,100);");
bot.close();
print("All done");
`

His line "bot.MegaPi_Mode(2,100); is what stops execution. I have been experimenting with and adding code in an attempt to find a working combination without success.
I have studied and learned most of the firmware. I think.
I have studied and learned most of PythonForMegaPi. I think.
Now, I am studying and learning Python2 which seems to be my week point!

`
import sys
print("sys.platform is: " + sys.platform);

from megapi import *

print("\r\nThis program is a test of the Raspberry's attempt to run the MegaPi's firmware to balance the robot");
print("\r\n2/4/2019");

class YourMegaPi(MegaPi):
def __init__(self):
MegaPi.__init__(self)

def MegaPi_ModeA(self, megapi_mode,speed): # Routine Alfa
print("\r\nInside 'MegaPi_A'")
deviceId = 60;
speed = 0;
bot.encoderMotorRun(3,100);
sleep(1);
bot.encoderMotorRun(3, 0);
#bot.__writePackage(bytearray( [0xff, 0x55, 0x07, 0x00, 0x02, deviceId, 0x12, megapi_mode ] + self.short2bytes(speed) ) )
print("\r\nExiting 'MegaPi_A'")

#def MegaPi_ModeB(self, megapi_modeA): # Routine Beta
def MegaPi_ModeB(self): # Routine Beta
print("\r\nInside 'MegPi_ModeB'")
deviceId = 60; # COMMON_COMMONCMD
deviceId = 62; # ENCODER_PID_MOTION encoderMotor
speed = 0;
slot = 3;
bot.encoderMotorRun(3,100);
sleep(1);
bot.encoderMotorRun(3, 0);
print("\r\nMotors Run")
bot.__writePackage(bytearray( [0xff, 0x55, 0x07, 0x00, 0x02, deviceId, 0x02, 3 ] + bot.short2bytes(speed) ) );
#self.__writePackage(bytearray( [0xff, 0x55, 0x07, 0x00, 0x02, deviceId, 0x02, 3 ] + self.short2bytes(speed) ) );
print("\r\nExiting 'MegaPi_ModeB'")

def MotorTest(self): # MotorTest
print("\r\nInside 'MotorTest'")
bot.encoderMotorRun(3,100);
sleep(1);
bot.encoderMotorRun(3, 0);
sleep(1);

#def COMMON_COMMONCMD (self,slot,speed): subcmd=0x12, megapi_mode=?
#def MegaPi_Mode (megapi_mode):
# deviceId = 60;
## uint8_t port = readBuffer(6);
# Number count 1 2 3 4 5 6 7 8 9
# Buffer Position 0 1 2 3 4 5 6 7 8
# Definition/Output ff 55 len idx action device port slot data_a
# subcmd cmd_data
# megapi_mode
# self.__writePackage(bytearray( [0xff, 0x55, 0x07, 0x00, 0x02, deviceId, 0x12, megapi_mode ] + self.short2bytes(speed) ) )
#bot.encoderMotorRun(3,100);
#sleep(1);
#bot.encoderMotorRun(3, 0);
#bot.__writePackage(bytearray( [0xff, 0x55, 0x07, 0x00, 0x02, deviceId, 0x12, megapi_mode ] + self.short2bytes(speed) ) )
print("\r\nExiting 'MotorTest'")



if __name__ == '__main__':

print("\r\nInside 'Main program'")
bot=YourMegaPi()
# bot=MegaPi()
bot.start()
megapi_modeb = 2;
speed = 0;
#bot.encoderMotorRun(3,100);
#sleep(1);
#bot.encoderMotorRun(3, 0);
#sleep(1);
bot.MotorTest();
bot.MotorTest();
print("After MotorTest");
bot.MegaPi_ModeB();
print("MegaPi_ModeB");
bot.__writePackage(bytearray([0xff,0x55,0x07,0x00,0x02,62,0x02,slot]+self.short2bytes(100)));
sleep(1);
bot.__writePackage(bytearray([0xff,0x55,0x07,0x00,0x02,62,0x02,slot]+self.short2bytes( 0)));

#bot.MegaPi_ModeA(2,100); # For routine Alfa
#bot.MegaPi_ModeB(2); # For routine Beta
#bot.__writePackage(bytearray( [0xff, 0x55, 0x07, 0x00, 0x02, deviceId, 0x12, megapi_mode ] + self.short2bytes(speed) ) )
#bot.__writePackage(bytearray([0xff,0x55,0x07,0x00,0x02,60,0x12,megapi_mode]+self.short2bytes(speed)))
self.__writePackage(bytearray([0xff,0x55,0x07,0x00,0x02,deviceId,0x02,slot]+self.short2bytes(speed)))

bot.close();
sys.exit("Execution complete")


#bot = MegaPi()
#bot.start('/dev/ttyAMA0')
#bot.start()


#MegaPi_Mode(2);


#def COMMON_COMMONCMD (self,slot,speed): subcmd=0x12, megapi_mode=?
#def MegaPi_Mode (megapi_mode):
# deviceId = 60;
# uint8_t port = readBuffer(6);
# Number count 1 2 3 4 5 6 7 8 9
# Buffer Position 0 1 2 3 4 5 6 7 8
# Definition/Output ff 55 len idx action device port slot data_a
# subcmd cmd_data
# megapi_mode
#self.__writePackage( bytearray( [0xff, 0x55, 0x07, 0x00, 0x02, deviceId, 0x12, megapi_mode ] + self.short2bytes(speed) ) )
# bot.device._writePackge ( bytearray( [0xff, 0x55, 0x07, 0x00, 0x02, deviceId, 0x12, megapi_mode ] + self.short2bytes(speed) ) )
# Buffer

#from megapi import *
#
#class YourMegaPi(MegaPi):
# def MegaPi_Mode(self, megapi_mode,speed):
# deviceId = 60;
# self.__writePackage(bytearray([0xff, 0x55, 0x07, 0x00, 0x02, deviceId, 0x12, megapi_mode]+self.short2bytes(speed)))
#
#if __name__ == '__main__':
#
# bot=YourMegaPi()
# bot.start()
# bot.MegaPi_Mode(2,100);
# bot.close();
`

Whatever the correct code is, it is beyond my me.

**Can any one help?**
Reply


Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020