simulate_map_gcode

System Insights

2019-01-07

Introduction

This vignette first shows how to obtain simulated device data from G Code. Then, with the help of actual device data available, the simulated and actual data are compared.

library(mtconnectR)
library(ggplot2)
library(dplyr)
## 
## Attaching package: 'dplyr'
## The following objects are masked from 'package:stats':
## 
##     filter, lag
## The following objects are masked from 'package:base':
## 
##     intersect, setdiff, setequal, union
file_path_dmtcd = "extdata/data_truncated.bz2"
file_path_xml   = "extdata/Devices.xml"

# Reading MTC Data
device_name = get_device_info_from_xml(system.file(file_path_xml,package = "mtconnectR"))$name[2]
mtc_device = create_mtc_device_from_dmtcd(system.file(file_path_dmtcd,package = "mtconnectR"),     system.file(file_path_xml,package = "mtconnectR"),device_name)
## Reading Delimted MTC data...
## 
  |                                                                       
  |                                                                 |   0%
  |                                                                       
  |                                                                 |   1%
  |                                                                       
  |=                                                                |   1%
  |                                                                       
  |=                                                                |   2%
  |                                                                       
  |==                                                               |   2%
  |                                                                       
  |==                                                               |   3%
  |                                                                       
  |==                                                               |   4%
  |                                                                       
  |===                                                              |   4%
  |                                                                       
  |===                                                              |   5%
  |                                                                       
  |====                                                             |   5%
  |                                                                       
  |====                                                             |   6%
  |                                                                       
  |====                                                             |   7%
  |                                                                       
  |=====                                                            |   7%
  |                                                                       
  |=====                                                            |   8%
  |                                                                       
  |======                                                           |   8%
  |                                                                       
  |======                                                           |   9%
  |                                                                       
  |======                                                           |  10%
  |                                                                       
  |=======                                                          |  10%
  |                                                                       
  |=======                                                          |  11%
  |                                                                       
  |=======                                                          |  12%
  |                                                                       
  |========                                                         |  12%
  |                                                                       
  |========                                                         |  13%
  |                                                                       
  |=========                                                        |  13%
  |                                                                       
  |=========                                                        |  14%
  |                                                                       
  |=========                                                        |  15%
  |                                                                       
  |==========                                                       |  15%
  |                                                                       
  |==========                                                       |  16%
  |                                                                       
  |===========                                                      |  16%
  |                                                                       
  |===========                                                      |  17%
  |                                                                       
  |===========                                                      |  18%
  |                                                                       
  |============                                                     |  18%
  |                                                                       
  |============                                                     |  19%
  |                                                                       
  |=============                                                    |  19%
  |                                                                       
  |=============                                                    |  20%
  |                                                                       
  |=============                                                    |  21%
  |                                                                       
  |==============                                                   |  21%
  |                                                                       
  |==============                                                   |  22%
  |                                                                       
  |===============                                                  |  22%
  |                                                                       
  |===============                                                  |  23%
  |                                                                       
  |===============                                                  |  24%
  |                                                                       
  |================                                                 |  24%
  |                                                                       
  |================                                                 |  25%
  |                                                                       
  |=================                                                |  25%
  |                                                                       
  |=================                                                |  26%
  |                                                                       
  |=================                                                |  27%
  |                                                                       
  |==================                                               |  27%
  |                                                                       
  |==================                                               |  28%
  |                                                                       
  |===================                                              |  28%
  |                                                                       
  |===================                                              |  29%
  |                                                                       
  |===================                                              |  30%
  |                                                                       
  |====================                                             |  30%
  |                                                                       
  |====================                                             |  31%
  |                                                                       
  |====================                                             |  32%
  |                                                                       
  |=====================                                            |  32%
  |                                                                       
  |=====================                                            |  33%
  |                                                                       
  |======================                                           |  33%
  |                                                                       
  |======================                                           |  34%
  |                                                                       
  |======================                                           |  35%
  |                                                                       
  |=======================                                          |  35%
  |                                                                       
  |=======================                                          |  36%
  |                                                                       
  |========================                                         |  36%
  |                                                                       
  |========================                                         |  37%
  |                                                                       
  |========================                                         |  38%
  |                                                                       
  |=========================                                        |  38%
  |                                                                       
  |=========================                                        |  39%
  |                                                                       
  |==========================                                       |  39%
  |                                                                       
  |==========================                                       |  40%
  |                                                                       
  |==========================                                       |  41%
  |                                                                       
  |===========================                                      |  41%
  |                                                                       
  |===========================                                      |  42%
  |                                                                       
  |============================                                     |  42%
  |                                                                       
  |============================                                     |  43%
  |                                                                       
  |============================                                     |  44%
  |                                                                       
  |=============================                                    |  44%
  |                                                                       
  |=============================                                    |  45%
  |                                                                       
  |==============================                                   |  45%
  |                                                                       
  |==============================                                   |  46%
  |                                                                       
  |==============================                                   |  47%
  |                                                                       
  |===============================                                  |  47%
  |                                                                       
  |===============================                                  |  48%
  |                                                                       
  |================================                                 |  48%
  |                                                                       
  |================================                                 |  49%
  |                                                                       
  |================================                                 |  50%
  |                                                                       
  |=================================                                |  50%
  |                                                                       
  |=================================                                |  51%
  |                                                                       
  |=================================                                |  52%
  |                                                                       
  |==================================                               |  52%
  |                                                                       
  |==================================                               |  53%
  |                                                                       
  |===================================                              |  53%
  |                                                                       
  |===================================                              |  54%
  |                                                                       
  |===================================                              |  55%
  |                                                                       
  |====================================                             |  55%
  |                                                                       
  |====================================                             |  56%
  |                                                                       
  |=====================================                            |  56%
  |                                                                       
  |=====================================                            |  57%
  |                                                                       
  |=====================================                            |  58%
  |                                                                       
  |======================================                           |  58%
  |                                                                       
  |======================================                           |  59%
  |                                                                       
  |=======================================                          |  59%
  |                                                                       
  |=======================================                          |  60%
  |                                                                       
  |=======================================                          |  61%
  |                                                                       
  |========================================                         |  61%
  |                                                                       
  |========================================                         |  62%
  |                                                                       
  |=========================================                        |  62%
  |                                                                       
  |=========================================                        |  63%
  |                                                                       
  |=========================================                        |  64%
  |                                                                       
  |==========================================                       |  64%
  |                                                                       
  |==========================================                       |  65%
  |                                                                       
  |===========================================                      |  65%
  |                                                                       
  |===========================================                      |  66%
  |                                                                       
  |===========================================                      |  67%
  |                                                                       
  |============================================                     |  67%
  |                                                                       
  |============================================                     |  68%
  |                                                                       
  |=============================================                    |  68%
  |                                                                       
  |=============================================                    |  69%
  |                                                                       
  |=============================================                    |  70%
  |                                                                       
  |==============================================                   |  70%
  |                                                                       
  |==============================================                   |  71%
  |                                                                       
  |==============================================                   |  72%
  |                                                                       
  |===============================================                  |  72%
  |                                                                       
  |===============================================                  |  73%
  |                                                                       
  |================================================                 |  73%
  |                                                                       
  |================================================                 |  74%
  |                                                                       
  |================================================                 |  75%
  |                                                                       
  |=================================================                |  75%
  |                                                                       
  |=================================================                |  76%
  |                                                                       
  |==================================================               |  76%
  |                                                                       
  |==================================================               |  77%
  |                                                                       
  |==================================================               |  78%
  |                                                                       
  |===================================================              |  78%
  |                                                                       
  |===================================================              |  79%
  |                                                                       
  |====================================================             |  79%
  |                                                                       
  |====================================================             |  80%
  |                                                                       
  |====================================================             |  81%
  |                                                                       
  |=====================================================            |  81%
  |                                                                       
  |=====================================================            |  82%
  |                                                                       
  |======================================================           |  82%
  |                                                                       
  |======================================================           |  83%
  |                                                                       
  |======================================================           |  84%
  |                                                                       
  |=======================================================          |  84%
  |                                                                       
  |=======================================================          |  85%
  |                                                                       
  |========================================================         |  85%
  |                                                                       
  |========================================================         |  86%
  |                                                                       
  |========================================================         |  87%
  |                                                                       
  |=========================================================        |  87%
  |                                                                       
  |=========================================================        |  88%
  |                                                                       
  |==========================================================       |  88%
  |                                                                       
  |==========================================================       |  89%
  |                                                                       
  |==========================================================       |  90%
  |                                                                       
  |===========================================================      |  90%
  |                                                                       
  |===========================================================      |  91%
  |                                                                       
  |===========================================================      |  92%
  |                                                                       
  |============================================================     |  92%
  |                                                                       
  |============================================================     |  93%
  |                                                                       
  |=============================================================    |  93%
  |                                                                       
  |=============================================================    |  94%
  |                                                                       
  |=============================================================    |  95%
  |                                                                       
  |==============================================================   |  95%
  |                                                                       
  |==============================================================   |  96%
  |                                                                       
  |===============================================================  |  96%
  |                                                                       
  |===============================================================  |  97%
  |                                                                       
  |===============================================================  |  98%
  |                                                                       
  |================================================================ |  98%
  |                                                                       
  |================================================================ |  99%
  |                                                                       
  |=================================================================|  99%
  |                                                                       
  |=================================================================| 100%
## 100% data contextualized successfuly!

Calculating feed rate from the position data

This function calculates the feed rate based on the position data and returns the timestamps and the corresponding feed rates

calc_feed = calculated_feed_from_position(mtc_device) %>%
  mutate(value = replace(value, value < 0.1, 0)) %>% 
  clean_reduntant_rows()
## Using Datatems path_pos_x,path_pos_y,path_pos_z for calculating PFR

Adding feed rate data item to MTC Device

This function is very useful to append data to the MTCDevice object A data frame in timestamp|value format along with data item type, name and source type can be added as a data item

mtc_device = add_data_item_to_mtc_device(mtc_device, calc_feed, category = "SAMPLE",
                                         data_item_name = "pfr_calculated<PATH_FEEDRATE>")

# Using merge with regex pattern to choose only the position variables
pos_data_mtc = merge(mtc_device, "path_pos") %>% filter(timestamp > as.POSIXct("2016-03-22"))
names(pos_data_mtc) = extract_param_from_xpath(names(pos_data_mtc), show_warnings = F)

Parse G Code

This is the first step in simulating device data from G Code This function parses the file which has the G code and splits it into blocks of G code and gives contextual information to each block with the help of the inbuilt G code dictionary

gcode_file_path = "extdata/raw_gcode.NC"
gcode_parsed = parse_gcode(system.file(gcode_file_path,package = "mtconnectR"))
## 
## CENTER_POSITION      MOTION_ARC   MOTION_LINEAR    MOTION_RAPID 
##             410             170             159              35 
##   PATH_FEEDRATE        POSITION 
##              69            1404

Simulate G Code data

This function finds the simulated postion and velocity data from the parsed G code

simulated_gcode_data = simulate_data_from_gcode(gcode_parsed, start_time = 0, data_res = 0.1, data_type = "HH") %>%   na.omit()

# Creating MTCDevice object from the simulated data
mtc_device_sim = create_mtc_device_from_ts(simulated_gcode_data)
pos_data_sim = merge(mtc_device_sim, "pos")

# Plotting variation of Y position with time
ggplot(pos_data_mtc) + geom_path(aes(x = timestamp, y = path_pos_y)) 

Subsetting data for the operation for which we want to compare the simulated and actual data

time_split_start = as.POSIXct("2016-03-22 12:45:00", tz = "UTC") 
time_split_end = as.POSIXct("2016-03-22 13:00:00", tz = "UTC") 

pos_data_mtc = pos_data_mtc %>% filter(timestamp > time_split_start & timestamp < time_split_end)

A number of plots are made to compare the simulated and actual data

# Plotting Y position vs X position to get an idea of the part made
ggplot(pos_data_mtc) + geom_path(aes(x = path_pos_x, y = path_pos_y)) 

# Plotting simulated part as per proper scale
ggplot(pos_data_sim) + geom_path(aes(x = x_pos, y = y_pos)) +
  coord_cartesian(xlim = c(-2.5, 6), ylim = c(-4, 1))

# Plotting actual part as per proper scale
ggplot(pos_data_mtc) + geom_path(aes(x = path_pos_x, y = path_pos_y)) +
  coord_cartesian(xlim = c(-2.5, 6), ylim = c(-4, 1))

Map G Code simulated data to MTC data

This function maps the timestamps of the simulated data to the actual ones by constructing a distance matrix and then using a step pattern to map

time_start_op = as.POSIXct("2016-03-22 12:45:00", tz = "UTC")
time_end_op = as.POSIXct("2016-03-22 13:00:00", tz = "UTC")

mtc_device_op = mtc_device %>% filter_timestamps_mtc_device(time_start_op, time_end_op)
mtc_sim_mapped_op = map_gcode_mtc(mtc_device_sim, mtc_device_op, elasticity = 200)
## Using the following mapping:
##   sim_name                                                 mtc_name
## 1    x_pos nist_testbed_GF_Agie_1<Device>:path_pos_x<PATH_POSITION>
## 2    y_pos nist_testbed_GF_Agie_1<Device>:path_pos_y<PATH_POSITION>
## 3    z_pos nist_testbed_GF_Agie_1<Device>:path_pos_z<PATH_POSITION>
## Normalized Distance: 0.35677295992244

plot_twoway

This function is used to visualise the mapping between the timestamps of the simulated and actual data

plot_twoway(mtc_sim_mapped_op, mtc_device_sim, mtc_device_op, 20, 100)

Comparing feed parameters

# Comparing feed rate data Simulated vs actual 
feed_compare_op = merge(mtc_sim_mapped_op, "pfr") %>% na.omit()
names(feed_compare_op) = extract_param_from_xpath(names(feed_compare_op), show_warnings = F)

ggplot(feed_compare_op, aes(x = timestamp)) +
  geom_step(aes(y = pfr_calculated, colour = 'Calculated'), size = 1) +
  geom_step(aes(y = pfr, colour = "Simulated"), size = 1) +
  coord_cartesian(ylim = c(-3, 300))

# Comparing feed ratio data Simulated vs actual
pos_feed_ratio_compare_op = merge(mtc_sim_mapped_op, "pfr|_pos_") %>% na.omit()
names(pos_feed_ratio_compare_op) = extract_param_from_xpath(names(pos_feed_ratio_compare_op), show_warnings = F)

pos_feed_ratio_compare_op = pos_feed_ratio_compare_op %>%
  mutate(feed_ratio = abs(round(((pfr_calculated)/(pfr + 1e-6)), 2))) %>%
  mutate(feed_ratio = replace(feed_ratio, feed_ratio > 1.1, NA_real_))

ggplot(pos_feed_ratio_compare_op) +
  geom_path(aes(x = path_pos_x, y = path_pos_y, col = feed_ratio), size = 1) +
  scale_colour_gradient(low="#91bfdb", high = "#fc8d59", na.value = "#ffffbf") +
  coord_cartesian(xlim = c(-2, 5), ylim = c(-4, 1.5))