Merge e3715918fb289b74b137bab7921ff541afe009da into ef81a2014004bc5a6bd8df19f9edd105a122db2b

This commit is contained in:
Deleted user 2018-07-24 21:39:48 +00:00 committed by GitHub
commit 8cf9e034c5
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 23 additions and 17 deletions

View File

@ -1,7 +1,8 @@
/* /*
* The MIT License (MIT) * The MIT License (MIT)
* *
* Author: Oussema Harbi <oussema.elharbi@gmail.com> * Authors: Oussema Harbi <oussema.elharbi@gmail.com> and
* Neuber Jose de Sousa <neuberfran@gmail.com>
* Copyright (c) <2016> <Oussema Harbi> * Copyright (c) <2016> <Oussema Harbi>
* *
* Permission is hereby granted, free of charge, to any person obtaining a copy of * Permission is hereby granted, free of charge, to any person obtaining a copy of
@ -32,6 +33,8 @@
#include "smartdrive.hpp" #include "smartdrive.hpp"
using namespace upm; using namespace upm;
SmartDrive::SmartDrive(int i2c_bus, int address): m_smartdrive_control_address(address), m_i2c_smartdrive_control(i2c_bus) SmartDrive::SmartDrive(int i2c_bus, int address): m_smartdrive_control_address(address), m_i2c_smartdrive_control(i2c_bus)
@ -48,7 +51,7 @@ SmartDrive::SmartDrive(int i2c_bus, int address): m_smartdrive_control_address(a
void void
SmartDrive::writeByte(uint8_t addr, uint8_t value) { SmartDrive::writeByte(uint8_t addr, uint8_t value) {
try { try {
m_i2c_smartdrive_control.writeReg(addr, value); m_i2c_smartdrive_control.writeReg(addr, value);
} catch (int e) { } catch (int e) {
std::cout << "Failed to write " << value << " to address " << addr << " --> " << e << std::endl; std::cout << "Failed to write " << value << " to address " << addr << " --> " << e << std::endl;
} }
@ -57,7 +60,7 @@ SmartDrive::writeByte(uint8_t addr, uint8_t value) {
uint8_t uint8_t
SmartDrive::readByte(uint8_t addr) { SmartDrive::readByte(uint8_t addr) {
try { try {
return m_i2c_smartdrive_control.readReg(addr); return m_i2c_smartdrive_control.readReg(addr);
} catch (int e) { } catch (int e) {
std::cout << "Failed to read byte at address " << addr << " --> " << e << std::endl; std::cout << "Failed to read byte at address " << addr << " --> " << e << std::endl;
} }
@ -65,9 +68,9 @@ SmartDrive::readByte(uint8_t addr) {
} }
void void
SmartDrive::writeArray(uint8_t* array, uint8_t len) { SmartDrive::writeArray(uint8_t* array, int size) {
try { try {
m_i2c_smartdrive_control.write(array, len); m_i2c_smartdrive_control.write(array, size);
} catch (int e) { } catch (int e) {
std::cout << "Failed to write array values to address " << array[0] << " --> " << e << std::endl; std::cout << "Failed to write array values to address " << array[0] << " --> " << e << std::endl;
} }
@ -76,7 +79,7 @@ SmartDrive::writeArray(uint8_t* array, uint8_t len) {
uint16_t uint16_t
SmartDrive::readInteger(uint8_t addr) { SmartDrive::readInteger(uint8_t addr) {
try { try {
return m_i2c_smartdrive_control.readWordReg(addr); return m_i2c_smartdrive_control.readWordReg(addr);
} catch (int e) { } catch (int e) {
std::cout << "Failed to read value at address " << addr << " --> " << e << std::endl; std::cout << "Failed to read value at address " << addr << " --> " << e << std::endl;
} }
@ -88,7 +91,7 @@ SmartDrive::readLongSigned(uint8_t addr) {
uint8_t bytes[4]={0}; uint8_t bytes[4]={0};
try { try {
m_i2c_smartdrive_control.readBytesReg(addr, bytes, sizeof(bytes)/sizeof(uint8_t)); m_i2c_smartdrive_control.readBytesReg(addr, bytes, sizeof(bytes)/sizeof(uint8_t));
return (bytes[0]|(bytes[1]<<8)|(bytes[2]<<16)|(bytes[3]<<24)); return (bytes[0]|(bytes[1]<<8)|(bytes[2]<<16)|(bytes[3]<<24));
} catch (int e) { } catch (int e) {
std::cout << "Failed to read integer value at address " << addr << " --> " << e << std::endl; std::cout << "Failed to read integer value at address " << addr << " --> " << e << std::endl;
@ -146,7 +149,7 @@ SmartDrive::Run_Unlimited(int motor_id, int direction, uint8_t speed) {
uint8_t array [5] = {SmartDrive_SPEED_M1, speed, 0, 0, ctrl}; uint8_t array [5] = {SmartDrive_SPEED_M1, speed, 0, 0, ctrl};
writeArray(array, sizeof(array)); writeArray(array, sizeof(array));
} }
if ( motor_id != SmartDrive_Motor_ID_1) { if (motor_id != SmartDrive_Motor_ID_1) {
uint8_t array [5] = {SmartDrive_SPEED_M2, speed, 0, 0, ctrl}; uint8_t array [5] = {SmartDrive_SPEED_M2, speed, 0, 0, ctrl};
writeArray(array, sizeof(array)); writeArray(array, sizeof(array));
} }
@ -179,11 +182,11 @@ SmartDrive::Run_Seconds(int motor_id, int direction, uint8_t speed, uint8_t dura
ctrl |= SmartDrive_CONTROL_GO; ctrl |= SmartDrive_CONTROL_GO;
if ( direction != SmartDrive_Dir_Forward ) if ( direction != SmartDrive_Dir_Forward )
speed = speed * -1; speed = speed * -1;
if ( motor_id != SmartDrive_Motor_ID_2) { if ((motor_id & SmartDrive_Motor_ID_2)!= 0) {
uint8_t array[5] = {SmartDrive_SPEED_M1, speed, duration, 0, ctrl}; uint8_t array[5] = {SmartDrive_SPEED_M1, speed, duration, 0, ctrl};
writeArray(array, sizeof(array)); writeArray(array, sizeof(array));
} }
if ( motor_id != SmartDrive_Motor_ID_1) { if ((motor_id & SmartDrive_Motor_ID_1)!= 0) {
uint8_t array[5] = {SmartDrive_SPEED_M2, speed, duration, 0, ctrl}; uint8_t array[5] = {SmartDrive_SPEED_M2, speed, duration, 0, ctrl};
writeArray(array, sizeof(array)); writeArray(array, sizeof(array));
} }
@ -427,4 +430,4 @@ SmartDrive::PrintMotorStatus(int motor_id) {
} else { } else {
std::cout << "Please specify which motor's status you want to fetch !" << std::endl; std::cout << "Please specify which motor's status you want to fetch !" << std::endl;
} }
} }

View File

@ -1,7 +1,8 @@
/* /*
* The MIT License (MIT) * The MIT License (MIT)
* *
* Author: Oussema Harbi <oussema.elharbi@gmail.com> * Authors: Oussema Harbi <oussema.elharbi@gmail.com> and
* Neuber Jose de Sousa <neuberfran@gmail.com>
* Copyright (c) <2016> <Oussema Harbi> * Copyright (c) <2016> <Oussema Harbi>
* *
* Permission is hereby granted, free of charge, to any person obtaining a copy of * Permission is hereby granted, free of charge, to any person obtaining a copy of
@ -24,6 +25,8 @@
#pragma once #pragma once
#include <mraa/i2c.hpp> #include <mraa/i2c.hpp>
//We can use direct integer IDs, //We can use direct integer IDs,
@ -134,7 +137,7 @@ namespace upm {
/** /**
* @library smartdrive * @library smartdrive
* @sensor smartdrive * @sensor smartdrive
* @comname SmartDrive Advanced Motor Controller * @comname SmartDrive advanced motor controller
* @altname smartdrive * @altname smartdrive
* @type motor * @type motor
* @man openelectrons * @man openelectrons
@ -154,8 +157,7 @@ class SmartDrive {
public: public:
/** /**
* Initialize the class with the i2c address of your SmartDrive * Initialize the class with the i2c address of your SmartDrive
* @param i2c_bus I2C bus to use. * @param SmartDrive_address Address of your SmartDrive.
* @param address Address of your SmartDrive.
*/ */
SmartDrive(int i2c_bus, int address = SmartDrive_DefaultAddress); SmartDrive(int i2c_bus, int address = SmartDrive_DefaultAddress);
@ -239,6 +241,7 @@ public:
/** /**
* Turns the specified motor(s) for given absolute tacheometer count * Turns the specified motor(s) for given absolute tacheometer count
* @param motor_id Number of the motor(s) you wish to turn. * @param motor_id Number of the motor(s) you wish to turn.
* @param direction The direction you wish to turn the motor(s).
* @param speed The speed at which you wish to turn the motor(s). * @param speed The speed at which you wish to turn the motor(s).
* @param tacho_count The absolute tacheometer count you wish to turn the motor(s). * @param tacho_count The absolute tacheometer count you wish to turn the motor(s).
* @param wait_for_completion Tells the program when to handle the next line of code. * @param wait_for_completion Tells the program when to handle the next line of code.
@ -288,7 +291,7 @@ public:
private: private:
void writeByte(uint8_t addr, uint8_t value); void writeByte(uint8_t addr, uint8_t value);
void writeArray(uint8_t* array, uint8_t len); void writeArray(uint8_t* array, int size);
uint8_t readByte(uint8_t addr); uint8_t readByte(uint8_t addr);
uint16_t readInteger(uint8_t addr); uint16_t readInteger(uint8_t addr);
uint32_t readLongSigned(uint8_t addr); uint32_t readLongSigned(uint8_t addr);
@ -299,4 +302,4 @@ private:
}; };
} }