Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Arduino Rapid Prototyping with Python, C++ and ...

Arduino Rapid Prototyping with Python, C++ and Web Services

The Arduino rapid prototyping electronics platform offers a low barrier to entry for hobby use of microcontrollers for art, control, automation and data logging projects. The so-called Arduino programming language is a thin veneer over C++. Program changes - of which there are usually many during project development - require that code changes be re-compiled, uploaded to the Arduino over USB and the program restarted, a process which can quickly become tedious for programs with long calibration times or for systems which interact with the physical environment. The requirement for a physical USB connection can be awkward or impossible with the Arduino in situ. Finally, program and working space on microcontroller platforms is extremely limited, so it can be difficult to temporarily squeeze in debugging, logging and diagnostic code which won't be needed in the final version.

An alternative approach is to implement web service APIs on the Arduino and transfer control logic into a separate program running on a 'real' computer. This frees us from the constraint of using C++ so we can use more flexible languages such as Python during development, with access to debuggers, charting, logging and all the other good things which can transform the development experience compared to the sparse Arduino environment. Importantly, there is no need to restart the Arduino, and physical access is not required since we can use web services over WiFi. Once the system is working and tuned, the control program can be recoded in C++ and uploaded to the Arduino once and for all.

I'll introduce Arduino and show the 'traditional' approach to development, then show how to implement web service APIs in C++ on the Arduino and access them from Python. The talk will include live demonstrations using real Arduino hardware.

Robert Smallshire

November 13, 2012
Tweet

More Decks by Robert Smallshire

Other Decks in Programming

Transcript

  1. Arduino Rapid Prototyping with Python, C++ and Web Services 1

    Robert Smallshire @robsmallshire Friday, 16 November, 12
  2. 3 Arduino Out of the Box An introduction to Arduino

    hardware and development Behind the Scenes Understanding how Arduino works and using professional tools Developing with Web Services Application architectures to ease Arduino prototyping 1 2 3 Friday, 16 November, 12
  3.          

                                                                             !  " "  "#$" ""    ""  "   " "         ""  "    ""   ""  ""   "" "  "  "   "   " "    %  &    "$  !  !  !                                    " " !         Friday, 16 November, 12
  4. 8271ES–AVR–07/2012 Features • High Performance, Low Power Atmel®AVR® 8-Bit Microcontroller

    Family • Advanced RISC Architecture – 131 Powerful Instructions – Most Single Clock Cycle Execution – 32 x 8 General Purpose Working Registers – Fully Static Operation – Up to 20 MIPS Throughput at 20MHz – On-chip 2-cycle Multiplier • High Endurance Non-volatile Memory Segments – 4/8/16/32KBytes of In-System Self-Programmable Flash program memory – 256/512/512/1KBytes EEPROM – 512/1K/1K/2KBytes Internal SRAM – Write/Erase Cycles: 10,000 Flash/100,000 EEPROM – Data retention: 20 years at 85qC/100 years at 25qC(1) – Optional Boot Code Section with Independent Lock Bits In-System Programming by On-chip Boot Program True Read-While-Write Operation – Programming Lock for Software Security • Atmel® QTouch® library support – Capacitive touch buttons, sliders and wheels – QTouch and QMatrix® acquisition – Up to 64 sense channels • Peripheral Features – Two 8-bit Timer/Counters with Separate Prescaler and Compare Mode – One 16-bit Timer/Counter with Separate Prescaler, Compare Mode, and Capture Mode – Real Time Counter with Separate Oscillator – Six PWM Channels – 8-channel 10-bit ADC in TQFP and QFN/MLF package Temperature Measurement – 6-channel 10-bit ADC in PDIP Package Temperature Measurement – Programmable Serial USART – Master/Slave SPI Serial Interface – Byte-oriented 2-wire Serial Interface (Philips I2C compatible) – Programmable Watchdog Timer with Separate On-chip Oscillator – On-chip Analog Comparator – Interrupt and Wake-up on Pin Change • Special Microcontroller Features – Power-on Reset and Programmable Brown-out Detection – Internal Calibrated Oscillator – External and Internal Interrupt Sources – Six Sleep Modes: Idle, ADC Noise Reduction, Power-save, Power-down, Standby, and Extended Standby Atmel 8-bit Microcontroller with 4/8/16/32KBytes In- System Programmable Flash ATmega48A; ATmega48PA; ATmega88A; ATmega88PA; ATmega168A; ATmega168PA; ATmega328; ATmega328P SUMMARY • 32768 bytes flash • bootloader • program • const static data† • 2048 bytes EEPROM • persistent data • 2048 bytes SRAM • stack • static • heap‡ † If you know what you’re doing ‡ A luxury you can’t afford Friday, 16 November, 12
  5. Many neat shields on the market Arduino Shields ‣ Many

    neat shields on the market ‣ Easy to fabricate your own using protoshields or PCB production 10 Friday, 16 November, 12
  6. Oh dear! Arduino Shields ‣ Shield physical design is incredibly

    dumb ‣ Most shields are badly designed, amateurish and mutually incompatible ‣ Getting some shields to work on the Mega requires hardware modification (better with Arduino R3) 11 Friday, 16 November, 12
  7. 12 Arduino Out of the Box An introduction to Arduino

    hardware and development Behind the Scenes Understanding how Arduino works and using professional tools Developing with Web Services Application architectures to ease Arduino prototyping 1 2 3 Friday, 16 November, 12
  8. 13 avr-gcc avr-libc avrdude http://www.nongnu.org/avr-libc/ Arduino.h Wire Serial SPI Ethernet

    SD EEPROM “Libraries” Arduino bootloader Arduino IDE core.a avr-ar blink.ino blink.cpp avr-objcopy blink.cpp.o SPI.cpp.o blink.cpp.elf blink.cpp.hex blink binary make Friday, 16 November, 12
  9. Removing the training wheels ‣ Eclipse IDE with CDT (C/C++

    integration) • Arduino plugin : Higher level http://eclipse.baeyens.it/ • AVR plugin : Lower level http://avr-eclipse.sourceforge.net/wiki/index.php/The_AVR_Eclipse_Plugin ‣ arscons - SCons builds for Arduino • https://github.com/suapapa/arscons ‣ AVR Freaks - Arduino-free AVR programming in C, C++, assembler • http://www.avrfreaks.net/ 14 Beyond the Arduino IDE Friday, 16 November, 12
  10. Expect to be tripped up by this stuff Gotchas ‣

    Different Arduino models (Uno / Mega) use different programmers “Arduino” versus “Atmel STK500 Version 2.0 firmware” ‣ Arduino community is (mostly) a bunch of amateurs with little experience of ‘real’ programming. Lots of bad information and unfounded hearsay. Mythology. ‣ Some C++ features rely on ‘missing’ pieces of libc (such as pure virtual functions) • extern "C" void __cxa_pure_virtual(void) { while(1); } 15 Friday, 16 November, 12
  11. 16 Arduino Out of the Box An introduction to Arduino

    hardware and development Behind the Scenes Understanding how Arduino works and using professional tools Developing with Web Services Application architectures to ease Arduino prototyping 1 2 3 Friday, 16 November, 12
  12. 19 Other Arduino web services ‣ RESTduino • http://jasongullickson.posterous.com/restduino-arduino-hacking-for-the-rest-of-us •

    low level pin access (digitalWrite, analogRead) • badly named - it’s not RESTful at all (no HATEOAS, no HTTP verbs) ‣ Teleduino • https://www.teleduino.org/ • Allows access to Arduino over the Internet (not just the network) via their centralised server. • low level pin access (digitalWrite, analogRead) Friday, 16 November, 12
  13. 20 Arduino Mega 2560 Arduino Ethernet & SD Card Shield

    Prototype Shield (PIC16F77 as servo controller) MAX6651 shield MAX6651 shield I2C bus SPI bus Linux laptop Ethernet Dallas One Wire bus H% Proprietary One Wire Bus Server cabinet environment controller Friday, 16 November, 12