Andres Sabas
Published © CC BY

Controlling Robot MeARM with TLV493D, Joystick and Arduino

Print your own joystick to control your robot with TLV493D 3 degrees of freedom magnetic sensor and Arduino IDE.

IntermediateFull instructions provided3 hours832
Controlling Robot MeARM with TLV493D, Joystick and Arduino

Things used in this project

Hardware components

Bast Pro Mini M0
Electronic Cats Bast Pro Mini M0
×1
TLV493D Croquette
Electronic Cats TLV493D Croquette
×1
Kit MeArm v1
×1
Dupont Cable
×20
Protoboard
×1
pushbutton
×2
Magnet 5mm diameter x 1mm thickness
×1

Software apps and online services

Arduino IDE
Arduino IDE

Hand tools and fabrication machines

3D Printer (generic)
3D Printer (generic)

Story

Read more

Custom parts and enclosures

Base for Joystick

Stick

Schematics

Circuit in fritzing

Code

Code for Arduino IDE

Arduino
In this file you will find the code for this project to control a MeArm robot arm with a TLV493D sensor in Arduino IDE
#include <Servo.h>
#include <Tlv493d.h>


// Servo objects declaration
Servo servo1,servo2,servo3,servo4;

Tlv493d Tlv493dMagnetic3DSensor = Tlv493d(); // Tlv493d Object
const int switchPin = 8;      // Switch to turn on and off mouse
const int gripperPin = 9; // Switch to turn on and off gripper


int range = 80;               // Output range in mT of X or Y movement
bool armIsActive = false;    // Whether or not to control the arm
int lastSwitchState = LOW;        // Previous switch state
bool gripperIsActive = false;      // Whether or not to control the gripper
int lastGripperState = LOW;         // Previous gripper state


 
void setup() {
  pinMode(switchPin, INPUT);       // Switch pin
  pinMode(LED_BUILTIN, OUTPUT);  // LED pin
  pinMode(gripperPin, INPUT);       // Gripper pin
 
  Serial.begin(9600);    // Start serial monitor
  while(!Serial);        // TL493D Connection
  Tlv493dMagnetic3DSensor.begin();
 
  // Link servo objects to pins
  servo1.attach(2);
  //servo2.attach(3);  //Optional
  servo3.attach(4);
  servo4.attach(5);
 
  // Initialize servos on 0
  servo1.write(0);
  //servo2.write(0); //Optional
  servo3.write(0);
  servo4.write(0);
}


void loop() {
  //TLV493D Data reading
  Tlv493dMagnetic3DSensor.updateData();
  delay(300);


  //TLV493D Data printing
  Serial.print("X = ");
  Serial.print(Tlv493dMagnetic3DSensor.getY());
  Serial.print(" Y = ");
  Serial.println(Tlv493dMagnetic3DSensor.getX());
  Serial.print(" Z = ");
  Serial.println(Tlv493dMagnetic3DSensor.getZ());


  // Read the switch
  int switchState = digitalRead(switchPin);
  int gripperState = digitalRead(gripperPin);
  // If it's changed and it's high, toggle the mouse state:
  if (switchState != lastSwitchState) {
    if (switchState == HIGH) {
      armIsActive = !armIsActive;
      // Turn on LED to indicate mouse state:
      digitalWrite(LED_BUILTIN, !armIsActive);
    }
  }
  // Save switch state for next comparison
  lastSwitchState = switchState;




  // Read and scale the two axes
  int xReading =   map(constrain((int)Tlv493dMagnetic3DSensor.getY(),0,range),0,range,0,180);
  int yReading = map(constrain((int)Tlv493dMagnetic3DSensor.getX(),0,range),0,range,0,180);

 // if the mouse control state is active, move the mouse:
  if (armIsActive) {
    servo1.write(xReading);
    //servo2.write(zReading);
    servo3.write(yReading);


      // Read the gripper
    if (gripperState != lastGripperState) {
          if (gripperState == HIGH) {
            gripperIsActive = !gripperIsActive;
            // Open or close the gripper
            if(gripperIsActive==true)
                  servo4.write(180);
            else
              servo4.write(0);
          }
    }
  // Save gripper state for next comparison
  lastGripperState = gripperState;
  }
}

Code for Circuitpython

Python
import board # Library to access board pins
import busio # Library to access i2c bus
import time # Library for delays
import digitalio # Library to configure digital pins
import analogio # Library to configure analog pins
import pulseio # Library to control pwm
import adafruit_tlv493d # Library to control TLV493D sensor
from adafruit_motor import servo # Library for servos control

# i2c object declaration
i2c = busio.I2C(board.SCL, board.SDA)

# tlv493d object declaration
tlv = adafruit_tlv493d.TLV493D(i2c)

# Servos objects declaration
pwm1 = pulseio.PWMOut(board.D2, frequency=50)
pwm2 = pulseio.PWMOut(board.D5, frequency=50)
pwm3 = pulseio.PWMOut(board.D8, frequency=50)
pwm4 = pulseio.PWMOut(board.D9, frequency=50)

# Servos objects declaration
servo1 = servo.Servo(pwm1)#, min_pulse=750, max_pulse=2250)
servo2 = servo.Servo(pwm2)#, min_pulse=750, max_pulse=2250)
servo3 = servo.Servo(pwm3)#, min_pulse=750, max_pulse=2250)
servo4 = servo.Servo(pwm4)#, min_pulse=750, max_pulse=2250)

# Switch pin declaration
switch = digitalio.DigitalInOut(board.D4)
switch.direction = digitalio.Direction.INPUT
switch.pull = digitalio.Pull.UP

# Gripper pin declaration
gripper = digitalio.DigitalInOut(board.D3)
gripper.direction = digitalio.Direction.INPUT
gripper.pull = digitalio.Pull.UP

# Led declaration
led = digitalio.DigitalInOut(board.LED)
led.direction = digitalio.Direction.OUTPUT

# Range for constrain the joystick coordinates
limit = 100
# Whether or not to control the arm
armIsActive = False
# Previous switch state
lastSwitchState = False
# Whether or not to control the gripper
gripperIsActive = False
# Previous gripper state
lastGripperState = False

# Servos initialization
servo1.angle = 0
servo2.angle = 0
servo3.angle = 30
servo4.angle = 0

# Variable to save switch state
switchState = False
# Vaiable to save gripper state
gripperState = False

# FUNCTIONS

# Scale a value from one range to another
def _map(val,fromLow,fromHigh,toLow,toHigh):
    return int((x-fromLow)*(toHigh-toLow)/(fromHigh-fromLow)+toLow)

# Steps
def steps(axis):
    return round((axis - pot_min) / step)

# Constrain a value to a range
def constrain(val,min_val,max_val):
#    if(val<min_val):
#        return min_val
#    elif(val>max_val):
#        return max_val
#    else:
#        return val
    return min(max_val, max(min_val,val))

while True:

    # Turn OFF Led
    led.value = True

    # Read the switch
    switchState = switch.value
    # Activate the arm
    if (switchState != lastSwitchState):
        if (switchState == True):
            armIsActive = not(armIsActive)
    # Save switch state for next comparison
    lastSwitchState = switchState

    # Check if arm is active
    if armIsActive:
        # Turn ON Led
        led.value = False

        # Read and constrain the two axles
        x = constrain(-int(tlv.magnetic[1]),-limit,limit)
        y = constrain(int(tlv.magnetic[0]),-limit,limit)

        # Scale the two axles
        x = _map(x,-limit,limit,30,150)
        y = _map(y,-limit,limit,1,179)

        #x = constrain(x,0,180)
        #y = constrain(y,0,180)

        # Print coordinates
        print("X = "+str(x))
        print("Y = "+str(y))

        # Move the arm
        servo2.angle = y 
        servo3.angle = x

        # Read gripper switch
        gripperState = gripper.value
        # Open or close the gripper
        if (gripperState != lastGripperState)and(gripperState == False):
            gripperIsActive = not(gripperIsActive)
            if(gripperIsActive == False):
                servo4.angle = 180
            else:
                servo4.angle = 0
        # Save gripper switch for next comparison
        lastGripperState = gripperState

Credits

Andres Sabas

Andres Sabas

47 projects • 45 followers
Co-Founder of The Inventor's House Hackerspace, DIY, Workaholic

Comments