Commit c933e292 authored by Vidisha Srivastav's avatar Vidisha Srivastav
Browse files

resolving conflict in init_spi() structure

Merge remote-tracking branch 'origin/accel-drivers' into spi-driver
parents 08427ca3 6643bc0b
......@@ -17,7 +17,6 @@ Input: spi_sensor_t object
acceleration read_bird_accelerometer(spi_sensor_t *device) {
uint16_t divider = 1;
uint8_t accel_range = get_range(device);
printf("Range: %d ", accel_range);
acceleration accel;
switch(accel_range) {
case RANGE_16_G:
......@@ -34,7 +33,7 @@ acceleration read_bird_accelerometer(spi_sensor_t *device) {
break;
printf("Divider: %d\n", divider);
}
printf("Divider: %f\n", (double)divider);
// Values are stored in two registers as left-justified two's complement
// read into int16_t as these are stored as two's complement
// read high and low bytes, and shift right 4 (12b high-resolution mode)
......@@ -50,8 +49,6 @@ acceleration read_bird_accelerometer(spi_sensor_t *device) {
raw_z <<= 8;
raw_z |= _read_register_byte(device, _REG_OUT_Z_L | 0x80);
// x, y, z = struct.unpack("<hhh", self._read_register(_REG_OUT_X_L | 0x80, 6))
// convert from Gs to m / s ^ 2 and adjust for the range
accel.x = ((float)raw_x / divider) * STANDARD_GRAVITY;
accel.y = ((float)raw_y / divider) * STANDARD_GRAVITY;
......@@ -61,11 +58,51 @@ acceleration read_bird_accelerometer(spi_sensor_t *device) {
}
acceleration read_i2c_accelerometer(i2c_sensor_t *device){
acceleration accel;
accel.x = 0;
accel.y = 0;
accel.z = 0;
return accel;
float divider = 1;
uint8_t accel_range = get_range(device);
printf("Range: %d\n", accel_range);
// TODO: determine how to check that correct range is being read in. Currently range = 0, defaulting to 2G
acceleration accel;
switch(accel_range) {
case RANGE_16_G:
divider = 1365;
break;
case RANGE_8_G:
divider = 4096;
break;
case RANGE_4_G:
divider = 8190;
break;
case RANGE_2_G:
divider = 16380;
break;
}
printf("Divider: %f\n", (double)divider);
// Values are stored in two registers as left-justified two's complement
// read into int16_t as these are stored as two's complement
// read high and low bytes, and shift right 4 (12b high-resolution mode)
int16_t raw_x = _read_register_byte(device, _REG_OUT_X_H | 0x80);
raw_x <<= 8;
raw_x |= _read_register_byte(device, _REG_OUT_X_L | 0x80);
raw_x >>= 4;
int16_t raw_y = _read_register_byte(device, _REG_OUT_Y_H | 0x80);
raw_y <<= 8;
raw_y |= _read_register_byte(device, _REG_OUT_Y_L | 0x80);
raw_y >>= 4;
int16_t raw_z = _read_register_byte(device, _REG_OUT_Z_H | 0x80);
raw_z <<= 8;
raw_z |= _read_register_byte(device, _REG_OUT_Z_L | 0x80);
raw_z >>= 4;
printf("Raw Z: %f. Does this divided by divider = 1 when the accel is right side up and still?", raw_z);
// convert from Gs to m / s ^ 2 and adjust for the range
accel.x = ((float)raw_x / divider) * STANDARD_GRAVITY;
accel.y = ((float)raw_y / divider) * STANDARD_GRAVITY;
accel.z = ((float)raw_z / divider) * STANDARD_GRAVITY;
return accel;
}
/*
......@@ -75,17 +112,20 @@ acceleration get_raw_accel(ACCEL_TYPE side){
if (side == CENTER){
spi_sensor_t device = get_bird_sensor();
accel = read_bird_accelerometer(&device);
printf("X: %f\n", (double)accel.x);
printf("Y: %f\n", (double)accel.y);
printf("Z: %f\n", (double)accel.z);
}
/* else if(side==RIGHTWING){
i2c_sensor_t wing_device = get_wing_sensor(RIGHT);
accel = read_i2c_accelerometer(&wing_device);
else if(side==RIGHTWING){
// i2c_sensor_t wing_device = get_wing_sensor(RIGHT);
// accel = read_i2c_accelerometer(&wing_device);
}
else if (side==LEFTWING) {
i2c_sensor_t wing_device = get_wing_sensor(LEFT);
accel = read_i2c_accelerometer(&wing_device);
}*/
// i2c_sensor_t wing_device = get_wing_sensor(LEFT);
// accel = read_i2c_accelerometer(&wing_device);
}
else
printf("Invalid accelerometer choice. Please select LEFTWING(0), CENTER(1) or RIGHTWING(2)");
printf("Invalid accelerometer choice. Please select LEFTWING(0), CENTER(1) or RIGHTWING(2)\n");
return accel;
}
......
......@@ -27,6 +27,8 @@
#define LEFT true
#define RIGHT false
// add in Accel addresses 0x18 or 0x19(?) somewhere, not sure where
typedef struct {
float x;
float y;
......
#ifndef MICROPY_INCLUDED_ATMEL_SAMD_COMMON_HAL_MOTOR_CONTROL_I2C_H
#define MICROPY_INCLUDED_ATMEL_SAMD_COMMON_HAL_MOTOR_CONTROL_I2C_H
#include "shared-bindings/adafruit_bus_device/I2CDevice.h"
#include "shared-bindings/busio/I2C.h"
#include "shared-bindings/digitalio/DigitalInOut.h"
#include "peripherals/samd/pins.h"
#include "shared-bindings/motor_control/__init__.h"
// I2C Control Pins
#define CS_LEFT &pin_PA18
#define CS_RIGHT &pin_PA16
#define I2C_SCL (&pin_PA13)
#define I2C_SDA (&pin_PA12)
busio_i2c_obj_t left_i2c;
busio_i2c_obj_t right_i2c;
#define STOP 1
#define NO_STOP 0
i2c_sensor_t sensor_right;
i2c_sensor_t sensor_left;
void init_i2c(i2c_sensor_t *device){
//frequency = ;
//timeout;
common_hal_busio_i2c_construct(&left_i2c, I2C_SCL, I2C_SDA, frequency, timeout);
void init_i2c(i2c_sensor_t *device, uint16_t device_addr){
// init i2c bus using busio library
frequency = 100000;
timeout = 0; // TODO: determine appropriate value for this
common_hal_busio_i2c_construct(&device->bus, I2C_SCL, I2C_SDA, frequency, timeout);
// init chip select pins using digitalio library
// common_hal_digitalio_digitalinout_construct(&device.cs, CS_LEFT);
// common_hal_digitalio_digitalinout_switch_to_output(&device.cs, true, DRIVE_MODE_PUSH_PULL);
// construct i2c devices from shared-bindings/adafruit_bus_device/I2C_Device
//
common_hal_adafruit_bus_device_i2cdevice_construct(&device->sensor, &device->bus, device_addr);
// TODO: how to determine device address from generalized info??
}
static void write(busio_i2c_obj_t *device, uint16_t addr, const uint8_t *data, size_t len, bool transmit_stop_bit){
common_hal_busio_i2c_write(&device, addr, &data, len, transmit_stop_bit);
}
static void readInto(busio_i2c_obj_t *device, uint16_t addr, uint8_t *data, size_t len){
common_hal_busio_i2c_read(&device, addr, *data, len);
}
// translated directly from original Python https://github.com/adafruit/Adafruit_CircuitPython_LIS3DH/blob/main/adafruit_lis3dh.py
// i2c driver line 405
static void _write_register_byte(i2c_sensor_t *device, uint8_t reg, uint8_t value){
device->gbuf[0] = reg & 0xFF;
device->gbuf[1] = value & 0xFF;
common_hal_adafruit_bus_device_i2cdevice_lock(&device->sensor);
write(&device->bus, reg, &device->gbuf, 2, STOP); //TODO: should STOP be enabled or is that the wrong thing?
common_hal_adafruit_bus_device_i2cdevice_unlock(&device->sensor);
}
// translated directly from original Python https://github.com/adafruit/Adafruit_CircuitPython_LIS3DH/blob/main/adafruit_lis3dh.py
// i2c driver line 398
static void _read_register(i2c_sensor_t *device, uint8_t reg, size_t length){
device->gbuf[0] = reg & 0xFF;
// enter device, write desired reg to read from, read value of reg, and exit device
common_hal_adafruit_bus_device_i2cdevice_lock(&device->sensor);
write(&device->bus, reg, &device->gbuf, 1, STOP);
readInto(&device->bus, reg, &device->gbuf, length);
common_hal_adafruit_bus_device_i2cdevice_unlock(&device->sensor);
}
void initI2CSensor(i2c_sensor_t *device) {
// Check device ID
uint8_t device_id = _read_register_byte(device, _REG_WHOAMI);
if (device_id != 0x33) {
printf("Failed to find LIS3DH on chip select");
}
// Reboot
_write_register_byte(device, _REG_CTRL5, 0x80);
// TODO: wait/sleep for 5 ms
// Enable all axes, normal mode.
_write_register_byte(device, _REG_CTRL1, 0x07);
// Set 400Hz data rate.
set_data_rate(device, DATARATE_400_HZ);
// High res & BDU enabled.
_write_register_byte(device, _REG_CTRL4, 0x88);
// Enable ADCs.
_write_register_byte(device, _REG_TEMPCFG, 0x80);
// Latch interrupt for INT1
_write_register_byte(device, _REG_CTRL5, 0x08);
}
// wrapper function for sending wing sensor (accel) objects
// done to protect each object from external tampering
i2c_sensor_t get_wing_sensor(bool side){
if(LEFT){
return sensor_left;
......@@ -25,4 +99,6 @@ i2c_sensor_t get_wing_sensor(bool side){
else{
return sensor_right;
}
}
\ No newline at end of file
}
#endif
\ No newline at end of file
......@@ -18,8 +18,9 @@
typedef struct {
adafruit_bus_device_i2cdevice_obj_t sensor;
//TODO - buffer needs?
// digitalio_digitalinout_obj_t cs;
uint8_t gbuf[6];
digitalio_digitalinout_obj_t cs;
busio_i2c_obj_t bus;
} i2c_sensor_t;
void init_i2c(i2c_sensor_t *device);
......
......@@ -107,21 +107,25 @@ float get_bird_angle(void){
float get_wing_angle(bool left){ ///Will take in sensor values from WING accelerometer (LEFT or RIGHT depends on boolean)
float bird_angle = get_bird_angle();
float wing_x;
float wing_y;
float wing_z;
acceleration wing; //variable "wing" with accceleration struct
wing = get_raw_accel(LEFTWING); //accelerometer value is read (for the test done in the Python script, the pinout for the body accelerometer was used)
if (left){ //for left wing
//float wing_x = 0.0; //X acceleration value from wing sensor
wing_y = wing.y; //Y acceleration value from wing sensor
wing_z = wing.z; //Z acceleration value from wing sensor
if (left) { //for left wing
acceleration leftWing = get_raw_accel(LEFTWING);
wing_x = leftWing.x; //X acceleration value from wing sensor
wing_y = leftWing.y; //Y acceleration value from wing sensor
wing_z = leftWing.z; //Z acceleration value from wing sensor
}
else { //for right wing
//wing_x = 0.0; //X acceleration value from wing sensor
wing_y = wing.y; //Y acceleration value from wing sensor
wing_z = wing.z; //Z acceleration value from wing sensor
acceleration rightWing = get_raw_accel(RIGHTWING);
wing_x = rightWing.x; //X acceleration value from wing sensor
wing_y = rightWing.y; //Y acceleration value from wing sensor
wing_z = rightWing.z; //Z acceleration value from wing sensor
}
// Currently switch to make it work - do we wanna keep going or raise
// exception to stop things?
if (wing_z < 0.0002){ // This is to make sure the tangent calculation does not go to infinity
wing_z = 0.0005;
}
......@@ -174,4 +178,4 @@ void set_wing_angle(bool left, int degrees_C){
}
}
}
// return duty;} //only used for testing if duty cycle values can be printed
\ No newline at end of file
// return duty;} //only used for testing if duty cycle values can be printed
......@@ -43,19 +43,16 @@
#define DATARATE_LOWPOWER_1K6HZ 0x1000
#define DATARATE_LOWPOWER_5KHZ 0x1001
busio_spi_obj_t spi;
spi_sensor_t sensor_center;
extern void common_hal_time_delay_ms(uint32_t);
void init_spi()
void init_spi(spi_sensor_t *device)
{
//init spi bus
common_hal_busio_spi_construct(&spi, SCK, MOSI,MISO);
common_hal_busio_spi_construct(&device->bus, SCK, MOSI,MISO);
//init chip select pins
common_hal_digitalio_digitalinout_construct(&sensor_center.cs, CS_CENTER);
common_hal_digitalio_digitalinout_switch_to_output(&sensor_center.cs, true, DRIVE_MODE_PUSH_PULL);
common_hal_digitalio_digitalinout_construct(&device.cs, CS_CENTER);
common_hal_digitalio_digitalinout_switch_to_output(&device.cs, true, DRIVE_MODE_PUSH_PULL);
//construct spidevices
//These are the default values from shared-bindings/adafruit_bus_device/SPIDevice.c
......@@ -63,25 +60,26 @@ void init_spi()
uint8_t polarity = 0;
uint8_t phase = 0;
uint8_t extra_clocks = 0;
common_hal_adafruit_bus_device_spidevice_construct(&sensor_center.sensor, &spi, &sensor_center.cs,baudrate, polarity, phase, extra_clocks);
common_hal_adafruit_bus_device_spidevice_construct(&device.sensor, &device->bus, &device.cs, baudrate, polarity, phase, extra_clocks);
initSPISensors(&sensor_center);
initSPISensors(&device);
}
spi_sensor_t get_bird_sensor(){
printf("Retrieving bird sensor...\n");
return sensor_center;
}
//Writes to SPI bus, make sure the device has been "entered" into
static void write(uint8_t *buffer, size_t length)
static void write(spi_sensor_t *device, uint8_t *buffer, size_t length)
{
common_hal_busio_spi_write(&spi, buffer, length);
common_hal_busio_spi_write(&device->bus, buffer, length);
}
//Reads from SPI bus into buffer
static void readinto(uint8_t *buffer, size_t length)
static void readinto(spi_sensor_t *device, uint8_t *buffer, size_t length)
{
common_hal_busio_spi_read(&spi, buffer, length, 0);
common_hal_busio_spi_read(&device->bus, buffer, length, 0);
}
//See https://github.com/adafruit/Adafruit_CircuitPython_LIS3DH/blob/master/adafruit_lis3dh.py
......@@ -110,7 +108,7 @@ static void _write_register_byte(spi_sensor_t *device, uint8_t reg, uint8_t valu
device->gbuf[0] = reg & 0x7F;
device->gbuf[1] = value & 0xFF;
common_hal_adafruit_bus_device_spidevice_enter(&device->sensor);
write(device->gbuf, 2);
write(&device, &device->gbuf, 2);
common_hal_adafruit_bus_device_spidevice_exit(&device->sensor);
}
......@@ -132,7 +130,7 @@ void initSPISensors(spi_sensor_t *device) {
// Check device ID
uint8_t device_id = _read_register_byte(device, _REG_WHOAMI);
if (device_id != 0x33) {
if (device_id != 0x33) {
printf("Failed to find LIS3DH on chip select");
}
// Reboot
......
#ifndef MICROPY_INCLUDED_ATMEL_SAMD_COMMON_HAL_MOTOR_CONTROL_SPI_H
#define MICROPY_INCLUDED_ATMEL_SAMD_COMMON_HAL_MOTOR_CONTROL_SPI_H
#include "ports/atmel-samd/common-hal/motor_control/Control.h"
#include "shared-bindings/adafruit_bus_device/SPIDevice.h"
#include "shared-bindings/busio/SPI.h"
#include "peripherals/samd/pins.h"
......@@ -15,6 +14,7 @@
#include <math.h>
typedef struct {
busio_spi_obj_t bus;
adafruit_bus_device_spidevice_obj_t sensor;
uint8_t gbuf[6];
digitalio_digitalinout_obj_t cs;
......
-----BEGIN OPENSSH PRIVATE KEY-----
b3BlbnNzaC1rZXktdjEAAAAABG5vbmUAAAAEbm9uZQAAAAAAAAABAAAAMwAAAAtzc2gtZW
QyNTUxOQAAACBjxnJS3lPQksTx9yLIjK3BjmxsYjaVzFlbsKVqqG5NtwAAAJi5sm+/ubJv
vwAAAAtzc2gtZWQyNTUxOQAAACBjxnJS3lPQksTx9yLIjK3BjmxsYjaVzFlbsKVqqG5Ntw
AAAED8h4fTBfV/B63slY43QDA3byYMRaEBjtu88XhR9pvQdmPGclLeU9CSxPH3IsiMrcGO
bGxiNpXMWVuwpWqobk23AAAAEXNzaCBrZXkgZm9yIGVlMTg1AQIDBA==
-----END OPENSSH PRIVATE KEY-----
ssh-ed25519 AAAAC3NzaC1lZDI1NTE5AAAAIGPGclLeU9CSxPH3IsiMrcGObGxiNpXMWVuwpWqobk23 ssh key for ee185
......@@ -8,6 +8,7 @@
#include "shared-bindings/neopixel_write/__init__.h"
#include "shared-bindings/microcontroller/Pin.h"
#include "shared-module/ee285/__init__.h"
#include "ports/atmel-samd/common-hal/motor_control/PID.h"
#define FIXED 4
......@@ -170,7 +171,7 @@ void shared_module_ee285_wing_angle(int side, float angle)
void shared_module_ee285_test(void)
{
serial_write_compressed(translate("This is the ee285 test function. \n Edit\
me in `software/firmware/circuitpython-main/shared-module/ee285/__init__.c`\
\n"));
init_spi();
test_spi_reset();
}
\ No newline at end of file
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment