Flying a crazyflie using a CSV file

Discussions and questions about the Crazyflie Nano Quadcopter
Post Reply
youngbin1130
Member
Posts: 46
Joined: Thu Dec 12, 2019 4:28 am

Flying a crazyflie using a CSV file

Post by youngbin1130 »

Hi again,

Recently I was able to import a CSV file which contains a linear velocity and angular velocity. ( [Linear Velocity, Angular Velocity] ) (from a value of -1 to 1)

The CSV file supposedly moves the crazyflie in a circle. While I do get a circular motion, it's not a smooth movement but rather stops after each line being read. I suspect it has something to do how I am certain commands. I've attached the code I've used for this in this thread.

Code: Select all

# -*- coding: utf-8 -*-
#
#     ||          ____  _ __
#  +------+      / __ )(_) /_______________ _____  ___
#  | 0xBC |     / __  / / __/ ___/ ___/ __ `/_  / / _ \
#  +------+    / /_/ / / /_/ /__/ /  / /_/ / / /_/  __/
#   ||  ||    /_____/_/\__/\___/_/   \__,_/ /___/\___/
#
#  Copyright (C) 2017 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.
"""
This script shows the basic use of the MotionCommander class.

Simple example that connects to the crazyflie at `URI` and runs a
sequence. This script requires some kind of location system, it has been
tested with (and designed for) the flow deck.

The MotionCommander uses velocity setpoints.

Change the URI variable to your Crazyflie configuration.
"""
import logging
import time

import cflib.crtp
from cflib.crazyflie import Crazyflie
from cflib.crazyflie.syncCrazyflie import SyncCrazyflie
from cflib.positioning.motion_commander import MotionCommander
from cflib.crazyflie.log import LogConfig
from cflib.crazyflie.syncLogger import SyncLogger

import csv

URI = 'radio://0/33/1M'


# Only output errors from the logging framework
logging.basicConfig(level=logging.ERROR)

log_config = LogConfig(name='test log', period_in_ms=100)
log_config.add_variable('stateEstimate.x', 'float')
log_config.add_variable('stateEstimate.y', 'float')
log_config.add_variable('stateEstimate.z', 'float')

if __name__ == '__main__':
    # Initialize the low-level drivers (don't list the debug drivers)
    cflib.crtp.init_drivers(enable_debug_driver=True)

    with SyncCrazyflie(URI, cf=Crazyflie(rw_cache='./cache')) as scf:
        # We take off when the commander is created
        with MotionCommander(scf, default_height=0.5) as mc: 
            time.sleep(3)

            """
            # Forward and Backward Test
            with open('D:\DSO-AISwarm\InitialSampleData\ActionTest(1AgtforSUTD_test)(MovForward) - Copy.txt') as csv_file:
                csv_reader = csv.reader(csv_file, delimiter=' ')
                line_count = 0
                for row in csv_reader:
                    velocity = float(row[0]) * 0.1
                    
                    if velocity > 0:
                        mc.forward(0.2, velocity=velocity)
                        print('forward')

                    if velocity < 0:
                        velocity = -1.0 * velocity
                        mc.back(0.2, velocity=velocity)
                        print('backward')

                    line_count += 1
                    print(row[0]+' ' + row[1])
                mc.stop

            # Rotate Test
            with open('D:\DSO-AISwarm\InitialSampleData\ActionTest(1AgtforSUTD_test)(RotatClockw) - Copy.txt') as csv_file:
                print('brought in rotate')
                csv_reader = csv.reader(csv_file, delimiter=' ')
                line_count = 0
                for row in csv_reader:
                    angle = float(row[1]) * 45

                    if angle > 0:
                        mc.turn_right(angle)

                    if angle < 0:
                        angle = -1.0 * angle
                        mc.turn_left(angle)

                    print(row[0]+' ' + row[1])
                    #mc.turn_right(angle,angle)
                    line_count += 1
                mc.stop()
            """

            with open('D:\DSO-AISwarm\InitialSampleData\Record(1AgtforSUTD_test)_2ndHalf.txt') as csv_file:
                # print('brought in data')
                csv_reader = csv.reader(csv_file, delimiter=' ')
                line_count = 0
                for row in csv_reader:

                    velocity_v = float(row[0]) * 0.05
                    rotation = float(row[1])*5

                    if velocity_v > 0:
                        mc.forward(distance_m= velocity_v,velocity= velocity_v)

                        if rotation > 0:
                            mc.turn_right(rotation)
                            line_count += 1

                        elif rotation < 0:
                            rotation = -1.0 * rotation
                            mc.turn_left(5,rotation)
                            line_count += 1

                    elif velocity_v < 0:

                        velocity_v = -1.0 * velocity_v
                        mc.back(distance_m= velocity_v,velocity= velocity_v)

                        if rotation > 0:
                            mc.turn_right(rotation)
                            line_count += 1

                        elif rotation < 0:
                            rotation = -1.0 * rotation
                            mc.turn_left(5,rotation)
                            line_count += 1

                    print(line_count)

                mc.stop()

            """
            # There is a set of functions that move a specific distance
            # We can move in all directions
            mc.forward(0.3)
            mc.back(0.3)
            time.sleep(1)
  
            mc.up(0.3)
            mc.down(0.3)
            time.sleep(1)

            # We can also set the velocity
            mc.right(0.3, velocity=0.5)
            time.sleep(1)
            mc.left(0.3, velocity=0.4)
            time.sleep(1)

            # We can do circles or parts of circles
            mc.circle_right(0.5, velocity=0.5, angle_degrees=180)

            # Or turn
            mc.turn_left(90)
            time.sleep(1)

            # We can move along a line in 3D space
            mc.move_distance(-1, 0.0, 0.5, velocity=0.6)
            time.sleep(1)

            # There is also a set of functions that start a motion. The
            # Crazyflie will keep on going until it gets a new command.

            mc.start_left(velocity=0.5)
            # The motion is started and we can do other stuff, printing for
            # instance
            for _ in range(5):
                print('Doing other work')
                time.sleep(0.2)

            print('Connected')
            """
            # And we can stop
            #mc.stop()

            # We land when the MotionCommander goes out of scope
Also, I would like to ask if it is possible to achieve a smoother movement using the CSV file or would it be better to refer to the trajectory maker using which is provided by whoenig? Additionally, would it be possible to control the crazyfie with using only the velocity (linear and angular) without the distance and target angle? I would assume it would just make the crazyflie go wild because it won't know when to end.
kimberly
Bitcraze
Posts: 1050
Joined: Fri Jul 06, 2018 11:13 am

Re: Flying a crazyflie using a CSV file

Post by kimberly »

The crazyflie will handle those setpoints as seperate setpoints, so I would indeed expect that type of behavior from what you described

It would be better to use a trajectory maker. Checkout out this blogpost on how you can use the bezier curve trajectory generator we provide in our python examples: https://www.bitcraze.io/2019/08/creatin ... er-curves/
youngbin1130
Member
Posts: 46
Joined: Thu Dec 12, 2019 4:28 am

Re: Flying a crazyflie using a CSV file

Post by youngbin1130 »

Hi Kimberly,

Thanks for your reply once again.

The effect I'm trying to achieve is making a circular motion with only velocity (both linear and angular) in real time. If I'm understanding the forum post for the trajectory maker, it uses a 7th degree polynomial function so I'm not sure if that will work for my case unless we can control the drone only using the velocity values (which I believe is the 2nd order?) from the bezier curve.

Also just to clarify, if I want my Crazyflie to rotate while moving forward while moving forward, should I use the turn_left/turn_right or start_turn_left/start_turn_right? I understand that turn_left/turn_right should be used when the drone is stationary but does using start_turn_left/start_turn_right help the drone move in an arc when it's moving forward? Or is there any other function or way I can achieve this (so far I've taken a look at commander.py and I could use it.)

Cheers and stay safe.
kristoffer
Bitcraze
Posts: 630
Joined: Tue Jun 30, 2015 7:47 am

Re: Flying a crazyflie using a CSV file

Post by kristoffer »

You can maybe find some inspiration in the flowsequenceSync.py example. It uses the hover setpoint (absolute Z, velocity X and Y) and flies two smooth circles (a figure 8). It sets a constant velocity forward (in the Crazyflie reference frame) and a constant yaw rate.
youngbin1130
Member
Posts: 46
Joined: Thu Dec 12, 2019 4:28 am

Re: Flying a crazyflie using a CSV file

Post by youngbin1130 »

This is exactly what I needed! Thanks kristoffer!
Post Reply