Skip to content

Commit

Permalink
devel: added code to find Amp1394 revision using CMake and c-preproce…
Browse files Browse the repository at this point in the history
…ssor, change encoder position API to return a signed int related to encoder mid-range
  • Loading branch information
adeguet1 committed Apr 10, 2015
1 parent a6d5519 commit a866550
Show file tree
Hide file tree
Showing 5 changed files with 116 additions and 31 deletions.
12 changes: 9 additions & 3 deletions Amp1394Config.cmake.in
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,16 @@
# Amp1394_INCLUDE_DIR - include directories for Amp1394
# Amp1394_LIBRARY_DIR - link library directories for Amp1394
# Amp1394_LIBRARIES - libraries to link against

# Version
set (Amp1394_VERSION_MAJOR "@Amp1394_VERSION_MAJOR@")
set (Amp1394_VERSION_MINOR "@Amp1394_VERSION_MINOR@")
set (Amp1394_VERSION_PATCH "@Amp1394_VERSION_PATCH@")
set (Amp1394_VERSION "@Amp1394_VERSION")

# Compute paths
set(Amp1394_INCLUDE_DIR "@CONF_INCLUDE_DIR@")
set(Amp1394_LIBRARY_DIR "@CONF_LIBRARY_DIR@")
set (Amp1394_INCLUDE_DIR "@CONF_INCLUDE_DIR@")
set (Amp1394_LIBRARY_DIR "@CONF_LIBRARY_DIR@")

# These are IMPORTED targets created by Amp1394Targets.cmake
set(Amp1394_LIBRARIES Amp1394)
set (Amp1394_LIBRARIES Amp1394)
37 changes: 23 additions & 14 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
#
# $Id$
#
# (C) Copyright 2011-2012 Johns Hopkins University (JHU), All Rights Reserved.
# (C) Copyright 2011-2015 Johns Hopkins University (JHU), All Rights Reserved.
#
# --- begin cisst license - do not edit ---
#
Expand All @@ -11,9 +9,15 @@
#
# --- end cisst license ---

project(Mechatronics)
project (Mechatronics)

cmake_minimum_required (VERSION 2.6)

cmake_minimum_required(VERSION 2.6)
# Set the version number
set (Amp1394_VERSION_MAJOR "1")
set (Amp1394_VERSION_MINOR "1")
set (Amp1394_VERSION_PATCH "0")
set (Amp1394_VERSION "${Amp1394_VERSION_MAJOR}.${Amp1394_VERSION_MINOR}.${Amp1394_VERSION_PATCH}")

# Add some warnings
include (CheckCXXCompilerFlag)
Expand All @@ -28,22 +32,27 @@ if (CXX_SUPPORTS_WALL)
endif (CXX_SUPPORTS_WALL)

# ... for the build tree
set(CONF_INCLUDE_DIR "${PROJECT_SOURCE_DIR}/lib")
set(CONF_LIBRARY_DIR "${PROJECT_BINARY_DIR}/lib")
configure_file(Amp1394Config.cmake.in
"${PROJECT_BINARY_DIR}/Amp1394Config.cmake" @ONLY)
set (CONF_INCLUDE_DIR "${PROJECT_SOURCE_DIR}/lib" "${CMAKE_CURRENT_BINARY_DIR}")
set (CONF_LIBRARY_DIR "${PROJECT_BINARY_DIR}/lib")
configure_file (Amp1394Config.cmake.in
"${PROJECT_BINARY_DIR}/Amp1394Config.cmake" @ONLY)

# revision file
configure_file ("${CMAKE_CURRENT_SOURCE_DIR}/lib/AmpIORevision.h.in"
"${CMAKE_CURRENT_BINARY_DIR}/Amp1394/AmpIORevision.h")

# To find Amp1394 library headers
include_directories(${CMAKE_CURRENT_SOURCE_DIR}/lib)
include_directories (${CMAKE_CURRENT_SOURCE_DIR}/lib
${CMAKE_CURRENT_BINARY_DIR})

# To link with build libraries
link_directories(${CMAKE_CURRENT_BINARY_DIR})
link_directories (${CMAKE_CURRENT_BINARY_DIR})

# Utilities to test libraw1394
add_subdirectory(util)
add_subdirectory (util)

# Code for library Amp1394
add_subdirectory(lib)
add_subdirectory (lib)

# Test program using curse for text based display
find_package (Curses)
Expand All @@ -54,4 +63,4 @@ else (CURSES_FOUND)
endif (CURSES_FOUND)

# Utility to flash/test board
add_subdirectory(programmer)
add_subdirectory (programmer)
17 changes: 14 additions & 3 deletions lib/AmpIO.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
/*
Author(s): Zihan Chen
(C) Copyright 2011-2014 Johns Hopkins University (JHU), All Rights Reserved.
(C) Copyright 2011-2015 Johns Hopkins University (JHU), All Rights Reserved.
--- begin cisst license - do not edit ---
Expand All @@ -19,12 +19,16 @@ no warranty. The complete license can be found in license.txt and
#ifndef __AMPIO_H__
#define __AMPIO_H__

#include <Amp1394/AmpIORevision.h>

#include "BoardIO.h"
#include <stdint.h> // for uint32_t
#include <iostream>

class ostream;


typedef int32_t AmpIO_Int32;
typedef uint32_t AmpIO_UInt32;
typedef uint16_t AmpIO_UInt16;
typedef uint8_t AmpIO_UInt8;
Expand Down Expand Up @@ -67,7 +71,12 @@ class AmpIO : public BoardIO
//@{
AmpIO_UInt8 GetEncoderChannelA(void) const;
bool GetEncoderChannelA(unsigned int index) const;

AmpIO_UInt8 GetEncoderChannelB(void) const;
bool GetEncoderChannelB(unsigned int index) const;

bool GetEncoderOverflow(unsigned int index) const;

AmpIO_UInt8 GetEncoderIndex(void) const;
//@}

Expand All @@ -79,10 +88,12 @@ class AmpIO : public BoardIO

AmpIO_UInt32 GetAnalogInput(unsigned int index) const;

AmpIO_UInt32 GetEncoderPosition(unsigned int index) const;
AmpIO_Int32 GetEncoderPosition(unsigned int index) const;

AmpIO_UInt32 GetEncoderVelocity(unsigned int index, const bool islatch = true) const;

AmpIO_Int32 GetEncoderMidRange(void) const;

// GetPowerStatus: returns true if motor power supply voltage
// is present on the QLA. If not present, it could be because
// power is disabled or the power supply is off.
Expand Down Expand Up @@ -138,7 +149,7 @@ class AmpIO : public BoardIO

bool WriteSafetyRelay(bool state);

bool WriteEncoderPreload(unsigned int index, AmpIO_UInt32 enc);
bool WriteEncoderPreload(unsigned int index, AmpIO_Int32 enc);

bool WriteDigitalOutput(AmpIO_UInt8 mask, AmpIO_UInt8 bits);

Expand Down
30 changes: 30 additions & 0 deletions lib/AmpIORevision.h.in
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
/* ex: set filetype=cpp softtabstop=4 shiftwidth=4 tabstop=4 cindent expandtab: */

/*
Author(s): Anton Deguet
Created on: 2014-04-10
(C) Copyright 2015 Johns Hopkins University (JHU), All Rights Reserved.
--- begin cisst license - do not edit ---
This software is provided "as is" under an open source license, with
no warranty. The complete license can be found in license.txt and
http://www.cisst.org/cisst/license.txt.
--- end cisst license ---
*/

#pragma once

#ifndef _AmpIORevision_h
#define _AmpIORevision_h

#define Amp1394_VERSION_MAJOR ${Amp1394_VERSION_MAJOR}
#define Amp1394_VERSION_MINOR ${Amp1394_VERSION_MINOR}
#define Amp1394_VERSION_PATCH ${Amp1394_VERSION_PATCH}
#define Amp1394_VERSION ${Amp1394_VERSION}

#endif // _AmpIORevision_h
51 changes: 40 additions & 11 deletions lib/code/AmpIO.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
/*
Author(s): Zihan Chen, Peter Kazanzides
(C) Copyright 2011-2014 Johns Hopkins University (JHU), All Rights Reserved.
(C) Copyright 2011-2015 Johns Hopkins University (JHU), All Rights Reserved.
--- begin cisst license - do not edit ---
Expand All @@ -29,6 +29,7 @@ const AmpIO_UInt32 MIDRANGE_VEL = 0x00008000; /*!< Midrange value of encode
const AmpIO_UInt32 MIDRANGE_FRQ = 0x00008000; /*!< Midrange value of encoder frequency */
const AmpIO_UInt32 MIDRANGE_ACC = 0x00008000; /*!< Midrange value of encoder acc */
const AmpIO_UInt32 ENC_PRELOAD = 0x007fffff; /*!< Encoder preload value */
const AmpIO_Int32 ENC_MIDRANGE = 0x00800000;

const AmpIO_UInt32 PWR_ENABLE = 0x000c0000; /*!< Turn pwr_en on */
const AmpIO_UInt32 PWR_DISABLE = 0x00080000; /*!< Turn pwr_en off */
Expand All @@ -39,7 +40,8 @@ const AmpIO_UInt32 MOTOR_CURR_MASK = 0x0000ffff; /*!< Mask for motor current a
const AmpIO_UInt32 ANALOG_POS_MASK = 0xffff0000; /*!< Mask for analog pot ADC bits */
const AmpIO_UInt32 ADC_MASK = 0x0000ffff; /*!< Mask for right aligned ADC bits */
const AmpIO_UInt32 DAC_MASK = 0x0000ffff; /*!< Mask for 16-bit DAC values */
const AmpIO_UInt32 ENC_POS_MASK = 0x01ffffff; /*!< Mask for quad encoder bits */
const AmpIO_UInt32 ENC_POS_MASK = 0x00ffffff; /*!< Encoder position mask */
const AmpIO_UInt32 ENC_OVER_MASK = 0x01000000; /*!< Encoder bit overflow mask */
const AmpIO_UInt32 ENC_VEL_MASK = 0x0000ffff; /*!< Mask for encoder velocity bits */
const AmpIO_UInt32 ENC_FRQ_MASK = 0x0000ffff; /*!< Mask for encoder frequency bits */

Expand Down Expand Up @@ -158,11 +160,28 @@ AmpIO_UInt8 AmpIO::GetEncoderChannelB(void) const
return (this->GetDigitalInput()&0x00f00000)>>20;
}

bool AmpIO::GetEncoderChannelB(unsigned int index) const
{
const AmpIO_UInt8 mask = (0x0001 << index);
return GetEncoderChannelB()&mask;
}

AmpIO_UInt8 AmpIO::GetEncoderIndex(void) const
{
return (this->GetDigitalInput()&0x000f0000)>>16;
}

bool AmpIO::GetEncoderOverflow(unsigned int index) const
{
if (index < NUM_CHANNELS) {
return bswap_32(read_buffer[index+ENC_POS_OFFSET]) & ENC_OVER_MASK;
}
else {
std::cerr << "Warning: GetEncoderOverflow, index out of range " << index << std::endl;
}
return true; // send error "code"
}

AmpIO_UInt8 AmpIO::GetAmpTemperature(unsigned int index) const
{
AmpIO_UInt8 temp = 0;
Expand Down Expand Up @@ -198,12 +217,12 @@ AmpIO_UInt32 AmpIO::GetAnalogInput(unsigned int index) const
return static_cast<AmpIO_UInt32>(buff) & ADC_MASK;
}

AmpIO_UInt32 AmpIO::GetEncoderPosition(unsigned int index) const
AmpIO_Int32 AmpIO::GetEncoderPosition(unsigned int index) const
{
if (index < NUM_CHANNELS)
return bswap_32(read_buffer[index+ENC_POS_OFFSET]);
else
return 0;
if (index < NUM_CHANNELS) {
return static_cast<AmpIO_Int32>(bswap_32(read_buffer[index + ENC_POS_OFFSET]) & ENC_POS_MASK) - ENC_MIDRANGE;
}
return 0;
}

// temp current the enc period velocity is unsigned 16 bits
Expand Down Expand Up @@ -231,6 +250,10 @@ AmpIO_UInt32 AmpIO::GetEncoderVelocity(unsigned int index, const bool islatch) c
else return cnter;
}

AmpIO_Int32 AmpIO::GetEncoderMidRange(void) const
{
return ENC_MIDRANGE;
}

bool AmpIO::GetPowerStatus(void) const
{
Expand Down Expand Up @@ -379,14 +402,20 @@ bool AmpIO::WriteSafetyRelay(bool state)
return (port ? port->WriteQuadlet(BoardId, 0, bswap_32(write_data)) : false);
}

bool AmpIO::WriteEncoderPreload(unsigned int index, AmpIO_UInt32 sdata)
bool AmpIO::WriteEncoderPreload(unsigned int index, AmpIO_Int32 sdata)
{
unsigned int channel = (index+1) << 4;

if (port && (index < NUM_CHANNELS))
return port->WriteQuadlet(BoardId, channel | ENC_LOAD_OFFSET, bswap_32(sdata));
else
if ((sdata >= ENC_MIDRANGE)
|| (sdata < -ENC_MIDRANGE)) {
std::cerr << "Error: WriteEncoderPreload, preload out of range" << std::endl;
return false;
}
if (port && (index < NUM_CHANNELS)) {
return port->WriteQuadlet(BoardId, channel | ENC_LOAD_OFFSET, bswap_32(static_cast<AmpIO_UInt32>(sdata + ENC_MIDRANGE)));
} else {
return false;
}
}

bool AmpIO::WriteDigitalOutput(AmpIO_UInt8 mask, AmpIO_UInt8 bits)
Expand Down

0 comments on commit a866550

Please sign in to comment.