User Guide: PicoC

Trex4Git edited this page Jun 14, 2015 · 54 revisions
Clone this wiki locally

Targets supporting PicoC

PicoC is supported on:

  • Sparky2
  • Colibri (TBS Gemini)
  • FlyingF4
  • Freedom
  • Quanton
  • Revolution
  • Revomini

... in other words, F4-based platforms.

Enable PicoC


TAB Enable: Check PicoC Interpreter (Save / Reboot) Enable PicoC

First Test



  • Startup = Disabled
  • Source = Disabled Enable PicoC

Serial Port


USB_VCPPort = PicoC In this case you have a virtual serial parallel to you normal USB for GCS Enable PicoC


Howto put a script to you board

GCS: Enable "Edit Gadgets Mode" Choose PicoC in a panel of your choice Enable PicoC Enter: printf("Here is your sript ...\n");

Click Button 1

Connect a Terminal application to your UART (perhaps CoolTerm) and connect

Click Button 2

Now you should see Here is your sript ...

Following some example scripts.

Sonar HC-SR04

  • OUT5 = Trigger,
  • IN2 = Echo
#include "system.h"
PWMFreqSet(2,10); /* set channel 5-6 to 10Hz */
PWMOutSet(5,10);  /* set channel 5 to 10µs pulse */
while (getchar() != 27) {
 float distance = PWMInGet(1)/58.0;
 printf("Distance = %f cm\n", distance);

Sinus-LED via Accessorychannel

  • Out5---470R---LED---GND
  • GCS Output: Channel 5 set 0 to 20.000 and neutral to 10.000
  • MixerSettings / Mixer5Type / Accessory0
#include "system.h"
#include "math.h"

int main() {
 float f = 0;
 while (!armed()) {
  printf("%f\n", sin(f));
 return 0;


Headers and Functions

There are virtual header files in PicoC. They create functions for calculation and communication between the UAVObjects and the user script.

Function (built-in) Description
void printf( char *, ...) ansi c
char *sprintf(char *, char *, ...) ansi c
char *gets(char *) ansi c
int getchar() get a char from stdIn (USART)
int putchar(int) put a char to stdOut (USART), returns the same char
void exit(int) abort the script and write the value to Exit Value
Header file string.h Description
void strcpy(char *,char *) ansi c
void strncpy(char *,char *,int) ansi c
int strcmp(char *,char *) ansi c
int strncmp(char *,char *,int) ansi c
void strcat(char *,char *) ansi c
char *index(char *,int) ansi c
char *rindex(char *,int) ansi c
int strlen(char *) ansi c
Header file math.h Description
float sin(float) sine
float cos(float) cosine
float tan(float) tangent
float asin(float) arc sine
float acos(float) arc cosine
float atan(float) arc tangent
float sinh(float) hyperbolic sine
float cosh(float) hyperbolic cosine
float tanh(float) hyperbolic tangent
float exp(float) e raised to the given power
float fabs(float) absolute value
float log(float) base 2 logarithm
float log10(float) base 10 logarithm
float pow(float,float) number raised to the given power
float sqrt(float) square root
float round(float) nearest integer
float ceil(float) nearest not less integer
float floor(float) nearest not greater integer
M_PI 3.14159265358979323846
M_E 2.7182818284590452354
Header file system.h Description Level
void delay(int) ms-delay for given value
void sync(int) like delay, but synchronized with last call. 0 resets interval. [ms]
unsigned long time() get current time count after boot up in ms
int armed() true when FlightStatusArmed=Armed, otherwise false
void AccessLevelSet(int) set UAVO access level. Used to secure some functions
void ChangeBaud(unsigned long) changes the PicoC USART rate
int TestValGet() PicoCStatusTestValue
void TestValSet(int i) PicoCStatusTestValue=i
float AccessoryValGet(int i) AccessoryDesiredInstance[i]AccessoryVal
void AccessoryValSet(int i,float f) AccessoryDesiredInstance[i]AccessoryVal=f
PWMFreqSet(int i, unsigned int f) set output frequency for channels i to f 1
PWMOutSet(int ch,int i) set pwm output channel ch to interval i [µs]
PWMInGet(int ch) get pwm interval of input ch [µs]
int TxChannelValGet(int c) µs-value of channel c from actual transmitter
void GPIOWrite(unsigned int pin_num, unsigned int command) Write a GPIO pin (0=resets the pin to low voltage , 1=sets the pin to high voltage, 2=toggle between high and low voltage)
int GPIORead(unsigned int pin_num) Read the value of a GPIO pin
Header file accels.h Unit / Description
typedef struct {
float x; m/s^2
float y; m/s^2
float z; m/s^2
float temperature; deg C
} AccelsData;
void AccelsGet(AccelsData *) get data object Accels
Header file attitudeactual.h Unit / Description
typedef struct {
float Roll; degrees
float Pitch; degrees
float Yaw; degrees
} AttitudeActualData;
void AttitudeActualGet(AttitudeActualData *) get data object AttitudeActual
Header file baroaltitude.h Unit / Description
typedef struct {
float Altitude; m
float Temperature; deg C
float Pressure; kPa
} BaroAltitudeData;
void BaroAltitudeGet(BaroAltitudeData *) get data object BaroAltitude
Header file flightbatterystate.h Unit / Description
typedef struct {
float Voltage; V
float Current; A
float BoardSupplyVoltage; V
float PeakCurrent; A
float AvgCurrent; A
float ConsumedEnergy; mAh
float EstimatedFlightTime; sec
} FlightBatteryStateData;
void FlightBatteryStateGet(FlightBatteryStateData *) get data object FlightBatteryState
Header file flightstatus.h Unit / Description
typedef struct {
unsigned char Armed;
unsigned char FlightMode; Manual=0, Acro=1, Leveling=2, VirtualBar=3, Stabilized1=4, Stabilized2=5, Stabilized3=6, Autotune=7, AltitudeHold=8, VelocityControl=9, PositionHold=10, ReturnToHome=11, PathPlanner=12, TabletControll=13
unsigned char ControlSource; Geofence=0, FailSafe=1, Transmitter=2, Tablet=3
} FlightStatusData;
void FlightStatusGet(FlightStatusData *) get data object FlightStatus
Header file gpsposition.h Unit / Description
typedef struct {
long Latitude; degrees x 10^-7
long Longitude; degrees x 10^-7
float Altitude; meters
float GeoidSeparation; meters
float Heading; degrees
float GroundSpeed; m/s
float PDOP;
float HDOP;
float VDOP;
unsigned char Status;
char Satellites;
} GPSPositionData;
void GPSPositionGet(GPSPositionData *) get data object GPSPosition
Header file gyros.h Unit / Description
typedef struct {
float x; deg/s
float y; deg/s
float z; deg/s
float temperature; deg C
} GyrosData;
void GyrosGet(GyrosData *) get data object Gyros
Header file magnetometer.h Unit / Description
typedef struct {
float x; deg/s
float y; deg/s
float z; deg/s
} MagnetometerData;
void MagnetometerGet(MagnetometerData *) get data object Magnetometer
Header file manualcontrol.h Description Level
FlightModeSet(int pos,int m) ManualControlSettingsFlightModePosition[pos]=m 1
void Stabilized1Set(int r,int p,int y) ManualControlSettingsStabilization1Settings[Roll,Pitch,Yaw]=(r,p,y) 1
void Stabilized2Set(int r,int p,int y) ManualControlSettingsStabilization2Settings[Roll,Pitch,Yaw]=(r,p,y) 1
void Stabilized3Set(int r,int p,int y) ManualControlSettingsStabilization3Settings[Roll,Pitch,Yaw]=(r,p,y) 1
Header file pathdesired.h Unit / Description
typedef struct {
float Start[3]; North,East,Down m
float End[3]; North,East,Down m
float StartingVelocity; m/s
float EndingVelocity; m/s
float ModeParameters;
unsigned char Mode;
} PathDesiredData;
void PathDesiredGet(PathDesiredData *) get data object PathDesired
void PathDesiredSet(PathDesiredData *) set data object PathDesired
Header file pathstatus.h Unit / Description
typedef struct {
float fractional_progress;
float error; m
unsigned char Status;
} PathStatusData;
void PathStatusGet(PathStatusData *) get data object PathStatus
Header file positionactual.h Unit / Description
typedef struct {
float North; m
float East; m
float Down; m
} PositionActualData;
void PositionActualGet(PositionActualData *) get data object PositionActual
Header file pid.h Description
typedef struct {
float p; Kp
float i; Ki
float d; Kd
float iLim; limits the integral accumulator
float iAccumulator;
float lastErr;
float lastDer;
float dTau;
} pid;
float pid_apply(pid *,float err,float dT) update pid and return controller value
float pid_apply_antiwindup(pid *,float err,float min_bound,float max_bound,float dT) like pid_apply() but with anti windup limit
float pid_apply_setpoint(pid *,float setpoint,float measured,float dT)
void pid_zero(pid *) reset pid structure
void pid_configure(pid *,float p,float i,float d,float iLim,float cutoff) configure the settings for a pid structure