-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
7 changed files
with
129 additions
and
81 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
#include "hardware/i2c.h" | ||
#include <machine/endian.h> | ||
|
||
#include "servo.h" | ||
#include "as560x.h" | ||
|
||
#define AS560x_ADDR 0x36 | ||
#define AS560x_STATUS_REG (0x0B) | ||
#define AS560x_RAW_ANGLE_REG (0x0C) | ||
|
||
uint16_t as560xReadReg(int addr, bool wide, uint16_t mask) { | ||
uint16_t buf; | ||
int result = i2c_write_timeout_us(I2C, AS560x_ADDR, (uint8_t *) &addr, 1, true, I2C_TIMEOUT_US); | ||
if (result <= 0) { | ||
i2cError(); | ||
} | ||
result = i2c_read_timeout_us(I2C, AS560x_ADDR, (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(const char *formatStr, int addr, uint16_t mask) { | ||
uint16_t result = as560xReadReg(addr, true, mask); | ||
printf(formatStr, result & mask); | ||
} | ||
|
||
void AS560x_print_reg8(const char *formatStr, int addr, uint8_t mask) { | ||
uint8_t result = (uint8_t) as560xReadReg(addr, false, mask); | ||
printf(formatStr, result & mask); | ||
} | ||
|
||
void as560x_init() { | ||
i2c_init(i2c1, 100 * 1000); | ||
} | ||
|
||
int as560xReadAngle() { | ||
return as560xReadReg(AS560x_RAW_ANGLE_REG, true, 0xFFF); | ||
} | ||
|
||
uint8_t as560xGetStatus() { | ||
return (uint8_t) as560xReadReg(AS560x_STATUS_REG, false, 0x38); | ||
} | ||
|
||
__unused void sensorData() { | ||
AS560x_print_reg8("Status: %02x; ", 0xb, 0x38); | ||
AS560x_print_reg8("AGC: %3x; ", 0x1a, 0xff); | ||
AS560x_print_reg16("Angle: %04x\n\r", 0x0c, 0xFFF); | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
|
||
#ifndef RPI_PICO_SERVO_AS560X_H | ||
#define RPI_PICO_SERVO_AS560X_H | ||
#define I2C_TIMEOUT_US (100000) | ||
|
||
#define AS5601_ANGLE_MAX (0xFFFL) | ||
#define AS560x_STATUS_MAGNET_DETECTED (0x20) | ||
#define AS560x_STATUS_MAGNET_HIGH (0x08) | ||
#define AS560x_STATUS_MAGNET_LOW (0x10) | ||
|
||
/** Initializes sensor (i2c) bus | ||
*/ | ||
void as560x_init(); | ||
|
||
_Noreturn void i2cError(); | ||
|
||
/** Returns raw angle from the sensor | ||
* | ||
* @return Raw angle value [0...AS5601_ANGLE_MAX] | ||
*/ | ||
int as560xReadAngle(); | ||
|
||
/** Reads sensor status | ||
* | ||
* @return combo of AS560x_STATUS_MAGNET_DETECTED, AS560x_STATUS_MAGNET_HIGH, AS560x_STATUS_MAGNET_LOW | ||
*/ | ||
uint8_t as560xGetStatus(); | ||
|
||
/** Debug purposes only | ||
*/ | ||
__unused void sensorData(); | ||
|
||
#endif //RPI_PICO_SERVO_AS560X_H |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
// | ||
// Created by elmot on 23 Apr 2023. | ||
// | ||
|
||
#ifndef RPI_PICO_SERVO_PARAMS_H | ||
#define RPI_PICO_SERVO_PARAMS_H | ||
//degrees | ||
#define ANGLE_TOLERANCE (2) | ||
#define DEAD_ANGLE (5) | ||
#define SLOW_ANGLE (40) | ||
|
||
#define NO_PWM (100) | ||
#define SLOW_PWM (70) | ||
#define FAST_PWM (20) | ||
|
||
#endif //RPI_PICO_SERVO_PARAMS_H |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,6 @@ | ||
#include "servo.h" | ||
#include "hardware/timer.h" | ||
#include "hardware/pwm.h" | ||
|
||
volatile uint16_t pwm_count; | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters