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 days5,539
Child Assistant

Things used in this project

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

64 projects • 569 followers
Developer, Maker & Hardware Hacker. Currently working as a faculty at the University of Asia Pacific, Dhaka, Bangladesh.

Comments