bolderflight / sbus

Arduino and CMake library for communicating with SBUS receivers and servos.
MIT License
397 stars 141 forks source link

Use PWM servo #35

Closed pierrotm777 closed 3 years ago

pierrotm777 commented 4 years ago

Hello, I try to use your code with a X8R and a Pro Mini. I try this code: `/* SBUS_example.ino Brian R Taylor brian.taylor@bolderflight.com

Copyright (c) 2016 Bolder Flight Systems

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */

// This example reads an SBUS packet from an // SBUS receiver (FrSky X8R) and then takes that // packet and writes it back to an SBUS // compatible servo. The SBUS out capability (i.e. // writing a command to the servo) could be generated // independently; however, the packet timing would need // to be controlled by the programmer, the write function // simply generates an SBUS packet and writes it to the // servos. In this case the packet timing is handled by the // SBUS receiver and waiting for a good packet read.

include "SBUS.h"

include

include / Needed for library /

include

include

include

// software SettingsPort #1: RX = digital pin 10, TX = digital pin 11 SoftSerial MyConsole(10,11);

// a SBUS object, which is on hardware // serial port 1 SBUS x8r(Serial);

// channel, fail safe, and lost frames data uint16_t channels[16]; bool failSafe; bool lostFrame;

SoftRcPulseOut Servo1;

void setup() {

MyConsole.begin(57600); delay(500); MyConsole << "OK" << endl;

Servo1.attach(4);

// begin the SBUS communication x8r.begin(); }

void loop() {

uint8_t Width_us =1500; // look for a good SBUS packet from the receiver if(x8r.read(&channels[0], &failSafe, &lostFrame)){

// write the SBUS packet to an SBUS compatible servo
x8r.write(&channels[0]);
MyConsole << _DEC(&channels[0]) << endl;

Width_us = map(&channels[0], -100, +100, 900, 2100);
Servo1.write_us(Width_us);
SoftRcPulseOut::refresh(1);

} }`

But the console return me always 434. Can you explain me how to map a channel please ? I use a NPN transistor for invert x8r SBUS signal.

Thanks for this share

Pierre

flybrianfly commented 3 years ago

Sorry, I haven't used Arduino's map function.