SBus Read End of trail navigation bar

Table of Contents

SBus Write

Sends an S.BUS frame over a stream. This block is intended for use in the main diagram.

Library

QUARC Targets/Communications/S.BUS

MATLAB Command Line

Click to copy the following command line to the clipboard. Then paste it in the MATLAB Command Window:

qc_open_library('quarc_library/Communications/S.BUS')

Description

SBus Write

The SBus Write block sends an S.BUS frame, which is 25 bytes of packed data, over a stream. The operation of this block is affected by the blocking mode of the stream connected to its input. Streams have two modes: blocking (the default) and non-blocking. If a Stream Call or Stream Answer block is used to create the stream, then the stream will be non-blocking. If a Stream Connect or Stream Accept block is used to create the stream then the blocking mode of the stream is determined by the Stream Connect or Stream Listen block.

This block constructs an S.BUS frame from its parameters and inputs, including the synchronization byte, 16 channels and flags information and sends the S.BUS frame over a client stream.

This block attempts to store the entire S.BUS frame in the stream buffer. If the Optimize parameter is set to minimize latency then it will also flush the stream each time that it executes. Otherwise, it only flushes the stream buffer to the underlying communication channel when the stream buffer is full. The size of the stream buffer is set by the Send buffer size parameter of the Stream Call or Stream Answer block that created the stream.

If an error occurs, then it returns a negative error code at its err output. If the connection is closed it is considered an error condition. The Compare to Error block may be used to check for specific error codes. If the stream is not valid because it is not yet connected, the err output will be zero since this condition is expected and the validity of the stream may be checked using the con output of the Stream Call or Stream Answer blocks.

This block does not support two threads calling SBus Write at the same time. However, SBus Write may be called by another thread at the same time as SBus Read.

Helpful Hints

Other uses of this block

Hint Although it is intended for use in the main diagram with the other Intermediate stream blocks, the SBus Write block may be used with the Advanced stream blocks, even in an asynchronous thread.

Configuration Requirements

Serial configuration

Configure The serial stream for S.BUS must be configured for 100000 baud, two stop bits, even parity and no flow control. The URI to use for the Stream Call block is:

serial://localhost:0?baud='100000',parity='even',stop='2',flow='none'

Note that the port number in the URI must be changed to match the UART used to produce the S.BUS signals.

Signal inversion

Configure S.BUS signals are generally inverted from the signals produced by a standard UART, so an inverter may be required on the UART used to drive the S.BUS receiver input. Alternatively, the flight controller may support an option to invert the S.BUS. For example, Cleanflight supports the set sbus_inversion=OFF/ON command on some flight controllers.

Input Ports

stm

A reference to the stream created by the Stream Call or Stream Answer block. If these blocks have not established a connection the err output of the SBus Write block will be zero.

ch

This input is always a 16 element vector where each element corresponds to a separate RC channel. Any of the standard Simulink data types may used. The interpretation of the values depends on the Offset and Gain parameters. Refer to the Input format parameter for details.

flg

The flags for the S.BUS frame, as a uint8.

Output Ports

stm

A reference to the stream. This output is merely a copy of the stm input. Providing this output makes it much easier to establish the execution order of Stream blocks in the diagram because Simulink generally executes daisy-chained blocks in sequence.

sent

A Boolean value indicating whether the S.BUS frame was written to the stream buffer successfully. This value will be true (non-zero) if the frame was written successfully and zero otherwise. If the data could not be written immediately without blocking then this output will be zero. This output merely indicates that the frame was written to the stream buffer. It does not validate that the frame was sent to the underlying communication channel or delivered to the remote peer successfully.

peer

A Boolean value indicating whether an error has occurred due to the peer not yet being designated. For a UDP server, the client is generally unknown until the first receive operation has been completed successfully, because the UDP protocol is a connection-less protocol. Not knowing the peer means no data will be sent. However, it is not regarded as a fatal error because the stream does not need to be closed in this case. It merely indicates that no client has "connected" yet by sending a datagram to the server. The UDP server should continue listening on the port and attempting to receive data. This output can be used to determine why no data has been sent (see the sent port) when there are no other errors and the operation should not have blocked.

err

An int32 value indicating whether any errors occurred. It is zero if no errors occurred and a negative error code otherwise. See Error Codes for the different error codes and their values. Use the Compare to Error block rather than the error code itself to check for specific error codes. To check for errors in general, simply test whether the output is non-zero. Note that the QERR_NO_DESIGNATED_PEER error is not reported at this output. It is signalled at the peer output instead since it is not regarded as a fatal error requiring closure of the stream. The QERR_WOULD_BLOCK error is also not reported at this output because it simply indicates that the data could not be sent without blocking. The sent output may be used to determine whether the data was actually written to the stream buffer.

Data Type Support

This block accepts any of the standard Simulink data types at its ch input.

Parameters and Dialog Box

SBus Write

sync (tunable online)

The synchronization byte for the S.BUS frame. Typical values are 0x0F or 0xF0 in hexadecimal. Use the MATLAB

hex2dec

MATLAB Command Line

Click to copy the following command line to the clipboard. Then paste it in the MATLAB Command Window:

doc hex2dec;
function to convert a hexadecimal string to a numeric value.

Input format (tunable online)

This parameter sets the Offset and Gain parameters to values which map common input formats to the 0 to 2047 range of values for SBUS channels. The table below describes the different input formats:

Format

Description

Bipolar percentage

Maps an input range of [-1,1] to the typical [1000,2000] range such that -1 becomes 1000, 0 becomes 1500 and 1 becomes 2000. Any values outside this range are mapped according to the same affine transformation. This input format is useful when the S.BUS channels represent -100% to 100% throttle.

Unipolar percentage

Maps an input range of [0,1] to the typical [1000,2000] range such that 0 becomes 1000 and 1 becomes 2000. Any values outside this range are mapped according to the same affine transformation. This input format is useful when the S.BUS channels represent 0 to 100% throttle.

Seconds

Maps the input range of [0.001,0.002] to the typical [1000,2000] range such that 0.001 becomes 1000 and 0.002 becomes 2000 so that the input is the RC pulse width in seconds. Any values outside this range are mapped according to the same affine transformation. This input format is useful when the S.BUS channels are being used to drive PWM pulse widths via the HIL blocks.

Microseconds (raw)

The input is the raw RC value, which is the RC pulse width in microseconds.

Custom

The custom input format allows a custom offset and gain to be specified for each input channel using the Offset and Gain parameters.

Offset (tunable online)

The offsets to apply to the input signals to convert to the 0 to 2047 SBUS range per channel. The gain is applied first and then the offset. This parameter is used when the Input format is set to Custom. The parameter may be a scalar or vector value, with up to 16 elements. If the value contains less than 16 elements then the last element is applied to the remaining channels. Hence, a scalar value will apply to all channels.

Gain (tunable online)

The gains to apply to the input signals to convert to the 0 to 2047 SBUS range per channel. The gain is applied first and then the offset. This parameter is used when the Input format is set to Custom. The parameter may be a scalar or vector value, with up to 16 elements. If the value contains less than 16 elements then the last element is applied to the remaining channels. Hence, a scalar value will apply to all channels.

Optimize for

Determines whether the block is optimized for maximizing throughput or minimizing latency. If this option is set to maximize throughput, then the block only flushes the stream buffer to the underlying communication channel when the stream buffer is full. In this way, as much data as possible can be transferred in each packet for packet-based protocols such as TCP/IP or UDP.

If this option is set to minimize latency then it flushes the stream buffer to the underlying communication channel each time the block executes. Doing so minimizes the time between when the data is generated and when it is sent to the remote host, but it may under-utilize the communications bandwidth since packets may not be full when they are sent.

Targets

Target Name

Compatible*

Model Referencing

Comments

QUARC Win32 Target

Yes

Yes

QUARC Win64 Target

Yes

Yes

QUARC Linux Nvidia Target

Yes

Yes

QUARC Linux QBot Platform Target

Yes

Yes

QUARC Linux QCar 2 Target

Yes

Yes

QUARC Linux QDrone 2 Target

Yes

Yes

QUARC Linux Raspberry Pi 3 Target

Yes

Yes

QUARC Linux Raspberry Pi 4 Target

Yes

Yes

QUARC Linux RT ARMv7 Target

Yes

Yes

QUARC Linux x64 Target

Yes

Yes

QUARC Linux DuoVero Target

Yes

Yes

QUARC Linux DuoVero 2016 Target

Yes

Yes

QUARC Linux Verdex Target

Yes

Yes

QUARC QNX x86 Target

Yes

Yes

Last fully supported in QUARC 2018.

Rapid Simulation (RSIM) Target

Yes

Yes

S-Function Target

No

N/A

Old technology. Use model referencing instead.

Normal simulation

Yes

Yes

If the Stream Call or Stream Answer block which created the stream is not active during normal simulation, then the SBus Write block does nothing. Otherwise it writes the S.BUS frame to the stream.

* Compatible means that the block can be compiled for the target.

See Also

 

navigation bar