Skip to content

Commit

Permalink
Initial commit with project description and a basic working MPU6050 c…
Browse files Browse the repository at this point in the history
…ode in raspberrypi-server.
  • Loading branch information
FlorentRevest committed Aug 13, 2013
1 parent c513ec1 commit f8d17be
Show file tree
Hide file tree
Showing 6 changed files with 228 additions and 57 deletions.
55 changes: 0 additions & 55 deletions LICENSE
Original file line number Diff line number Diff line change
Expand Up @@ -617,58 +617,3 @@ reviewing courts shall apply local law that most closely approximates
an absolute waiver of all civil liability in connection with the
Program, unless a warranty or assumption of liability accompanies a
copy of the Program in return for a fee.

END OF TERMS AND CONDITIONS

How to Apply These Terms to Your New Programs

If you develop a new program, and you want it to be of the greatest
possible use to the public, the best way to achieve this is to make it
free software which everyone can redistribute and change under these terms.

To do so, attach the following notices to the program. It is safest
to attach them to the start of each source file to most effectively
state the exclusion of warranty; and each file should have at least
the "copyright" line and a pointer to where the full notice is found.

{one line to give the program's name and a brief idea of what it does.}
Copyright (C) {year} {name of author}

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 3 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, see {http://www.gnu.org/licenses/}.

Also add information on how to contact you by electronic and paper mail.

If the program does terminal interaction, make it output a short
notice like this when it starts in an interactive mode:

Raspcopter Copyright (C) 2013 Florent Revest
This program comes with ABSOLUTELY NO WARRANTY; for details type `show w'.
This is free software, and you are welcome to redistribute it
under certain conditions; type `show c' for details.

The hypothetical commands `show w' and `show c' should show the appropriate
parts of the General Public License. Of course, your program's commands
might be different; for a GUI interface, you would use an "about box".

You should also get your employer (if you work as a programmer) or school,
if any, to sign a "copyright disclaimer" for the program, if necessary.
For more information on this, and how to apply and follow the GNU GPL, see
{http://www.gnu.org/licenses/}.

The GNU General Public License does not permit incorporating your program
into proprietary programs. If your program is a subroutine library, you
may consider it more useful to permit linking proprietary applications with
the library. If this is what you want to do, use the GNU Lesser General
Public License instead of this License. But first, please read
{http://www.gnu.org/philosophy/why-not-lgpl.html}.
27 changes: 25 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,27 @@
Raspcopter
Repository description
==========

Source code of my Quadcopter drone powered by a Raspberry Pi.
This git repo contains three directories, all of them are related to the same project: A home-made quadcopter running a Raspberry Pi.

* raspberrypi-server: This software, made in C++ is a flight controller intended to be run on the R-Pi, it basically do three things:
- it collects the values from the accelerometer and gyroscope via i2c (magnetometer and barometer may be added later)
- it filters these values via a Kallman or low-pass filter
- it manages the motors speed via the Pololu Maestro board according to the above values
- it communicates over wifi with one of the two clients below
* android-client: The Android smartphone is a wifi hotspot with whom the R-Pi communicates. An app (in java) will also be made to provide a handy controller.
* laptop-client: This client, written in C++ with Qt is intended to be a complete "ground station" with recorded logs and real-time informations on the quadcopter. The laptop will also connect to the Android smartphone to be on the same local network as the raspberry pi.

## Used pieces
* 4 Motors: [FC 28-22 Brushless Outrunner 1200kv](vhttp://www.hobbyking.com/hobbyking/store/uh_viewitem.asp?idproduct=5354)
* 4 ESC(Electronic Speed Controllers): [Hobby King 20A ESC 3A UBEC](http://www.hobbyking.com/hobbyking/store/__15202__hobby_king_20a_esc_3a_ubec.html) flashed in a hacky way with [SimonK](http://www.rchacker.com/diy/simonk-esc-firmware-flashing)
* 1 Propellers pack (two of each directions): [Slow Fly Electric Prop 1045SF (4 pc)]( http://www.hobbyking.com/hobbyking/store/__22446__Slow_Fly_Electric_Prop_1045SF_4_pc_.html)
* 1 Frame: [Hobbyking X525 V3 Glass Fiber Quadcopter Frame 600mm](http://www.hobbyking.com/hobbyking/store/__22800__hobbyking_x525_v3_glass_fiber_quadcopter_frame_600mm.html)
* 1 Battery: [Turnigy 2200mAh 3S 20C Lipo Pack](http://www.hobbyking.com/hobbyking/store/__8932__Turnigy_2200mAh_3S_20C_Lipo_Pack.html)
* 1 Battery charger: [Turnigy 2S 3S Balance Charger. Direct 110/240v Input](http://www.hobbyking.com/hobbyking/store/uh_viewitem.asp?idproduct=8247)

(the pieces above have been commanded at the same time from HobbyKing despit their non-professionalism... I waited this pack a month and a half)

* An ESC/servos USB communication board: [Micro Maestro 6-Channel USB Servo Controller](http://www.pololu.com/catalog/product/1350)
* A low battery alarm: [Lipo Battery alarm](http://www.ebay.fr/itm/2S-4S-7-4V-14-8V-RC-Lipo-Batterie-Tension-Faible-Testeur-Buzzer-Alarme-autonomie-/130732900168?pt=FR_YO_Jeux_RadioComRobots_VehiculesRadiocommandes&hash=item1e7049bf48)
* Accelerometer and gyroscope: [MPU6050 board](http://www.ebay.fr/itm/MPU-6050-Module-3-Axis-Gyroscope-Accelerometer-Sensor-for-Arduino-DIY-/400487483979?pt=AU_B_I_Electrical_Test_Equipment&hash=item5d3eea0a4b#ht_3042wt_1166)
* Connections: [Some cables used in the SimonK flashing process and in the MPU6050 communication](http://www.ebay.fr/itm/40-Cables-pour-BreadBoard-Femelle-Femele-Arduino-Femalle-Fils-jumper-Wire-/330902593748?pt=FR_YO_MaisonJardin_Bricolage_ElectroniqueComposants&hash=item4d0b54e0d4#ht_2467wt_1406)
90 changes: 90 additions & 0 deletions raspberry-server/Accelerometer.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
/* ====================================================================
* This file is part of Raspcopter.
*
* Copyright (C) 2013 - Florent Revest <florent.revest666@gmail.com>
* Raspcopter 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 3 of the License, or
* (at your option) any later version.
*
* Raspcopter 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 Raspcopter. If not, see <http://www.gnu.org/licenses/>.
* ================================================================== */

#include "Accelerometer.h"

Accelerometer::Accelerometer()
{
m_isConnected = true;
m_i2cfd = open("/dev/i2c-1", O_RDWR);
if(!m_i2cfd < 0)
m_isConnected = false;
if(ioctl(m_i2cfd, I2C_SLAVE, 0x68) < 0)
m_isConnected = false;

setRegisterValue(0x6B, 2, 3, 1); // Choose the XGYRO clock
setRegisterValue(0x1B, 4, 2, 1); // Set gyroscopes range to 500
setRegisterValue(0x1C, 4, 2, 2); // Set accelerometer range to 8
setRegisterValue(0x6B, 6, 1, 0); // Disable Sleep Mode
}

Accelerometer::~Accelerometer()
{
close(m_i2cfd);
setRegisterValue(0x6B, 6, 1, 1); // Disable Sleep Mode
}

AccelGyroValues Accelerometer::getAccelGyroValues()
{
AccelGyroValues values;
uint8_t buffer[14];
getRegisterValue(0x3B, 14, buffer);

values.ax = (((int16_t)buffer[0]) << 8) | buffer[1];
values.ay = (((int16_t)buffer[2]) << 8) | buffer[3];
values.az = (((int16_t)buffer[4]) << 8) | buffer[5];
values.gx = (((int16_t)buffer[8]) << 8) | buffer[9];
values.gy = (((int16_t)buffer[10]) << 8) | buffer[11];
values.gz = (((int16_t)buffer[12]) << 8) | buffer[13];

return values;
}

bool Accelerometer::isConnected()
{
return m_isConnected;
}

// These two methods contain some code written by Jeff Rowberg for the I2Cdev" lib
void Accelerometer::setRegisterValue(uint8_t regAddr, uint8_t bitStart, uint8_t length, uint8_t data)
{
// Read the old value
uint8_t value;
getRegisterValue(regAddr, 1, &value);

// Mix the new bits with the new ones
uint8_t mask = ((1 << length) - 1) << (bitStart - length + 1);
data <<= (bitStart - length + 1);
data &= mask;
value &= ~(mask);
value |= data;

// Write the new value to /dev/i2c-1
uint8_t buffer[2] = {regAddr, value}; // The first octet is the register adress, the second contains the data
write(m_i2cfd, buffer, 2);
}

void Accelerometer::getRegisterValue(uint8_t regAddr, uint8_t length, uint8_t *data)
{
if(write(m_i2cfd, &regAddr, 1) != 1)
return;
int8_t count = read(m_i2cfd, data, length);
if (count < 0 || count != 1)
return;
}
54 changes: 54 additions & 0 deletions raspberry-server/Accelerometer.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
/* ====================================================================
* This file is part of Raspcopter.
*
* Copyright (C) 2013 - Florent Revest <florent.revest666@gmail.com>
* Raspcopter 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 3 of the License, or
* (at your option) any later version.
*
* Raspcopter 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 Raspcopter. If not, see <http://www.gnu.org/licenses/>.
* ================================================================== */

#ifndef _ACCELEROMETER_H_
#define _ACCELEROMETER_H_

#include <linux/i2c-dev.h>
#include <sys/ioctl.h>
#include <sys/types.h>
#include <fcntl.h>
#include <unistd.h>
#include <stdint.h>

typedef struct AccelGyroValues {
int16_t ax;
int16_t ay;
int16_t az;
int16_t gx;
int16_t gy;
int16_t gz;
} AccelGyroValues;

class Accelerometer
{
public:
Accelerometer();
~Accelerometer();
void setRegisterValue(uint8_t regAddr, uint8_t bitStart, uint8_t length, uint8_t data);
void getRegisterValue(uint8_t regAddr, uint8_t length, uint8_t *data);
AccelGyroValues getAccelGyroValues();
bool isConnected();

private:
int m_i2cfd;
bool m_isConnected;
};

#endif // _ACCELEROMETER_H_
18 changes: 18 additions & 0 deletions raspberry-server/Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
CC=g++
CFLAGS=-c -Wall
LDFLAGS=
SOURCES=main.cpp Accelerometer.cpp
OBJECTS=$(SOURCES:.cpp=.o)
EXECUTABLE=server

all: $(SOURCES) $(EXECUTABLE)

$(EXECUTABLE): $(OBJECTS)
$(CC) $(LDFLAGS) $(OBJECTS) -o $@

.cpp.o:
$(CC) $(CFLAGS) $< -o $@

clean:
rm -rf *.o server

41 changes: 41 additions & 0 deletions raspberry-server/main.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
/* ====================================================================
* This file is part of Raspcopter.
*
* Copyright (C) 2013 - Florent Revest <florent.revest666@gmail.com>
* Raspcopter 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 3 of the License, or
* (at your option) any later version.
*
* Raspcopter 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 Raspcopter. If not, see <http://www.gnu.org/licenses/>.
* ================================================================== */

#include <iostream>
#include "Accelerometer.h"

int main(int argc, char *argv[])
{
Accelerometer *accel= new Accelerometer(); // Initialize the MPU6050
if(accel->isConnected())
std::cout << "Accelerometer initialisation successful." << std::endl;
else
{
std::cout << "Accelerometer initialisation error... Abort" << std::endl;
return 1;
}

while(1)
{
AccelGyroValues values = accel->getAccelGyroValues();
std::cout << "accelerometer: " << values.ax << values.ay << values.az << std::endl
<< "gyrometer: " << values.gx << values.gy << values.gz << std::endl;
}
return 0;
}

0 comments on commit f8d17be

Please sign in to comment.