Ada 2012 and SPARK
Crazyflie and Railway demos
Eric Perlade
2015/10/01
CrazyFlie 2.0
• 27g ready to fly drone
• Ideal for indoor use
• Android and iOS app (Bluetooth LE)
• Python client using a radio dongle
• Based on STM32F4 (ARM Cortex M4) and NRF5181 (radio chip)
• Firmware entirely written in C
• 5900 lines of code (without including drivers, HAL and external
libs)
• Based on FreeRTOS (tasking)
Why ?
• Drones security and safety becoming an important issue
• Stricter certification regime for drones coming in Europe
• AdaCore is partnering with Squadrone Systems to build open-
source certifiable drone in Ada/SPARK
• Will show the feasibility of this project
First Steps … towards safety
Q1 ) What code parts are the most critical in a drone firmware?
Answer: the parts related to the stabilization system
Action Items:
– Re-implementing stabilization system in SPARK 2014
– Proving absence of runtime errors
Stabilization system
Stabilization system
Issues with original source code
• The C code was not designed to be formally proved
• Stabilization system in C uses C predefined types (float, int etc.)
• Absence of runtime errors can’t be proved on calculations with general
types
• Example:
float calculateError(float measured, float desired)
{
return desired – measured;
}
// Will cause an obvious overflow if called with
// FLT_MIN and FLT_MAX…
Solution with SPARK 2014
• Each module of the stabilization system transformed into a SPARK
package
• Use of constrained types and subtypes (ex: defining a type
T_Angle instead of using the general Ada type Float)
• Genericity for sharing code
• Insert saturation when needed
Result
• Proof of absence of runtime errors on every package
• Discovery of one bug related to overflows, corrected by the
Bitcraze team later
Next Steps … towards more safety
Q2) What code parts are critical in a drone firmware?
Answer: whole!
Action Items:
Re-implementing the whole firmware in Ada 2012 and SPARK
• Replacing the OS by Ravenscar runtime
• Rewriting other modules and drivers
SPARK 2014 and C
SPARK 2014
High-Level C code
Low-Level C code
Runtime and Drivers
• Replace FreeRTOS by a Ravenscar based runtime targeting the
STM32F4
• Replace all the FreeRTOS tasks using the Ravenscar tasking
model (tasks, protected objects)
• Rewrite the Crazyflie drivers using ST Peripheral Drivers in Ada
SPARK 2014, Ada 2012 and no C
SPARK 2014
High-Level Ada 2012 code
Low-Level Ada 2012 code
Goal achieved ?
• Crazyflie with a 100% Ada and SPARK firmware in 5 months
without any previous Ada nor fomal methods experience
• But still not easy to pilot
One last question, what code part would be critical in a drone
firmware?
Answer: A secret recovery feature
Action Item:
Implementing free fall recovery mode
Free Fall detection
When placed on a flat surface Free-fall detected
Recovery and Landing
• Set desired angles to 0.0 for roll and pitch
• High thrust applied after a free-fall
• Thrust slowly decreased until a minimum that permits the drone to
land properly
• Calculate the acceleration variance when the drone is in the
descending phase
• If variance is high, the drone has landed, recovery is over !
Real life validation
Action Item:
Dropping the flie
Railway safe signaling Demo
Why ?
• To prove the absence of collision using SPARK 2014
• To get closer to customer experience
• And mainly to play with our new Raspberry Pi 2 port !
Hardware
• Electrical railway modelling kit
• On-off power relay to control trains
• Hall effect sensors to detect trains
• Turnout motors to control switches
• Raspberry Pi 2
One way track model
Controlled
section
On/Off
Uncontrolled
section
always on
Sensor
Slow train coming
Slow train going on
Slow train detected
Slow train still going
Slow train detected again
Slow train going away
Turnout the tricky part
Turnout the tricky part
Railway layout
Software design
with SPARK_Mode
Signaling Manager
(protected object)
Sensor monitoring
(Task)
SDL Graphical Interface
(task)
Train Simulator
(Task)
HW interface
Raspberry Pi 2 GPIO
SPARK proof
Real life validation
Action Item:
Dropping the train
Conclusion
• It works!
• Ada 2012 mixed with SPARK 2014 are ready for the industry
• Easy to access and learn technology
• Demo sources will be available on GitHub
• Have a look at AdaCore University
• http://university.adacore.com/

Tech Days 2015: Ada 2012 and Spark Crazyflie and Railway Demo

  • 2.
    Ada 2012 andSPARK Crazyflie and Railway demos Eric Perlade 2015/10/01
  • 3.
    CrazyFlie 2.0 • 27gready to fly drone • Ideal for indoor use • Android and iOS app (Bluetooth LE) • Python client using a radio dongle • Based on STM32F4 (ARM Cortex M4) and NRF5181 (radio chip) • Firmware entirely written in C • 5900 lines of code (without including drivers, HAL and external libs) • Based on FreeRTOS (tasking)
  • 4.
    Why ? • Dronessecurity and safety becoming an important issue • Stricter certification regime for drones coming in Europe • AdaCore is partnering with Squadrone Systems to build open- source certifiable drone in Ada/SPARK • Will show the feasibility of this project
  • 5.
    First Steps …towards safety Q1 ) What code parts are the most critical in a drone firmware? Answer: the parts related to the stabilization system Action Items: – Re-implementing stabilization system in SPARK 2014 – Proving absence of runtime errors
  • 6.
  • 7.
    Issues with originalsource code • The C code was not designed to be formally proved • Stabilization system in C uses C predefined types (float, int etc.) • Absence of runtime errors can’t be proved on calculations with general types • Example: float calculateError(float measured, float desired) { return desired – measured; } // Will cause an obvious overflow if called with // FLT_MIN and FLT_MAX…
  • 8.
    Solution with SPARK2014 • Each module of the stabilization system transformed into a SPARK package • Use of constrained types and subtypes (ex: defining a type T_Angle instead of using the general Ada type Float) • Genericity for sharing code • Insert saturation when needed Result • Proof of absence of runtime errors on every package • Discovery of one bug related to overflows, corrected by the Bitcraze team later
  • 9.
    Next Steps …towards more safety Q2) What code parts are critical in a drone firmware? Answer: whole! Action Items: Re-implementing the whole firmware in Ada 2012 and SPARK • Replacing the OS by Ravenscar runtime • Rewriting other modules and drivers
  • 10.
    SPARK 2014 andC SPARK 2014 High-Level C code Low-Level C code
  • 11.
    Runtime and Drivers •Replace FreeRTOS by a Ravenscar based runtime targeting the STM32F4 • Replace all the FreeRTOS tasks using the Ravenscar tasking model (tasks, protected objects) • Rewrite the Crazyflie drivers using ST Peripheral Drivers in Ada
  • 12.
    SPARK 2014, Ada2012 and no C SPARK 2014 High-Level Ada 2012 code Low-Level Ada 2012 code
  • 13.
    Goal achieved ? •Crazyflie with a 100% Ada and SPARK firmware in 5 months without any previous Ada nor fomal methods experience • But still not easy to pilot One last question, what code part would be critical in a drone firmware? Answer: A secret recovery feature Action Item: Implementing free fall recovery mode
  • 14.
    Free Fall detection Whenplaced on a flat surface Free-fall detected
  • 15.
    Recovery and Landing •Set desired angles to 0.0 for roll and pitch • High thrust applied after a free-fall • Thrust slowly decreased until a minimum that permits the drone to land properly • Calculate the acceleration variance when the drone is in the descending phase • If variance is high, the drone has landed, recovery is over !
  • 16.
    Real life validation ActionItem: Dropping the flie
  • 17.
  • 18.
    Why ? • Toprove the absence of collision using SPARK 2014 • To get closer to customer experience • And mainly to play with our new Raspberry Pi 2 port !
  • 19.
    Hardware • Electrical railwaymodelling kit • On-off power relay to control trains • Hall effect sensors to detect trains • Turnout motors to control switches • Raspberry Pi 2
  • 20.
    One way trackmodel Controlled section On/Off Uncontrolled section always on Sensor
  • 21.
  • 22.
  • 23.
  • 24.
  • 25.
  • 26.
  • 27.
  • 28.
  • 29.
  • 30.
    Software design with SPARK_Mode SignalingManager (protected object) Sensor monitoring (Task) SDL Graphical Interface (task) Train Simulator (Task) HW interface Raspberry Pi 2 GPIO
  • 31.
  • 32.
    Real life validation ActionItem: Dropping the train
  • 33.
    Conclusion • It works! •Ada 2012 mixed with SPARK 2014 are ready for the industry • Easy to access and learn technology • Demo sources will be available on GitHub • Have a look at AdaCore University • http://university.adacore.com/

Editor's Notes

  • #3 Change the title
  • #4 No solder Software that are provided by BitCraze
  • #7 Divided in 3 main modules: PID module, implementation of a general PID controller Controller module, used to control the 3 Attitude/Rate PIDs (for each axis) Stabilizer module, which contains the main stabilization loop (Read commands / IMU measurements  Update the controller  Send output to the actuators)
  • #9 Post on the AdaCore blog explaining the techniques I used
  • #11 SPARK color #853577 133, 53, 119 FB color #3B5998 (59,89,152)
  • #13 Crazyflie flying with a 100% Ada + SPARK firmware Discovery of some flaws on the remaining C parts Bug found in the Ravenscar runtime related with interrupts and context switching (corrected)
  • #20 Or model railroading in US One way
  • #21 On off power control => no orange section One way only
  • #22 Bod dylan
  • #23 Bod dylan
  • #24 Bod dylan
  • #25 Bod dylan
  • #26 Bod dylan
  • #27 Bod dylan
  • #28 Requires control on validity of switching Can’t switch to occupied track procedure Can_Switch Contract_Cases => (Is_Occupied(Connected_Section) => not Can_Switch’Return, not Is_Occupied(Connected_Section) => Can_Switch’Return) f Can_Switch’Return then Is_Occupied(Connected_Section) = False Else Is_Occupied(Connected_Section) = True) procedure Swith Post => Is_Locked(Disconnected_Section)
  • #29 Requires control on validity of switching Can’t switch to occupied track
  • #34 Formal methods through SPARK 2014 mixed with Ada 2012 are industrial ready Summarize presentation content by restating the important points from the lessons. What do you want the audience to remember when they leave your presentation?