Hybrid Programming with Arduino, Visual C# and Python for LettaPanda
Table of Contents
1 Hybrid Coding
Hybrid coding is not always my first choice.
Since LettaPanda is hybrid architecture,
- Intel Cherry Trail Z8350 Quad Core Processor and pre-installed with a full edition of Windows 10 Home Edition to run powerful tools such as Visual Studio, NodeJS, Java, Processing, and more.With existing APIs, develop projects on a LattePanda is the same as that on a normal PC - C#, Javascript, Ruby etc.
- ATmega Processor GPIO x 20 with Arduino IDE preinstalled.
- The OpenVINO runtime support Python API and C++ API.
Therefore, my only choice is hybride Hybrid Coding with Arduino, Visual C# and Python,
Refer to https://docs.lattepanda.com/content/1st_edition/hardware_introduction/ , Pinouts in area U1 are assigned to the X-Z8350 core, and Pinouts in area U2 are assigned to the ATmega32u4 core.Some pins have specialized functions:
- Analog Inputs: A0 - A5, A6 - A11 (on D4, D6, D8, D9, D10, and D12). The LattePanda has 12 analog inputs, labeled A0 through A11, all of which can also be used as digital I/O (inputs/ouptus). Each pin has a 10 bit resolution (i.e. 1024 different values). By default, their voltages measure from ground to 5 volts.
- Serial: D0 (RX) and D1 (TX). Used to receive (RX) and transmit (TX) TTL serial data.
- External Interruptions: D3 (interrupt 0), D2 (interrupt 1), D0 (interrupt 2), D1 (interrupt 3) and D7 (interrupt 4). These pins can be configured to trigger an interruption when measuring low values, a rising or falling edge, or a change in value.
- PWM: D3, D5, D6, D9, D10, and D13 provide 8-bit PWM output.
- SPI: D16 (MOSI), D14 (MISO), D15 (SCK).
- LED: D13 There is a built-in LED driven by digital pin 13.
2 Arduino Coding
Arduino coding according to https://docs.lattepanda.com/content/1st_edition/power_on/ and https://github.com/LattePandaTeam/.
Flash standard firmata for ATmega32u4 core to control Pinout in area U1 for motor control.
The board shall be selected as Arduino lenardo
The opensourced firmata using internal I2C bridging ATmega32u4 core with Intel Cherry Trail Z8350 Quad Core.
Keep this code running , ready for next part of code.
3 Visual C# Coding
Download the LattePanda.Firmata class library for VIsual C#, that is only programming language for now.
Using Visual studio 2017, create new project rovingEye for motor control logic here in Visual C#, Refer https://docs.lattepanda.com/content/1st_edition/io_programming_reference/ for API.
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Threading; using LattePanda.Firmata; namespace rovingEye { class Program { static void Main(string[] args) { string motormode; int motorduraion =200; Arduino arduino = new Arduino(); arduino.pinMode(4, Arduino.OUTPUT);//Set the digital pin 4 as output arduino.pinMode(5, Arduino.OUTPUT);//Set the digital pin 5 as output arduino.pinMode(6, Arduino.OUTPUT);//Set the digital pin 6 as output arduino.pinMode(7, Arduino.OUTPUT);//Set the digital pin 7 as output //Console.WriteLine("Hello, rovingEye."); if (args.Length > 0) { motormode = args[0]; if (motormode is "up") { Console.WriteLine($"Direction={motormode}"); arduino.digitalWrite(4, Arduino.LOW);//set the LED off arduino.digitalWrite(5, Arduino.HIGH);//set the LED off Thread.Sleep(motorduraion); } else if (motormode is "down") { Console.WriteLine($"Direction={motormode}"); arduino.digitalWrite(5, Arduino.LOW);//set the LED off arduino.digitalWrite(4, Arduino.HIGH);//set the LED off Thread.Sleep(motorduraion); } else if (motormode is "left") { Console.WriteLine($"Direction={motormode}"); arduino.digitalWrite(6, Arduino.LOW);//set the LED off arduino.digitalWrite(7, Arduino.HIGH);//set the LED off Thread.Sleep(motorduraion); } else if (motormode is "right") { Console.WriteLine($"Direction={motormode}"); arduino.digitalWrite(7, Arduino.LOW);//set the LED off arduino.digitalWrite(6, Arduino.HIGH);//set the LED off Thread.Sleep(motorduraion); } else { Console.WriteLine($"Direction= No operaion."); } // Reset the motor to still for new command, arduino.digitalWrite(4, Arduino.LOW);//set the LED off arduino.digitalWrite(5, Arduino.LOW);//set the LED off arduino.digitalWrite(6, Arduino.LOW);//set the LED off arduino.digitalWrite(7, Arduino.LOW);//set the LED off Thread.Sleep(200); } return; }
This part is simple, just parse the args and detect what command would be , than control Pins.
Build the project successfully
Run the exe file as expected.
4 Python Coding
Coding in Visual Code on OpenVINO venv Environment
Design the logic with python OpenVINO package
The following code can inject command to above exe file to run motor furward in "up" direction.
import os p1="up" # only valid for up, down, left, right para= "%s %s" %("rovingEye",p1) #print (para) os.system(para)
5 Chain Them Together
Hybrid coding process is chain above codes one by one one Lettapanda, to make it roving Eye in hash environment.