8000 QEI Module by celetts · Pull Request #7 · elua/elua · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content

QEI Module #7

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions SConstruct
Original file line number Diff line number Diff line change
Expand Up @@ -352,7 +352,7 @@ if not GetOption( 'help' ):

# Application files
app_files = """ src/main.c src/romfs.c src/semifs.c src/xmodem.c src/shell.c src/term.c src/common.c src/common_tmr.c src/buf.c src/elua_adc.c src/dlmalloc.c
src/salloc.c src/luarpc_elua_uart.c src/elua_int.c src/linenoise.c src/common_uart.c src/eluarpc.c """
src/salloc.c src/luarpc_elua_uart.c src/elua_int.c src/linenoise.c src/common_uart.c src/eluarpc.c src/elua_qei.c """

# Newlib related files
newlib_files = " src/newlib/devman.c src/newlib/stubs.c src/newlib/genstd.c src/newlib/stdtcp.c"
Expand All @@ -367,7 +367,7 @@ if not GetOption( 'help' ):
comp.Append(CPPPATH = ['src/fatfs'])

# Lua module files
module_names = "pio.c spi.c tmr.c pd.c uart.c term.c pwm.c lpack.c bit.c net.c cpu.c adc.c can.c luarpc.c bitarray.c elua.c i2c.c"
module_names = "pio.c spi.c tmr.c pd.c uart.c term.c pwm.c lpack.c bit.c net.c cpu.c adc.c can.c luarpc.c bitarray.c elua.c i2c.c qei.c"
module_files = " " + " ".join( [ "src/modules/%s" % name for name in module_names.split() ] )

# Remote file system files
Expand Down
114 changes: 114 additions & 0 deletions doc/eluadoc/refman_gen_qei.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,114 @@
-- eLua reference manual - QEI

data_en =
{

-- Title
title = "eLua reference manual - QEI",

-- Menu name
menu_name = "qei",

-- Overview
overview = [[This module contains functions that access the Quadrature Encoder Interface (QEI).<\p>
<p>Using the QEI is a three or four step process: First the peripheral must be initiailised (Using $qei.init$). The peripheral can be optionally initialised to measure velocity in a second step (Using $qei.velInit$). The peripheral must then be enabled ($qei.enable$) before lastly, results can be read (Using $qei.getPosition$, and if initialised, $qei.getVelPulses$, $qei.getRPM$). The QEI can also be disabled via $qei.disabled$<\p>
<p>Encoders can be set alone or in combination in the initialisation functions, however must have data read in separate calls for each encoder.<\p>
<p>This module can be utilized if the device in use has a supported QEI peripheral (see @status.html@status@ for details) and if QEI functionality is enabled at build time (see @building.html@building@).</p>
<p><span class="warning">IMPORTANT</span>: Platform support varies for this module (see @status.html#plat_notes@status notes@ for details) .
]],

-- Structures
structures =
{
{
text = [[// eLua qei error codes
enum
{
ELUA_QEI_ERR_OK = 0, // exported as $qei.ERR_OK$
ELUA_QEI_ERR_VEL_NOT_ENABLED, // exported as $qei.ERR_VELOCITY_NOT_ENABLED$
ELUA_QEI_ERR_ENC_NOT_ENABLED, // exported as $qei.ERR_ENCODER_NOT_ENABLED$
};]],
name = "Error codes",
desc = "These are the error codes defined by the eLua_qei layer and are returned by a number of functions in this module.",
}
},

-- Functions
funcs =
{
{ sig = "#qei.init#( encoder_id, phase, swap, index, max_count )",
desc = "Initiates the encoder channels for use.",
args =
{
"$encoder_id$ - QEI channel ID. Specified as qei.CH0, qei.CH1, qei.CHO1. Channels 0 or 1 can be initialised together or separately by calling this function once on each encoder.",
"$phase$ - Each encoder has two phases: A and B. Either qei.PHA or qei.PHAB can be specified, effecting resolution. NOTE: phase B cannot be set on its own.",
"$swap$ - Specified as either qei.SWAP or qei.NO_SWAP, this defines whether the phases are switched before processing. This changes the direction of the encoders via software.",
"$index$ - Specified as either qei.INDEX or qei.NO_INDEX, this defines whether an index pulse is used or not.",
"$max_count$ - Sets the maximum count of the encoder before it resets to zero.",
}
},
{ sig = "#qei.velInit#( encoder_id, vel_period, ppr, edges )",
desc = "Optionally initialises the encoder channels for use in velocity measurement.",
args =
{
"$encoder_id$ - QEI channel ID. Specified as qei.CH0, qei.CH1, qei.CHO1. Channels 0 or 1 can be initialised together or separately by calling this function once on each encoder.",
"$vel_period$ - Sets the period in micro seconds over which time the velocity is calculated.",
"$ppr$ - Pulses per revolution of the encoder.",
"$edges$ - The number of edges the encoder is triggering on.",
}
},
{ sig = "#qei.enable#( encoder_id )",
desc = "Enables the encoder, post initialisation.",
args =
{
"$encoder_id$ - QEI channel ID. Specified as qei.CH0, qei.CH1, qei.CHO1. Channels 0 or 1 can be enabled separately by calling this function once on each encoder.",
}
},
{ sig = "#qei.disable#( encoder_id )",
desc = "Disables the encoder.",
args =
{
"$encoder_id$ - QEI channel ID. Specified as qei.CH0, qei.CH1, qei.CHO1. Channels 0 or 1 can be disabled separately by calling this function once on each encoder.",
}
},
{ sig = "#qei.getPosition#( encoder_id )",
desc = "Returns the current count of the encoder, ranging between 0 and maximum value set during initialisation.",
args =
{
"$encoder_id$ - QEI channel ID. Ethier qei.CH0 or qei.CH1 can be called. NOTE: qei.CH01 cannot be called. Must call each encoder separately.",
}
ret =
{
"$count$ - the current encoder count between 0 and max_count set during initialisation.",
"$err$ - Error code as defined above."
}
},
{ sig = "#qei.getVelPulses#( encoder_id )",
desc = "Returns the number of pulses counted during the vel_period set during initialisation of velocity measurement. This value can be used raw, but is also used to calculate the RPM. ",
args =
{
"$encoder_id$ - QEI channel ID. Ethier qei.CH0 or qei.CH1 can be called. NOTE: qei.CH01 cannot be called. Must call each encoder separately.",
}
ret =
{
"$pulses$ - The number of pulses recorded during the period defined during initialisation.",
"$err$ - Error code as defined above."
}
},
{ sig = "#qei.getRPM#( encoder_id )",
desc = "Returns the current number of revolutions per minute (RPM) of the selected encoder.",
args =
{
"$encoder_id$ - QEI channel ID. Ethier qei.CH0 or qei.CH1 can be called. NOTE: qei.CH01 cannot be called. Must call each encoder separately.",
}
ret =
{
"$rpm$ - The current rpm.",
"$err$ - Error code as defined above."
}
}
}
}

data_pt = data_en

41 changes: 41 additions & 0 deletions inc/elua_qei.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
// Quadrature Encoding Interface (QEI)

#ifndef ELUA_QEI_H
#define ELUA_QEI_H

#include "platform_conf.h"

/* Encoder 0 and 1 each with phase A and B */
enum valid_encoder_ids { ELUA_QEI_CH0 = 1, ELUA_QEI_CH1, ELUA_QEI_CH01 };
enum valid_encoder_phases { ELUA_QEI_PHA = 0, ELUA_QEI_PHAB };
/* Defines whether phases are switched before processing */
enum qei_swap_codes { ELUA_QEI_NO_SWAP = 0, ELUA_QEI_SWAP };
/* Defines whether an index pulse is used */
enum qei_index_codes { ELUA_QEI_NO_INDEX = 0, ELUA_QEI_INDEX };
/* Error Codes */
enum qei_error_codes { ELUA_QEI_ERR_OK = 0, ELUA_QEI_ERR_VEL_NOT_ENABLED, ELUA_QEI_ERR_ENC_NOT_ENABLED };

/* qei_flag keeps track of encoders/channels that are enabled, as well
* as whether they are enabled for velocity measurement.
* BIT0 Channel0 Enabled
* BIT1 Channel1 Enabled
* BIT2 Channel0 Velocity Enabled
* BIT3 Channel1 Velocity Enabled
* BIT4-7 Unused. */
u8 qei_flag;
#define VEL_FLAG_OFFSET 2

/* vel_period is time (us) over which to measure velocity. vel_ticks is
* this period converted into counts on the system clock. */
u32 vel_ticks;

/* Function Prototypes */
void elua_qei_init( u8 enc_id, u8 phase, u8 swap, u8 index, u32 max_count );
void elua_qei_vel_init( u8 enc_id, u32 vel_period, int ppr, int edges );
void elua_qei_enable( u8 enc_id );
void elua_qei_disable( u8 enc_id );
u32 elua_qei_getPulses( u8 enc_id );
s32 elua_qei_getRPM( u8 enc_id );
u32 elua_qei_getPosition( u8 enc_id );

#endif
12 changes: 11 additions & 1 deletion inc/platform.h
Original file line number Diff line number Diff line change
Expand Up @@ -315,7 +315,17 @@ u32 platform_eth_get_elapsed_time();
void platform_usb_cdc_send( u8 data );
int platform_usb_cdc_recv( s32 timeout );


//******************************************************************************
// Quadrature encoder platform interface

6D4E void platform_qei_init( u8 enc_id, u8 phase, u8 swap, u8 index, u32 max_count );
void platform_qei_vel_init( u8 enc_id, u32 vel_period );
void platform_qei_enable( u8 enc_id );
void platform_qei_disable( u8 enc_id );
u32 platform_qei_get_sys_clk();
u32 platform_qei_getPulses( u8 enc_id );
u32 platform_qei_getPosition( u8 enc_id );
long platform_qei_getDirection( u8 enc_id );

// *****************************************************************************
// Allocator support
Expand Down
51 changes: 51 additions & 0 deletions src/elua_qei.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
// Quadrature Encoding Interface (QEI)

#include "elua_qei.h"
#include "platform.h"

#ifdef BUILD_QEI
static u32 vel_modifier = 0;

void elua_qei_init( u8 enc_id, u8 phase, u8 swap, u8 index, u32 max_count )
{
platform_qei_init( enc_id, phase, swap, index, max_count );
}

void elua_qei_vel_init( u8 enc_id, u32 vel_period, int ppr, int edges )
{
platform_qei_vel_init( enc_id, vel_period );
qei_flag |= ( enc_id << VEL_FLAG_OFFSET ); //Sets encoder velocity flag
u32 clk_freq = platform_qei_get_sys_clk();
vel_modifier = (clk_freq * 60) / (vel_ticks * ppr * edges);
}
void elua_qei_enable( u8 enc_id )
{
platform_qei_enable( enc_id );
qei_flag |= enc_id;
}

void elua_qei_disable( u8 enc_id )
{
platform_qei_disable( enc_id );
qei_flag &= ~enc_id;
}

u32 elua_qei_getPulses( u8 enc_id )
{
u32 pulses = platform_qei_getPulses( enc_id );
return pulses;
}

s32 elua_qei_getRPM( u8 enc_id )
{
u32 pulses = elua_qei_getPulses( enc_id );
s32 rpm = pulses * vel_modifier * platform_qei_getDirection( enc_id );
return rpm;
}

u32 elua_qei_getPosition( u8 enc_id )
{
return platform_qei_getPosition( enc_id );
}

#endif
3 changes: 3 additions & 0 deletions src/modules/auxmods.h
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,9 @@ LUALIB_API int ( luaopen_elua )( lua_State *L );
#define AUXLIB_I2C "i2c"
LUALIB_API int ( luaopen_i2c )( lua_State *L );

#define AUXLIB_QEI "qei"
LUALIB_API int ( luaopen_qei )( lua_State *L );

// Helper macros
#define MOD_CHECK_ID( mod, id )\
if( !platform_ ## mod ## _exists( id ) )\
Expand Down
Loading
0