Francesco Guerri0rla3Umberto Cucini
Published © GPL3+

UBalance

Self-leveling platform powered by whole new MAGUM library and UDOO Neo board

IntermediateFull instructions provided15 hours2,269
UBalance

Things used in this project

Hardware components

UDOO NEO
UDOO NEO
×1
Servomotor
×2
Breadboard (generic)
Breadboard (generic)
×1
Male/Male Jumper Wires
×6

Software apps and online services

Arduino IDE
Arduino IDE
UDOObuntu 2
MAGUM Library

Story

Read more

Schematics

Schematics and circuit diagram

This is the circuit used to connect servomotors to the UDOO Neo board

Code

Arduino sketch

Arduino
Works as a static controller and moves the engines
#include <Servo.h>
#include <servo_mqx.h>


// Definition Pins of servos
#define pinBase 10
#define pinTop 9
#define pinSe#include <Servo.h>
#include <servo_mqx.h>


// Definition Pins of servos
#define pinBase 10
#define pinTop 9
#define pinSerial 13

// Creation servo objects to control the servos
Servo servoBase;
Servo servoTop;

// Definition characteristics of servos
int posBase, posTop;            // indicate the positions of the servos
int rangeBase[] = {0, 89, 165}; // indicate the ranges of servos  {minVal,90°,maxVal}, minVal >= 0° , maxVal <=180°
int rangeTop[] = {0, 89, 180};  // minVal should be the angle value when the servos is in 0° position. maxVal should be the angle value when the servos is in 180° position

int flag = 0;

void setup() {
  servoBase.attach(pinBase);          // Set the command pin where the servo is attached
  servoTop.attach(pinTop);
  servoBase.write(rangeBase[1]);      // Set the servos to 90 degrees
  servoTop.write(rangeTop[1]);
  posBase = servoBase.read();         // Get the position of servos
  posTop = servoTop.read();

  Serial.begin(115200);               // Begin serial Communication at 115200 bps
  Serial.print('1');
}

void loop() {
  int Angles[2] ={1000, 1000};
  String string = "";
  String ang1,ang2;
  char input;
  while((Serial.available() > 0) && (flag == 0)) {    // Check if there are data in the buffer and get them Byte by Byte
    input = Serial.read();                            
    if(input != '|') {                                // the " | " character separate the values of angles
      string = string + input;
    }
    else {
      // Fine primo numero
      flag = 1;
    }
  }
  if(string != "") {
    Angles[0] = string.toInt();                       // Convertion from string to integer
    ang1 = string;
  
    flag = 0;
    string = "";
  }
  while((Serial.available() > 0) && (flag == 0)) {
    input = Serial.read();
    if(input != '|') {
      string = string + input;
    }
    else {
      // Fine secondo numero
      flag = 1;
    }
  }
  if(string != "") {
    flag = 0;
    Angles[1] = string.toInt();
    ang2 = string;
  }
  if(string != "") { 

      servoBase.write(-Angles[0] + rangeBase[1]);    
      servoTop.write(-Angles[1] + rangeTop[1]);
      posBase = servoBase.read();                               // Refresh the position variables
      posTop = servoTop.read();
      delay(150);
    
    Serial.println("1");                                        // Write to the serial the command that permit to send new data from the python script
  }
}

Python script

Python
Reads the angles from both gyroscope and accelerometer
import serial
from random import randint
import time
from magum import Magum

arduino = serial.Serial(
	port='/dev/ttyMCC',
    baudrate=115200,
    parity=serial.PARITY_NONE,
    stopbits=serial.STOPBITS_ONE,
    bytesize=serial.EIGHTBITS
) 
timeout = 0
DT = 0.02 # 20ms

magum = Magum(250,0,2,1)											# Instanzio magum

print "Collegamento Seriale OK"

# first of all calibrate the sensor
axisOffset = magum.calibrateSens(1000)

# arduino.close()
# arduino.open()
while True:															# Inizia il ciclo
	try:															# Questo serve solo per chiudere la seriale in caso di ctrl+C
		while timeout < 50:											# Finche' non ho nessun Byte nel buffer e non ho esaurito il numero di tentativi (50)
			print "Tentativo N. " + str(timeout + 1)
			textIn = arduino.read()	
			if textIn == '1':
				print "M4 Pronto"
				arduino.flushInput()								# Pulisco il buffer in entrata
				timeout = 0
				try:
					cFAngleAxis = magum.compFilter(DT,axisOffset)
				except IOError: #  avoid timeout errors
					pass
				base = 	int(round(cFAngleAxis[1],0))				# Rilevo gli angoli di inclinazione della UDOO
				top = int(round(cFAngleAxis[0],0))
				message = str(base) + '|' + str(top) + '|'			# Costruisco il messaggio da inviare	
				print str(message)											
				arduino.flushOutput()								# pulisco il buffer in uscita
				time.sleep(.100)
				arduino.write(message)								# invio il messaggio
				time.sleep(.200)
			else:
				print "M4 non disponibile\n"
				timeout += 1
				time.sleep(.100)
		print "Timeout\n"
		arduino.close()												# Chiudo la seriale
		exit(1)
	except KeyboardInterrupt:
		arduino.close()

Credits

Francesco Guerri

Francesco Guerri

4 projects • 16 followers
Computer engineering at University of Siena. Android developer and graphic designer.
0rla3

0rla3

3 projects • 8 followers
Ingegneria Informatica presso l'Univesità degli Studi di Siena. Web Developer (PHP, Javascript), iOS Developer.
Umberto Cucini

Umberto Cucini

2 projects • 10 followers
Bachelor degree in Computer science and Information engineering. In the future I'm attending the Master of science in Robotics Engineering

Comments