Megapi Python Problem


#1

Hello, I need urgent help
How can I transfer the value from the ultrasonic sensor and compass to the main program in Python?
I get the value of the ultrasonic sensor and Compass not in the main loop?
What am I doing wrong ?
Is there an example of this?

from megapi import *
import time, os, random


    def onRead(v):
        global Ultraschall;
        Ultraschall = v;

    def onRead2(v):
        global compass;
        Compass = v;
        


    def main():
        bot = MegaPi()
        bot.start()
        sleep(1);
        while 1:
            bot.ultrasonicSensorRead(7,onRead);
            bot.compassReadRead(8,onRead2);
            time.sleep( 2 )
            print "distance:"+str(Ultraschall)+" cm";
            if Ultraschall > 50:
                bot.encoderMotorRun(1,100);
                bot.encoderMotorRun(2,-100);
            else:
                bot.encoderMotorRun(1,0);
                bot.encoderMotorRun(2,0);
            if Compass < 100:
                bot.encoderMotorRun(1,100);
                bot.encoderMotorRun(2,-100);
                else:
                bot.encoderMotorRun(1,0);
                bot.encoderMotorRun(2,0);
                

                    
    if __name__ == '__main__':      
        main()

#2

Hi, were you able to solve this problem?


#3

@tec_support - The callbacks in python are not working.
bot.ultrasonicSensorRead(6,onRead); It seems like its not calling the function onRead at all


#4