Hi !
I have got an self calibrating sensor now the Adafruit BNO055 : Overview | Adafruit BNO055 Absolute Orientation Sensor | Adafruit Learning System
I´m using the map to change the value from 0.00-360.00 to -360-360 to controll the value compassAngle1 and that value i use in function 2 " move stepper to compassAngle1.
i´m having two problems with that function, the first it that when the compass angle is 0 or 360 the stepper has to take an full rotation.
The other problem is that when i Singleclick the stepper moves to the compassAngle1 and then returns to function 1, i want the stepper to keep holding the compassAngle1 ( function2) position in 60 seconds or if the manual function is activated.
When i Singleclick and activates "function 2" i want to take the compass reading (event.orientation.x) and save that value when function 2 is activated, i do that with "compassAnglePos"
Then i want to hold the axleposition in compassAnglePos direction even if the motor mount rotates. I understand that i have to calculate the difference between compassAnglePos and the event.orientation.x to adjust the axlepositon i just dont know how? ![]()
#include <MobaTools.h>
#include <Wire.h>
#include <Adafruit_Sensor.h>
#include <Adafruit_BNO055.h>
#include <utility/imumaths.h>
// hardware related definitiona
// Set pins according to your hardware
const int stepPin = 6; //3;
const int dirPin = 5; //4;
const int enPin = 7; //2;
const byte buttonPins[] = {2};
const byte joyLRPin = A1;
const int ledPin = LED_BUILTIN;
//----------------------------
/* Set the delay between fresh samples */
#define BNO055_SAMPLERATE_DELAY_MS (100)
// Check I2C device address and correct line below (by default address is 0x29 or 0x28)
// id, address
Adafruit_BNO055 bno = Adafruit_BNO055(55, 0x28);
int compassAngle = 12; // compassAngle for testing**
int compassAngle1 = 0; // compassAngle adjusted from BNO055 Reading** BNO055 Read value 0.00-360.00
int compassAnglePos = 0; // compassAngle when Singleclick ( function 2 is activated)
int compassAngleAdjusted = 0;
const long maxAngle = 400; // this is the max angle in every direction.
const int maxSpeed = 1500; // max Speed is 2500 steps/sec ( depends what the motor can do )
const int minSpeed = 500; // lowest Speed
const int stepsPerRev = 200*50; // steps per revolution with gear box
//-----------------------------
// limits for joystick
const int joyLimits[] = { 245, 450, 560, 774 };
//const int joyLimits[] = { 0, 500, 524, 1024 };
//------------------------------------------------------------------------------
int joyLR;
byte function = 1; // default is joystick
int motorSpeed = 1500; // Steps/sec
// create MobaTools objects
MoToStepper stepper1( stepsPerRev, STEPDIR ); // create a stepper instance
MoToButtons myButtons( buttonPins, 1, 20, 3000 ); // manage buttons(s), longpress is 3 seconds
MoToTimebase printTimer; // Timer to print in regular intervals without blocking sketch
MoToTimebase joyTimer; // Timer to read joystick in regular intervals without blocking sketch
void setup() {
Serial.begin (115200);
printTimer.setBasetime( 1000 ); // print values every second
joyTimer.setBasetime( 100 ); // read joystick 10 times a second
sensor_t sensor;
bno.getSensor(&sensor);
if(!bno.begin())
{
/* There was a problem detecting the BNO055 ... check your connections */
Serial.print("Ooops, no BNO055 detected ... Check your wiring or I2C ADDR!");
while(1);
}
delay(1000);
bno.setExtCrystalUse(true);
pinMode(ledPin, OUTPUT);
// initiate stepper
stepper1.attach( stepPin, dirPin );
stepper1.setSpeed(motorSpeed ); // rev/min (if stepsPerRev is set correctly)
stepper1.setRampLen( 100 ); // set ramp length to what the motor needs to not loose steps
stepper1.attachEnable( enPin, 100, LOW ); // disable motor current in standstill
}
void loop() {
/* Get a new sensor event */
sensors_event_t event;
bno.getEvent(&event);
myButtons.processButtons();
compassAngle1 = map (event.orientation.x, 0, 360 ,-360, 360); // Map value from BNO055 to stepper -360 to 360
// select function mode by button presses
if ( myButtons.longPress(0) ) {
// button pressed long -> set referencepoint
Serial.println( "Set referencepoint" );
stepper1.setZero();
function = 1;
}
switch ( myButtons.clicked(0) ) {
case NOCLICK:
; // do nothing
break;
case DOUBLECLICK:
Serial.println( "Return RP" );
stepper1.setSpeedSteps( 20000 ); // Speed when moving home
stepper1.write(0);
function = 3;
break;
case SINGLECLICK:
Serial.println( "Set function 2" );
stepper1.setSpeedSteps( 20000 ); // Speed when moving home
stepper1.write(compassAngle1);
function = 2;
compassAnglePos = event.orientation.x;
compassAngleAdjusted = compassAnglePos;
break;
}
if ( event.orientation.x >= compassAnglePos ) {
compassAngleAdjusted ++;
}
if ( event.orientation.x <= compassAnglePos ) {
compassAngleAdjusted - 1;
}
if (joyTimer.tick() ) {
// read joystick and set stepper speed accordingly
joyLR = analogRead(joyLRPin);
// if the joystic is not in the middle ===> its activ
bool joyActive = !(joyLR > joyLimits[1] && joyLR < joyLimits[2]);
switch ( function ) {
case 1: // mode 1 - joystick control
if ( !joyActive ) // neutral area
{
digitalWrite(ledPin, HIGH);
stepper1.rotate(0 ); // stop stepper if joystick in the middle
}
else
{ // move the motor in desired direction
digitalWrite(ledPin, LOW);
if ( joyLR >= joyLimits[2] ) {
// move towards max angle
motorSpeed = map ( joyLR , joyLimits[2], joyLimits[3] , minSpeed, maxSpeed ); // Values may be adjusted to the joystick
stepper1.setSpeedSteps( motorSpeed * 10 );
stepper1.write( maxAngle );
} else {
// move towards min angle
motorSpeed = map ( joyLR , joyLimits[1], joyLimits[0] , minSpeed, maxSpeed ); // Values may be adjusted to the joystick
stepper1.setSpeedSteps( motorSpeed * 10 );
stepper1.write( -maxAngle );
}
}
break;
case 2: // // Move to angle set by compass for testing "CompassAngle",
// switch back to joystick control if position reached or joystick is active
if ( !stepper1.moving() || joyActive ) function = 1;
break;
case 3: // return to reference point,
// switch back to joystick control if reached or joystick is active
if ( !stepper1.moving() || joyActive ) function = 1;
break;
}
}
if ( printTimer.tick() ) {
// debug printing every second
Serial.print( "CurrentAngle "); Serial.print(stepper1.read() );
Serial.print ( " step "); Serial.print (stepper1.readSteps() );
Serial.print ( " DIR ");
if (joyLR >= joyLimits[2]) Serial.print ( "right") ;
else if (joyLR <= joyLimits[1]) Serial.print( "left");
else Serial.print( "stop");
Serial.print( "compassAngle1 "); Serial.print(compassAngle1);
Serial.print( "event.orientation.x "); Serial.print(event.orientation.x);
Serial.print( "compassAnglePos "); Serial.print(compassAnglePos);
Serial.print( "compassAngleAdjusted "); Serial.println(compassAngleAdjusted);
}
}
Best Regards
Andreas