platformio.ini:
[env:uno]
platform = atmelavr
board = uno
framework = arduino
lib_deps =
emotibit/EmotiBit BMI160 @ ^0.3.3
// ****************************************************************
// ****************************************************************
main.cpp:
#include <Arduino.h>
#include <BMI160Gen.h>
#define PWM_OUT_X PB3 // OC2A (D11)
#define PWM_OUT_Y PD3 // OC2B (D3)
void interrupts_init(void);
void podesi_akceleraciju_LR_TMR2(float acc_raw);
void podesi_akceleraciju_FR_TMR2(float acc_raw);
void setup() {
DDRB |= 1 << PWM_OUT_X; // OC2A (PB3) is output <-- D11
DDRD |= 1 << PWM_OUT_Y; // OC2B (PD3) is output <-- D3
DDRB |= 1 << PB0; // output for oscilloscope <-- D8
interrupts_init();
Serial.begin(9600);
while (!Serial)
; // wait for the port
Serial.println("Initialisation...");
//BMI160.begin(BMI160GenClass::SPI_MODE, /* SS pin# = */ 10);
BMI160.begin(BMI160GenClass::I2C_MODE);
uint8_t dev_id = BMI160.getDeviceID();
Serial.print("DEVICE ID: ");
Serial.println(dev_id, HEX);
// gyro range = 250 deg/s
BMI160.setGyroRange(250);
// accelerometer range = 2g
BMI160.setAccelerometerRange(2);
Serial.println("END initialisation");
}
void loop() {
//int gxRaw, gyRaw, gzRaw;
int axRaw, ayRaw, azRaw;
// BMI160.readGyro(gxRaw, gyRaw, gzRaw);
BMI160.readAccelerometer(axRaw, ayRaw, azRaw);
podesi_akceleraciju_LR_TMR2(ayRaw);
podesi_akceleraciju_FR_TMR2(axRaw);
delay(5); // adjust the looptime
// debug:
// for measuring looptime
PINB = 0x01; // pulse for oscilloscope (D8)
}
// Generating high frequency PWM signal with duty cycle
// proportional to the acceleration measured by BMI160
void podesi_akceleraciju_LR_TMR2(float acc_raw) {
// -32768 to +32767 --> 0 do 255 (almost)
float val = ((acc_raw * 127.0 * 2) / 32768.0) + 128;
// for Teleplot
// Serial.print(">dLR:");
// Serial.println(val);
OCR2A = val; // set duty cycle at D11
}
// Generating high frequency PWM signal with duty cycle
// proportional to the acceleration measured by BMI160
void podesi_akceleraciju_FR_TMR2(float acc_raw) {
// -32768 to +32767 --> 0 do 255 (almost)
float val = ((acc_raw * 127.0 * 2) / 32768.0) + 128;
// for Teleplot
// Serial.print(">dFR:");
// Serial.println(val);
OCR2B = val; // set duty cycle at D3
}
// Timer2 is used for both the PWM signal and as the time base
// prescaler = 1, phase correct PWM --> PWM frequency:
// 16000000/510 = 31372.5 Hz --> 31.875 µs
// The same frequency is for Timer2 overflow interrupts
void interrupts_init(void) {
TCCR2A |= 1 << WGM20; // phase correct PWM mode, TOP = 0xff
TCCR2B |= 1 << CS20; // prescaler = 1
//TCCR2B &= ~(1 << CS21);
// TCCR2B |= 1 << CS21; // debug (prescaler = 8)
// No need to reset CS22 if using bare metal firmware because it is LOW
// by default but if using Arduino framework that bit has to be reset!
TCCR2B &= ~(1 << CS22); // set by Arduino framework!!
TCCR2A |= (1 << COM2A1) | (1 << COM2B1); // clear OC2A and OC2B on compare match
TIMSK2 = 1 << TOIE2; // enable timer2 overflow interrupt
sei(); // enable interrupts
}
// For measuring the interrupt frequency
ISR(TIMER2_OVF_vect) {
// Frequency has to be 31372.5 / 2 = 15686.25 Hz
// To measure the interrupt frequency uncomment the next line and
// comment out the same line for measuring the looptime in loop()
// PINB = 0x01; // pulse for oscilloscope (D8)
}
Here are some pictures:
The jumper wire is pull up "resistor" for starting the controller in analog mode. Without that the controller starts in digital mode and it could be switched into analog mode by a button. This is "ARC" version of the PCB where it isn't so easy to solder the wires because the traces are very tiny but there is CY-822A version of the PCB where the wires could be soldered by everyone.
Red wire is +5V from the USB for powering up the microcontroller and the green wire is X axis analog input. The other side of the green wire is connected to D11 through RC filter (R = 1k, C = 100 µF).
Y axis output is D3.
The VPX settings depend on the weight of the box. With my box which is quite heavy I am using:
X-Max 100, X-Gain 80% Y-Max 100, Y-Gain 180% DeadZone 4%
Edited by ChupoCro, 21 October 2024 - 12:56 AM.









Top














are all trademarks of VPFORUMS.