Initial commit
This commit is contained in:
93
AS5600L/AS5600L.c
Normal file
93
AS5600L/AS5600L.c
Normal file
@@ -0,0 +1,93 @@
|
||||
#include "AS5600L.h"
|
||||
|
||||
void as560x_init(AS5600L* as5600l, i2c_inst_t *I2C_ID, int frq , int sda, int scl ) {
|
||||
i2c_init(I2C_ID, frq);
|
||||
gpio_set_function(sda, GPIO_FUNC_I2C);
|
||||
gpio_set_function(scl, GPIO_FUNC_I2C);
|
||||
gpio_pull_up(sda);
|
||||
gpio_pull_up(scl);
|
||||
|
||||
AS5600L_I2C instance_i2c;
|
||||
instance_i2c.I2C_PORT = I2C_ID;
|
||||
instance_i2c.FREQUENCE = frq;
|
||||
instance_i2c.I2C_SDA_PIN = sda;
|
||||
instance_i2c.I2C_SCL_PIN = scl;
|
||||
|
||||
as5600l->serial_instance = instance_i2c;
|
||||
as5600l->abs_pos = 0;
|
||||
as5600l->curr_angle = 0;
|
||||
as5600l->last_angle = 0;
|
||||
as5600l->start_pos = 0;
|
||||
|
||||
|
||||
as5600l->start_pos = as560xReadAngle(as5600l);
|
||||
as5600l->abs_pos = 0 - as5600l->start_pos;
|
||||
}
|
||||
|
||||
int update_pos(AS5600L* as5600l){
|
||||
// sensorData();
|
||||
as5600l->curr_angle = as560xReadAngle(as5600l);
|
||||
|
||||
if(as5600l->last_angle > ENCODER_RESOLUTION/2 & as5600l->curr_angle < (as5600l->last_angle - ENCODER_RESOLUTION/2)){
|
||||
as5600l->abs_pos = as5600l->abs_pos + ENCODER_RESOLUTION - as5600l->last_angle + as5600l->curr_angle;
|
||||
}else if(as5600l->curr_angle > ENCODER_RESOLUTION/2 & as5600l->last_angle < (as5600l->curr_angle - ENCODER_RESOLUTION/2) ){
|
||||
as5600l->abs_pos = as5600l->abs_pos -ENCODER_RESOLUTION - as5600l->last_angle + as5600l->curr_angle;
|
||||
}else{
|
||||
as5600l->abs_pos = as5600l->abs_pos - as5600l->last_angle + as5600l->curr_angle;
|
||||
}
|
||||
|
||||
as5600l->last_angle = as5600l->curr_angle;
|
||||
return as5600l->abs_pos;
|
||||
}
|
||||
|
||||
int __bswap16(int value) {
|
||||
return (value >> 8) | (value << 8);
|
||||
}
|
||||
|
||||
void i2cError() {
|
||||
printf("I2C Error\n");
|
||||
}
|
||||
|
||||
int as560xReadReg(AS5600L* as5600l,int addr, bool wide, int mask) {
|
||||
int buf;
|
||||
int result = i2c_write_timeout_us(as5600l->serial_instance.I2C_PORT, AS5600L_ADDRESS, (uint8_t *)&addr, 1, true, I2C_TIMEOUT_US);
|
||||
if (result <= 0) {
|
||||
i2cError();
|
||||
}
|
||||
result = i2c_read_timeout_us(as5600l->serial_instance.I2C_PORT, AS5600L_ADDRESS, (uint8_t *)&buf, (wide ? 2 : 1), false, I2C_TIMEOUT_US);
|
||||
if (result <= 0) {
|
||||
i2cError();
|
||||
}
|
||||
if (wide) {
|
||||
return __bswap16(buf) & mask;
|
||||
} else {
|
||||
return buf & mask;
|
||||
}
|
||||
}
|
||||
|
||||
void AS560x_print_reg16(AS5600L* as5600l,const char *formatStr, int addr, int mask) {
|
||||
int result = as560xReadReg(as5600l,addr, true, mask);
|
||||
printf(formatStr, result & mask);
|
||||
}
|
||||
|
||||
void AS560x_print_reg8(AS5600L* as5600l,const char *formatStr, int addr, uint8_t mask) {
|
||||
uint8_t result = (uint8_t)as560xReadReg(as5600l,addr, false, mask);
|
||||
printf(formatStr, result & mask);
|
||||
}
|
||||
|
||||
|
||||
int as560xReadAngle(AS5600L* as5600l) {
|
||||
return as560xReadReg(as5600l,AS560x_RAW_ANGLE_REG, true, 0xFFF) ;
|
||||
}
|
||||
|
||||
uint8_t as560xGetStatus(AS5600L* as5600l) {
|
||||
return (uint8_t)as560xReadReg(as5600l,AS560x_STATUS_REG, false, 0x38);
|
||||
}
|
||||
|
||||
void sensorData(AS5600L* as5600l) {
|
||||
AS560x_print_reg8(as5600l,"Status: %02x; ", AS560x_STATUS_REG, 0x38);
|
||||
AS560x_print_reg8(as5600l,"AGC: %3x; ", 0x1a, 0xff);
|
||||
AS560x_print_reg16(as5600l,"Angle: %d\n\r", AS560x_RAW_ANGLE_REG, 0xFFF);
|
||||
}
|
||||
|
||||
|
||||
47
AS5600L/AS5600L.h
Normal file
47
AS5600L/AS5600L.h
Normal file
@@ -0,0 +1,47 @@
|
||||
#ifndef AS5600L_H
|
||||
#define AS5600L_H
|
||||
|
||||
#include "hardware/i2c.h"
|
||||
#include "pico/stdlib.h"
|
||||
#include "pico/time.h"
|
||||
#include <stdio.h>
|
||||
#include <stdint.h>
|
||||
#include <stdbool.h>
|
||||
|
||||
#define I2C_TIMEOUT_US (100000)
|
||||
|
||||
#define AS560x_STATUS_REG (0x0B)
|
||||
#define AS560x_RAW_ANGLE_REG (0x0C)
|
||||
#define AS5600L_ADDRESS 0x40
|
||||
#define ENCODER_RESOLUTION 4096
|
||||
|
||||
|
||||
|
||||
typedef struct {
|
||||
i2c_inst_t *I2C_PORT;
|
||||
int FREQUENCE;
|
||||
int I2C_SDA_PIN;
|
||||
int I2C_SCL_PIN;
|
||||
} AS5600L_I2C;
|
||||
|
||||
typedef struct {
|
||||
AS5600L_I2C serial_instance;
|
||||
int last_angle;
|
||||
int curr_angle ;
|
||||
int abs_pos ;
|
||||
int start_pos;
|
||||
} AS5600L;
|
||||
|
||||
|
||||
void as560x_init(AS5600L* as5600l, i2c_inst_t *I2C_ID, int frq , int sda, int scl);
|
||||
int update_pos(AS5600L* as5600l);
|
||||
int __bswap16(int value);
|
||||
void i2cError();
|
||||
int as560xReadReg(AS5600L* as5600l,int addr, bool wide, int mask);
|
||||
void AS560x_print_reg16(AS5600L* as5600l,const char *formatStr, int addr, int mask);
|
||||
void AS560x_print_reg8(AS5600L* as5600l,const char *formatStr, int addr, uint8_t mask);
|
||||
int as560xReadAngle(AS5600L* as5600l);
|
||||
uint8_t as560xGetStatus(AS5600L* as5600l);
|
||||
void sensorData(AS5600L* as5600l);
|
||||
|
||||
#endif /* AS5600L_H */
|
||||
32
AS5600L/CMakeLists.txt
Normal file
32
AS5600L/CMakeLists.txt
Normal file
@@ -0,0 +1,32 @@
|
||||
cmake_minimum_required(VERSION 3.12)
|
||||
|
||||
#cmake -G "MinGW Makefiles" ..
|
||||
|
||||
set(PROJECT_NAME "AS5600L_test")
|
||||
set(PICO_SDK_PATH "E:/Beyon_Motion/pico-sdk")
|
||||
set(PICO_TOOLCHAIN_PATH "C:/Users/spide/scoop/apps/gcc-arm-none-eabi/current/bin")
|
||||
set(CMAKE_C_STANDARD 11)
|
||||
set(CMAKE_CXX_STANDARD 17)
|
||||
|
||||
include(${PICO_SDK_PATH}/external/pico_sdk_import.cmake)
|
||||
|
||||
project(${PROJECT_NAME} C CXX ASM)
|
||||
|
||||
pico_sdk_init()
|
||||
|
||||
add_executable(${PROJECT_NAME}
|
||||
main.c
|
||||
AS5600L.c
|
||||
AS5600L.h
|
||||
)
|
||||
|
||||
target_link_libraries(${PROJECT_NAME}
|
||||
pico_stdlib
|
||||
hardware_i2c
|
||||
hardware_dma
|
||||
)
|
||||
|
||||
pico_enable_stdio_usb(${PROJECT_NAME} 1)
|
||||
pico_enable_stdio_uart(${PROJECT_NAME} 0)
|
||||
|
||||
pico_add_extra_outputs(${PROJECT_NAME})
|
||||
28
AS5600L/main.c
Normal file
28
AS5600L/main.c
Normal file
@@ -0,0 +1,28 @@
|
||||
#include "pico/stdlib.h"
|
||||
#include "pico/time.h"
|
||||
#include "hardware/i2c.h"
|
||||
#include <stdio.h>
|
||||
#include "AS5600L.h"
|
||||
|
||||
#define I2C_PORT i2c0
|
||||
#define I2C_SDA_PIN 8
|
||||
#define I2C_SCL_PIN 9
|
||||
|
||||
int main() {
|
||||
stdio_init_all();
|
||||
sleep_ms(1000);
|
||||
|
||||
AS5600L as5600l;
|
||||
|
||||
as560x_init(&as5600l,I2C_PORT,100000,I2C_SDA_PIN,I2C_SCL_PIN);
|
||||
|
||||
int abs = 0;
|
||||
abs = update_pos;
|
||||
while (true) {
|
||||
// abs = update_pos(&as5600l);
|
||||
// printf("Position absolue : %d\n",abs);
|
||||
sensorData(&as5600l);
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
Reference in New Issue
Block a user