Md. Khairul Alam
Published © CC BY

Child Assistant

A robot for children which helps to play and reduce addiction of smartphone.

AdvancedFull instructions providedOver 2 days2,012
Child Assistant

Things used in this project

Hardware components

MATRIX Creator
MATRIX Labs MATRIX Creator
×1
Raspberry Pi 3 Model B
Raspberry Pi 3 Model B
×1
Arduino UNO & Genuino UNO
Arduino UNO & Genuino UNO
×1
SparkFun Ardumoto - Motor Driver Shield
SparkFun Ardumoto - Motor Driver Shield
×1
Gravity: MiniQ Robot chassis Encoder
DFRobot Gravity: MiniQ Robot chassis Encoder
×1
Speaker, Mini
Speaker, Mini
×1
SparkFun Mono Audio Amp Breakout - TPA2005D1
SparkFun Mono Audio Amp Breakout - TPA2005D1
×1
5V 2.5A Switching Power Supply
Digilent 5V 2.5A Switching Power Supply
×1

Software apps and online services

Arduino IDE
Arduino IDE
Snips AIR
Snips AIR
Jupyter Notebook
Jupyter Notebook
Raspbian
Raspberry Pi Raspbian

Hand tools and fabrication machines

Multitool, Screwdriver
Multitool, Screwdriver
Soldering iron (generic)
Soldering iron (generic)
Solder Wire, Lead Free
Solder Wire, Lead Free
Wire Stripper & Cutter, 18-10 AWG / 0.75-4mm² Capacity Wires
Wire Stripper & Cutter, 18-10 AWG / 0.75-4mm² Capacity Wires
3D Printer (generic)
3D Printer (generic)

Story

Read more

Custom parts and enclosures

Case Bottom

Case Top

Schematics

Connection of Motors

Code

Arduino Code

Arduino
This code will be uploaded to Arduino. It receives serial commands from Raspberry Pi and drives the motors accordingly.
/*************************************
 * Author: Md. Khairul Alam
 * This code is for controlling a robotic arm containing 5 servos
 */
#include <AFMotor.h>

AF_DCMotor motorLeft(3);
AF_DCMotor motorRight(4);

String inputString = "";         // a string to hold incoming data
boolean stringComplete = false;  // whether the string is complete

void setup() {
  // put your setup code here, to run once:
  Serial.begin(9600);
 
  inputString.reserve(200);

   // turn on motor
  motorLeft.setSpeed(200);
  motorRight.setSpeed(200);
 
  motorLeft.run(RELEASE);
  motorRight.run(RELEASE);

}

void loop() {
  
  if (stringComplete) {
    if(inputString == "Forward"){//if received message = pos1
        forward();
        delay(20);
      }
    else if(inputString == "Backward"){
        backward();
        delay(20);
      } 
    else if(inputString == "Right"){
        turnRight();
        delay(20);
      } 
    else if(inputString == "Left"){
        turnLeft();
        delay(20);
      } 
    else if(inputString == "Stop"){
        stop();
        delay(20);
      }   
    // clear the string:
    inputString = "";
    stringComplete = false;
  }

}


void serialEvent() {
  while (Serial.available()) {    
    // get the new byte:
    char inChar = (char)Serial.read();     
    // if the incoming character is a newline, set a flag
    // so the main loop can do something about it:
    if (inChar == '\n') {
      stringComplete = true;
    }
    else
    // add it to the inputString:  
      inputString += inChar;
  }
}


void forward(){
  motorLeft.run(FORWARD);
  motorRight.run(FORWARD);
  }

void backward(){
  motorLeft.run(BACKWARD);
  motorRight.run(BACKWARD);
  }

void turnRight(){
  motorLeft.run(FORWARD);
  motorRight.run(BACKWARD);
  }

void turnLeft(){
  motorLeft.run(BACKWARD);
  motorRight.run(FORWARD);
  }

void stop(){
  motorLeft.run(RELEASE);
  motorRight.run(RELEASE);
  }

Action Code for Snips

Python
#!/usr/bin/env python3
# -*- coding: utf-8 -*-

from snipsTools import SnipsConfigParser
from hermes_python.hermes import Hermes
from hermes_python.ontology import *


import io
import sys
import logging
import time
import serial
from imp import load_source
from matrix_lite import led
from matrix_lite import sensors

ser = serial.Serial('/dev/ttyAMA0',9600)

CONFIG_INI = "config.ini"
logging.basicConfig()

_LOGGER = logging.getLogger(__name__)
_LOGGER.setLevel(logging.ERROR)


# If this skill is supposed to run on the satellite,
# please get this mqtt connection info from <config.ini>
# Hint: MQTT server is always running on the master device
MQTT_IP_ADDR = "localhost"
MQTT_PORT = 1883
MQTT_ADDR = "{}:{}".format(MQTT_IP_ADDR, str(MQTT_PORT))


class Child_Assistant_app(object):
    """Class used to wrap action code with mqtt connection        
    This app dispatch the intents to the corresponding actions
    """

    def __init__(self):
        """Initialize our app 
        - read the config file
        - initialize our API and Multilanguage Text handler class with 
          correct language 
        """

        # get the configuration if needed
        try:
            self.config = SnipsConfigParser.read_configuration_file(CONFIG_INI)

            # set log level according to config.ini
            if self.config["global"]["log_level"] == "DEBUG":
                _LOGGER.setLevel(logging.DEBUG)

            _LOGGER.debug(u"[__init__] - reading the config file {}".format(self.config))
            _LOGGER.debug(u"[__init__] - MQTT address is {}".format(MQTT_ADDR))

        except:
            self.config = None
            _LOGGER.error(u"[__init__] - not able to read config file!")


        # start listening to MQTT
        self.start_blocking()

    # -------------------------------------------------------------------------
    # --> Sub callback function, one per intent
    # -------------------------------------------------------------------------

    # ===train_schedule_to intent action ======================================
    def robot_direction(self, hermes, intent_message):
        """Action for direction slot 
        """
        # terminate the session first if not continue
        hermes.publish_end_session(intent_message.session_id, "")
        
        direction = intent_message.slots.Direction.first().value
        ser.write(direction)
        ser.write('\n')
        # terminate the session first if not continue
        #hermes.publish_end_session(intent_message.session_id, text_to_speak)
        hermes.publish_start_session_notification(intent_message.site_id, "Car is going {}".format(str(direction)), "")

    # ===train_schedule_from_to intent action =================================
    def robot_color(self, hermes, intent_message):
        """Action for color slot
        """
        # terminate the session first if not continue
        hermes.publish_end_session(intent_message.session_id, "")
        
        color = intent_message.slots.Color.first().value
        ser.write(color)
        ser.write('\n')
        if color == 'Red':
            led.set('red') # color name
        elif color == 'Green':
            led.set('green') # color name
        elif color == 'Blue':
            led.set('blue') # color name
        elif color == 'White':
            led.set('white') # color name
        elif color == 'Black':
            led.set('black') # color name
        
        # terminate the session first if not continue
        #hermes.publish_end_session(intent_message.session_id, text_to_speak)
        hermes.publish_start_session_notification(intent_message.site_id, "Changes colot to {}".format(str(color)), "")

    # ===station_timetable intent action ======================================
    def robot_sound(self, hermes, intent_message):
        """fulfill the intent 
        """
        # terminate the session first if not continue
        hermes.publish_end_session(intent_message.session_id, "")
        
        sound = intent_message.slots.Sound.first().value
        ser.write(sound)
        ser.write('\n')
      
        
        # terminate the session first if not continue
        #hermes.publish_end_session(intent_message.session_id, text_to_speak)
        hermes.publish_start_session_notification(intent_message.site_id, "Car is playing {}".format(str(sound)), "")
        
    def robot_voice(self, hermes, intent_message):
        """fulfill the intent 
        """
        # terminate the session first if not continue
        hermes.publish_end_session(intent_message.session_id, "")
        
        voice = intent_message.slots.Voice.first().value
        ser.write(voice)
        ser.write('\n')
        
        # terminate the session first if not continue
        #hermes.publish_end_session(intent_message.session_id, text_to_speak)
        hermes.publish_start_session_notification(intent_message.site_id, "Car is talking {}".format(str(voice)), "")
        
    def robot_sense(self, hermes, intent_message):
        """fulfill the intent 
        """
        # terminate the session first if not continue
        hermes.publish_end_session(intent_message.session_id, "")
        
        sense = intent_message.slots.Sense.first().value
        #if sense == 'Temperature':
        #    sensors.humidity.read()
        #elif sense == 'Humidity':
        #    sensors.humidity.read()
        #elif sense == 'Pressure':
        #    sensors.pressure.read()
        #elif sense == 'Backward':
        #    self.relay13.off()
        #elif sense == 'Stop':
        #    self.relay13.off()
        #sensors.uv.read()
        # terminate the session first if not continue
        #hermes.publish_end_session(intent_message.session_id, text_to_speak)
        hermes.publish_start_session_notification(intent_message.site_id, "Car is reading {}".format(str(sense)), "")

    # -------------------------------------------------------------------------
    # --> Master callback function, triggered everytime an intent is recognized
    # -------------------------------------------------------------------------

    def master_intent_callback(self, hermes, intent_message):
        coming_intent = intent_message.intent.intent_name
        _LOGGER.debug(u"[master_intent_callback] - Intent: {}".format(coming_intent))
        if coming_intent == "taifur:robot_direction":
            self.robot_direction(hermes, intent_message)
        if coming_intent == "taifur:robot_sound":
            self.robot_sound(hermes, intent_message)
        if coming_intent == "taifur:robot_color":
            self.robot_color(hermes, intent_message)
        if coming_intent == "taifur:robot_voice":
            self.robot_voice(hermes, intent_message)
        if coming_intent == "taifur:robot_sense":
            self.robot_sense(hermes, intent_message)

    # --> Register callback function and start MQTT
    def start_blocking(self):
        with Hermes(MQTT_ADDR) as h:
            h.subscribe_intents(self.master_intent_callback).start()


if __name__ == "__main__":
    Child_Assistant_app()

Credits

Md. Khairul Alam

Md. Khairul Alam

41 projects • 365 followers
Engineer, developer, maker, and hacker.

Comments

Add projectSign up / Login