Skip to content

Commit

Permalink
Motor driver has been created with the help from another project.
Browse files Browse the repository at this point in the history
There is test3.cpp which gives an example how it works.
http://sourceforge.net/p/bonelib/wiki/Home/
  • Loading branch information
root committed Apr 4, 2013
1 parent 6aeb31f commit d2a987b
Show file tree
Hide file tree
Showing 11 changed files with 2,164 additions and 0 deletions.
6 changes: 6 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,11 @@ include_directories("./include")

add_library(${PROJECT_NAME} src/servo.cpp)

add_library(motordriver src/motordriver.cpp src/motorpwm.cpp src/gpio.cpp src/pinmux.cpp)


add_executable(test1 src/test1.cpp)
target_link_libraries(test1 ${PROJECT_NAME})

add_executable(test3 src/test3.cpp)
target_link_libraries(test3 motordriver)
9 changes: 9 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,12 @@ Written in C++ although needs setPWMReg.py to be run after booting the BeagleBon

To build:
mkdir build && cd build && cmake .. && make

These files are
src/gpio.cpp
src/pinmux.cpp
includes/gpio.hpp
includes/pinmux.hpp

copied from this project:
http://sourceforge.net/p/bonelib/wiki/Home/
81 changes: 81 additions & 0 deletions include/gpio.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
//
// Copyright (c) 2012 Janick Bergeron
// All Rights Reserved
//
// Licensed under the Apache License, Version 2.0 (the
// "License"); you may not use this file except in
// compliance with the License. You may obtain a copy of
// the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in
// writing, software distributed under the License is
// distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
// CONDITIONS OF ANY KIND, either express or implied. See
// the License for the specific language governing
// permissions and limitations under the License.
//

#ifndef __BONELIB_GPIO__
#define __BONELIB_GPIO__

#include "pinmux.hpp"

namespace BeagleBone {

/** Class representing a GPIO pin */
class gpio: public pin
{
friend class pin;

public:
/** Get the GPIO pin corresponding to a connector pin.
* Returns NULL if it does not support GPIO or it cannot be muxed to its GPIO function.
*/
static gpio* P8(unsigned char n);
static gpio* P9(unsigned char n);

/** Configure the direction (and pull resistors) of the GPIO pin */
int configure(pin::direction_t dir, pin::pull_t pulls = pin::NONE);

/** Get the current direction of the GPIO pin */
pin::direction_t get_direction();

/** Get the pull resistors of the GPIO pin */
pin::pull_t get_pulls();

/** Set the value of the GPIO pin. Return TRUE if successful. */
int set(unsigned char val);

/** Get the value of the GPIO pin */
unsigned char get();

private:
unsigned char m_number;
char* m_dev;
char* m_dev_append;

gpio(const char* name,
const char* pin_dev,
unsigned char port_no,
unsigned char pin_no,
pin_fct* const mode0,
pin_fct* const mode1,
pin_fct* const mode2,
pin_fct* const mode3,
pin_fct* const mode4,
pin_fct* const mode5,
pin_fct* const mode6,
pin_fct* const mode7,
int init,
direction_t dir = IN,
pull_t pulls = NONE);

/** Export the GPIO function of the pin. Returns TRUE on success. */
int pin_xport();
};

}

#endif
55 changes: 55 additions & 0 deletions include/motordriver.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
/*
BSD License
Copyright © 2013, Bence Magyar
All rights reserved.
Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:
Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.
Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution.
Neither the name of the owner nor the names of its contributors may be used to endorse or promote products derived from this software without specific prior written permission.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/

#ifndef __MOTORDRIVER_H_
#define __MOTORDRIVER_H_



#include <fstream>
#include <string>

#include "gpio.hpp"
#include "motorpwm.h"
/**
* \author Bence Magyar
* \year 2013
* \brief This is a class implementing an Arduino-style servo interface for the BeagleBone. PWM control is used through the filesystem interface of Angstrom and Debian systems.
**/
class MotorDriver
{
private:
MotorPwm motor1;
MotorPwm motor2;
BeagleBone::gpio* motor1dir;
BeagleBone::gpio* motor2dir;

public:
MotorDriver();
~MotorDriver();
void init();
void detach();
void forward(int milisec, int dutypercent);
void backward(int milisec, int dutypercent);
void turnleft(int milisec, int dutypercent);
void turnright(int milisec, int dutypercent);

void stop();

std::string toString() const;

//private:

};

#endif


104 changes: 104 additions & 0 deletions include/motorpwm.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,104 @@
/*
BSD License
Copyright © 2013, Bence Magyar
All rights reserved.
Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:
Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.
Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution.
Neither the name of the owner nor the names of its contributors may be used to endorse or promote products derived from this software without specific prior written permission.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/

#ifndef __MOTORPWM_H_
#define __MOTORPWM_H_

#define MAX_SPEED 78
#define MIN_SPEED 10
#define PWM_FREQUENCY 8000 //hz

#define SYSFS_EHRPWM_PREFIX "/sys/class/pwm/"
#define SYSFS_EHRPWM_SUFFIX_A ":0"
#define SYSFS_EHRPWM_DUTY_PERC "duty_percent"
#define SYSFS_EHRPWM_PERIOD_FREQUENCY "period_frequency"
#define SYSFS_EHRPWM_POLARITY "polarity"
#define SYSFS_EHRPWM_RUN "run"
#define SYSFS_EHRPWM_REQUEST "request"


#include <fstream>
#include <string>

/**
* \author Bence Magyar
* \year 2013
* \brief This is a class implementing an Arduino-style servo interface for the BeagleBone. PWM control is used through the filesystem interface of Angstrom and Debian systems.
**/
class MotorPwm
{
private:
std::string _pin;
bool _attached;
double _lastValue;

/*****************************************
*
* sysfs tree:
*
ehrpwm.0:0
¦   +-- duty_ns
¦   +-- period_ns
¦   +-- polarity
¦   +-- request
¦   +-- run
+-- ehrpwm.0:1 -> ../../devices/platform/omap/ehrpwm.0/pwm/ehrpwm.0:1
¦   +-- duty_ns
¦   +-- period_ns
¦   +-- polarity
¦   +-- request
¦   +-- run
*
Define files to match sysfs tree:
*/

std::ofstream _sysfsfid_duty;
std::ofstream _sysfsfid_period;
//std::ofstream _sysfsfid_polarity;
std::ofstream _sysfsfid_run;
std::fstream _sysfsfid_request;

std::string _filename_request;
std::string _filename_duty;
std::string _filename_period;
//std::string _filename_polarity;
std::string _filename_run;
int _duty;
static const int _PERIOD = PWM_FREQUENCY;
int _polarity;
int _run;
bool forced;
static std::string pinToFile(const std::string& pin);


public:
MotorPwm();
~MotorPwm();
void attach(const std::string& pin);
//void init();
void write(int value);
void writeMicroseconds(int value);
int read() const;
bool attached() const;
void stop();
void detach();
static void enablepwm();
std::string toString() const;

private:
void set_request(const int val);
void set_duty(const int val);
void set_period(const int val);
void set_run(const int val);
};

#endif
Loading

0 comments on commit d2a987b

Please sign in to comment.