So after a very long time, I finally have my code functioning (some what).
The problem I have now is that the output sbus value doesn't match the percentage of the pot.
The pot (joystick) im using is a 3.3V, 3-axis joystick (X,Y,Z), with hall sensors for each axis.
The values for the end points I should be getting are 172 and 1811.
What im actually getting are 520 and 1520
My code (Started from the example on brtaylor's sbus library on github) already uses a scale factor and a bias, that is for all channels, but how can I re-scale or re-bias the individual channels?
Im thinking of using something like a calibration script (activated by perhaps making a digital pin go high) to determine the actual min and max values and store them for each analog input. I just don't know where to start.
Does anybody have suggestions?
Also, if the teensy input voltage varies at all, will the output value vary?
The problem I have now is that the output sbus value doesn't match the percentage of the pot.
The pot (joystick) im using is a 3.3V, 3-axis joystick (X,Y,Z), with hall sensors for each axis.
The values for the end points I should be getting are 172 and 1811.
What im actually getting are 520 and 1520
My code (Started from the example on brtaylor's sbus library on github) already uses a scale factor and a bias, that is for all channels, but how can I re-scale or re-bias the individual channels?
Im thinking of using something like a calibration script (activated by perhaps making a digital pin go high) to determine the actual min and max values and store them for each analog input. I just don't know where to start.
Does anybody have suggestions?
Also, if the teensy input voltage varies at all, will the output value vary?
Code:
#include <TimerOne.h>
#include "SBUS.h"
// output SBus on serial port 1
SBUS Transmit(Serial1);
//CH01: Roll (pin [A13])
uint16_t CH01 = A13;
int CH01Value = 0;
int CH01out = 0;
const int ledPin = 13; //just a heartbeat to show that the code is going
void setup() {
pinMode(ledPin,OUTPUT);
pinMode(A13,INPUT);
// serial to display the channel commands for debugging
Serial.begin(115200);
// begin the SBUS communication
Transmit.begin();
// setup the analog read resolution to 16 bits
analogReadResolution(16);
// setup an interrupt to send packets every 9 ms
Timer1.initialize(9000);
Timer1.attachInterrupt(sendSBUS);
}
void loop() {
//heartbeat timing
digitalWrite(ledPin, HIGH); // set the LED on
delay(200); // wait for a 0.2 seconds
digitalWrite(ledPin, LOW); // set the LED on
delay(100); // wait for a 0.1 seconds
digitalWrite(ledPin, HIGH); // set the LED on
delay(200); // wait for a 0.2 seconds
digitalWrite(ledPin, LOW); // set the LED off
delay(1000); // wait for a second
}
// reads analog and digital inputs and sends an SBUS packet
void sendSBUS() {
float scaleFactor = 1639.0f / 65535.0f;
float bias = 172.0f;
uint16_t channels[16];
CH01Value = analogRead(CH01); // read the analog input
CH01out = (uint16_t)(((float)CH01Value) * scaleFactor + bias); // linearly map the analog measurements (0-65535) to the SBUS commands (172-1811)
Serial.print("CH01: ");
Serial.print(CH01out); // print the channel command (172-1811)
Serial.print("\t");
Serial.println();
// write the SBUS packet to an SBUS compatible servo
Transmit.write(&channels[0]);
}