Get Motor Speed from Parameter

Firmware/software/electronics/mechanics
Post Reply
mcheli
Beginner
Posts: 14
Joined: Fri Oct 02, 2015 7:48 pm

Get Motor Speed from Parameter

Post by mcheli »

Hi All,

I'm looking to be able to access the motor parameter values in a program that I am writing. In summary, I want to grab the x, y and z values of the gyroscope, and the 4 motor speeds and send them in real time to a web server for analysis. I've got the send to webserver part down and I am able to access the gryo values, but am having trouble with getting motor information.

Here's where I add the variables to my code...

Code: Select all

        self._lg_stab = LogConfig(name="Stabilizer", period_in_ms=10)

        self._lg_stab.add_variable("gyro.x", "float")

        self._lg_stab.add_variable("gyro.y", "float")

        self._lg_stab.add_variable("gyro.z", "float")

        self._lg_stab.add_variable("motor.m1", "int32_t")

        self._lg_stab.add_variable("motor.m2", "int32_t")

        self._lg_stab.add_variable("motor.m3", "int32_t")

        self._lg_stab.add_variable("motor.m4", "int32_t")
Here's my JSON for those parameters...

Code: Select all

{

  "logconfig": {

    "logblock":

    {"name": "Stabilizer", "period":20,

     "variables": [

          {"name":"gyro.x", "type":"TOC", "stored_as":"float", "fetch_as":"float"},

          {"name":"gyro.y", "type":"TOC", "stored_as":"float", "fetch_as":"float"},

          {"name":"gyro.z", "type":"TOC", "stored_as":"float", "fetch_as":"float"},

          {"name":"motor.m1", "type":"TOC", "stored_as":"float", "fetch_as":"int32_t"},

          {"name":"motor.m2", "type":"TOC", "stored_as":"float", "fetch_as":"int32_t"},

          {"name":"motor.m3", "type":"TOC", "stored_as":"float", "fetch_as":"int32_t"},

          {"name":"motor.m4", "type":"TOC", "stored_as":"float", "fetch_as":"int32_t"},

     ]}

  }

}
When I do this the motor values just stay at 0. It should be noted that i'm using my computer with the crazyflie dongle for the program and my phone to fly it.

Any help would be greatly appreciated!

Full code for reference...

Code: Select all

# -*- coding: utf-8 -*-

#

#     ||          ____  _ __

#  +------+      / __ )(_) /_______________ _____  ___

#  | 0xBC |     / __  / / __/ ___/ ___/ __ `/_  / / _ \

#  +------+    / /_/ / / /_/ /__/ /  / /_/ / / /_/  __/

#   ||  ||    /_____/_/\__/\___/_/   \__,_/ /___/\___/

#

#  Copyright (C) 2014 Bitcraze AB

#

#  Crazyflie Nano Quadcopter Client

#

#  This program is free software; you can redistribute it and/or

#  modify it under the terms of the GNU General Public License

#  as published by the Free Software Foundation; either version 2

#  of the License, or (at your option) any later version.

#

#  This program is distributed in the hope that it will be useful,

#  but WITHOUT ANY WARRANTY; without even the implied warranty of

#  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the

#  GNU General Public License for more details.



#  You should have received a copy of the GNU General Public License

#  along with this program; if not, write to the Free Software

#  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,

#  MA  02110-1301, USA.



"""

Simple example that connects to the first Crazyflie found, logs the Stabilizer

and prints it to the console. After 10s the application disconnects and exits.

"""



import sys

sys.path.append("../lib")



import cflib.crtp



import logging

import time

from threading import Timer



import cflib.crtp

from cfclient.utils.logconfigreader import LogConfig

from cflib.crazyflie import Crazyflie



import requests



# Only output errors from the logging framework

logging.basicConfig(level=logging.ERROR)



class LoggingExample:

    

    """

    Simple logging example class that logs the Stabilizer from a supplied

    link uri and disconnects after 5s.

    """

    def __init__(self, link_uri):

        """ Initialize and run the example with the specified link_uri """



        # Create a Crazyflie object without specifying any cache dirs

        self._cf = Crazyflie()







        # Connect some callbacks from the Crazyflie API

        self._cf.connected.add_callback(self._connected)

        self._cf.disconnected.add_callback(self._disconnected)

        self._cf.connection_failed.add_callback(self._connection_failed)

        self._cf.connection_lost.add_callback(self._connection_lost)



        print "Connecting to %s" % link_uri



        # Try to connect to the Crazyflie

        self._cf.open_link(link_uri)



        # Variable used to keep main loop occupied until disconnect

        self.is_connected = True



    def _connected(self, link_uri):

        """ This callback is called form the Crazyflie API when a Crazyflie

        has been connected and the TOCs have been downloaded."""

        print "Connected to %s" % link_uri



        # The definition of the logconfig can be made before connecting

        self._lg_stab = LogConfig(name="Stabilizer", period_in_ms=10)

        self._lg_stab.add_variable("gyro.x", "float")

        self._lg_stab.add_variable("gyro.y", "float")

        self._lg_stab.add_variable("gyro.z", "float")

        self._lg_stab.add_variable("motor.m1", "int32_t")

        self._lg_stab.add_variable("motor.m2", "int32_t")

        self._lg_stab.add_variable("motor.m3", "int32_t")

        self._lg_stab.add_variable("motor.m4", "int32_t")



        # Adding the configuration cannot be done until a Crazyflie is

        # connected, since we need to check that the variables we

        # would like to log are in the TOC.

        self._cf.log.add_config(self._lg_stab)

        if self._lg_stab.valid:

            # This callback will receive the data

            self._lg_stab.data_received_cb.add_callback(self._stab_log_data)

            # This callback will be called on errors

            self._lg_stab.error_cb.add_callback(self._stab_log_error)

            # Start the logging

            self._lg_stab.start()

        else:

            print("Could not add logconfig since some variables are not in TOC")



        # Start a timer to disconnect in 10s

        t = Timer(5, self._cf.close_link)

        t.start()



    def _stab_log_error(self, logconf, msg):

        """Callback from the log API when an error occurs"""

        print "Error when logging %s: %s" % (logconf.name, msg)



    def _stab_log_data(self, timestamp, data, logconf):

        """Callback froma the log API when data arrives"""

        #ThingWorx connection information

        server = 'http://acadev1.cloud.thingworx.com/Thingworx/Things/'

        thing = 'MicroQuad_Thing'

        servicecall = '/Services/GetString?method=put&'

        appKey = 'appKey=4456a419-5b4d-4c8c-98a7-d88a1804f7ee'

        session = '&x-thingworx-session=true&'

        parameters = 'Data=' + str(data)



        print parameters



        r = requests.get(server + thing + servicecall + appKey + session + parameters)

        print r





    def _connection_failed(self, link_uri, msg):

        """Callback when connection initial connection fails (i.e no Crazyflie

        at the speficied address)"""

        print "Connection to %s failed: %s" % (link_uri, msg)

        self.is_connected = False



    def _connection_lost(self, link_uri, msg):

        """Callback when disconnected after a connection has been made (i.e

        Crazyflie moves out of range)"""

        print "Connection to %s lost: %s" % (link_uri, msg)



    def _disconnected(self, link_uri):

        """Callback when the Crazyflie is disconnected (called in all cases)"""

        print "Disconnected from %s" % link_uri

        self.is_connected = False



if __name__ == '__main__':

    # Initialize the low-level drivers (don't list the debug drivers)

    cflib.crtp.init_drivers(enable_debug_driver=False)

    # Scan for Crazyflies and use the first one found

    print "Scanning interfaces for Crazyflies..."

    available = cflib.crtp.scan_interfaces()

    print "Crazyflies found:"

    for i in available:

        print i[0]



    if len(available) > 0:

        le = LoggingExample(available[0][0])

    else:

        print "No Crazyflies found, cannot run example"



    # The Crazyflie lib doesn't contain anything to keep the application alive,

    # so this is where your application should do something. In our case we

    # are just waiting until we are disconnected.

    while le.is_connected:

        time.sleep(1)


mcheli
Beginner
Posts: 14
Joined: Fri Oct 02, 2015 7:48 pm

Re: Get Motor Speed from Parameter

Post by mcheli »

Still stuck on getting this to work, tried messing around with the data types quite a bit but no luck.
arnaud
Bitcraze
Posts: 2538
Joined: Tue Feb 06, 2007 12:36 pm

Re: Get Motor Speed from Parameter

Post by arnaud »

Hi,

You are doing something we did not think about (connecting both PC and phone) and it is working, nice!

I manage to get your code to work by modifying the type:

Code: Select all

        self._lg_stab.add_variable("motor.m1", "int16_t")

        self._lg_stab.add_variable("motor.m2", "int16_t")

        self._lg_stab.add_variable("motor.m3", "int16_t")

        self._lg_stab.add_variable("motor.m4", "int16_t")
and to test I disabled requests:

Code: Select all

    def _stab_log_data(self, timestamp, data, logconf):

        """Callback froma the log API when data arrives"""

        print data
        return
I then get some thrust data:

Code: Select all

{'motor.m1': 27876, 'motor.m2': -28596, 'motor.m3': -24222, 'gyro.y': -130.615234375, 'gyro.x': -20.08056640625, 'gyro.z': 15.9912109375, 'motor.m4': 28222}
{'motor.m1': 28064, 'motor.m2': -28744, 'motor.m3': -24430, 'gyro.y': -122.25341796875, 'gyro.x': -18.85986328125, 'gyro.z': 15.625, 'motor.m4': 28390}
{'motor.m1': 24710, 'motor.m2': 31718, 'motor.m3': -29202, 'gyro.y': -100.7080078125, 'gyro.x': -14.892578125, 'gyro.z': 15.80810546875, 'motor.m4': 24290}
{'motor.m1': 25177, 'motor.m2': 30967, 'motor.m3': -29843, 'gyro.y': -78.369140625, 'gyro.x': -18.4326171875, 'gyro.z': 14.09912109375, 'motor.m4': 25215}
{'motor.m1': 25777, 'motor.m2': 30113, 'motor.m3': -30559, 'gyro.y': -53.3447265625, 'gyro.x': -21.05712890625, 'gyro.z': 13.427734375, 'motor.m4': 26185}
{'motor.m1': 26536, 'motor.m2': 28792, 'motor.m3': -31934, 'gyro.y': -12.939453125, 'gyro.x': -27.03857421875, 'gyro.z': 10.55908203125, 'motor.m4': 28122}
{'motor.m1': 27045, 'motor.m2': 28087, 'motor.m3': -32325, 'gyro.y': 2.74658203125, 'gyro.x': -28.9306640625, 'gyro.z': 10.80322265625, 'motor.m4': 28709}
{'motor.m1': 27356, 'motor.m2': 27686, 'motor.m3': 32474, 'gyro.y': 18.24951171875, 'gyro.x': -28.86962890625, 'gyro.z': 9.521484375, 'motor.m4': 29536}
{'motor.m1': 27669, 'motor.m2': 27467, 'motor.m3': 31913, 'gyro.y': 28.4423828125, 'gyro.x': -26.67236328125, 'gyro.z': 8.544921875, 'motor.m4': 30003}
{'motor.m1': 19367, 'motor.m2': 18539, 'motor.m3': 22433, 'gyro.y': 41.8701171875, 'gyro.x': -24.0478515625, 'gyro.z': 7.32421875, 'motor.m4': 22033}
{'motor.m1': 19683, 'motor.m2': 18451, 'motor.m3': 22103, 'gyro.y': 46.38671875, 'gyro.x': -20.1416015625, 'gyro.z': 7.26318359375, 'motor.m4': 22135}
{'motor.m1': 20108, 'motor.m2': 18550, 'motor.m3': 21350, 'gyro.y': 53.28369140625, 'gyro.x': -12.02392578125, 'gyro.z': 6.40869140625, 'motor.m4': 22364}
{'motor.m1': 20455, 'motor.m2': 18491, 'motor.m3': 21209, 'gyro.y': 54.6875, 'gyro.x': -7.568359375, 'gyro.z': 6.53076171875, 'motor.m4': 22217}
{'motor.m1': 20657, 'motor.m2': 18547, 'motor.m3': 21041, 'gyro.y': 54.01611328125, 'gyro.x': -3.60107421875, 'gyro.z': 7.38525390625, 'motor.m4': 22127}
{'motor.m1': 20405, 'motor.m2': 19013, 'motor.m3': 21101, 'gyro.y': 44.98291015625, 'gyro.x': -0.6103515625, 'gyro.z': 6.8359375, 'motor.m4': 21853}
{'motor.m1': 20624, 'motor.m2': 19584, 'motor.m3': 20708, 'gyro.y': 40.52734375, 'gyro.x': 10.31494140625, 'gyro.z': 5.126953125, 'motor.m4': 21456}
mcheli
Beginner
Posts: 14
Joined: Fri Oct 02, 2015 7:48 pm

Re: Get Motor Speed from Parameter

Post by mcheli »

Awesome! Thanks so much for the reply :)

Quick question, should I modify my JSON to storeas and fetchas int16_t? Or should it remain the way it is currently? I'll give this a try later on today and let you know how it goes!
arnaud
Bitcraze
Posts: 2538
Joined: Tue Feb 06, 2007 12:36 pm

Re: Get Motor Speed from Parameter

Post by arnaud »

I am not sure I understand your question, json encodes only floats anyway so what you are doing now should work.

Please keep up updated, what kind of analysis do you do on the web server?
mcheli
Beginner
Posts: 14
Joined: Fri Oct 02, 2015 7:48 pm

Re: Get Motor Speed from Parameter

Post by mcheli »

Sorry for responding so late, I figured out the JSON (I just stored as float and retrieved as float) and I have data streaming! Thanks so much for the help.

I do have a major problem with the data however, by using a int16_t data type once the quadcopter starts getting to 70-80% throttle, and the 32,000 value is surpassed then the value can no longer be stored and comes to 0. The numbers we are working with are too large for the int16_t data type it seems.

I'm not my training / nature a programmer, so there may be a trick to get around this but I am unable to figure it out. Changing to int32_t results in no data.
mcheli
Beginner
Posts: 14
Joined: Fri Oct 02, 2015 7:48 pm

Re: Get Motor Speed from Parameter

Post by mcheli »

arnaud wrote:Please keep up updated, what kind of analysis do you do on the web server?
I am planning to visualize the data coming off of the quad to show the correlation between pitch and motor speed. I'm using an IoT platform (Thingworx) to show this data and analyze it.
arnaud
Bitcraze
Posts: 2538
Joined: Tue Feb 06, 2007 12:36 pm

Re: Get Motor Speed from Parameter

Post by arnaud »

The thrust is a uint16_t value, between 0 and 65535.
mcheli
Beginner
Posts: 14
Joined: Fri Oct 02, 2015 7:48 pm

Re: Get Motor Speed from Parameter

Post by mcheli »

Got it! I had to change the data type form int16_t to uint16_t and that did it.

You guys are great!
Post Reply