Skip to content

Instantly share code, notes, and snippets.

@collinalexbell
Last active August 29, 2015 14:25
Show Gist options
  • Save collinalexbell/2f309b92dc9f58980d37 to your computer and use it in GitHub Desktop.
Save collinalexbell/2f309b92dc9f58980d37 to your computer and use it in GitHub Desktop.
#ifndef DISTANCE_SENSOR_H
#define DISTANCE_SENSOR_H
class Robot;
#include <vector>
#include "robot.h"
class Distance_Sensor{
public:
Distance_Sensor(Robot*, double, double);
bool angle_in_range(double angle, double a, double b);
double sense(std::vector<Drawable*>);
private:
Robot* owner;
double center_angle;
double sensor_width;
};
#endif
clang++ -std=c++11 -stdlib=libc++ -c -g -I include/simulator src/robot.cpp
In file included from src/robot.cpp:4:
In file included from include/simulator/robot.h:7:
In file included from include/simulator/world.h:10:
In file included from /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/../include/c++/v1/unordered_map:350:
In file included from /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/../include/c++/v1/__hash_table:16:
In file included from /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/../include/c++/v1/memory:597:
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/../include/c++/v1/type_traits:948:38: error:
implicit instantiation of undefined template 'std::__1::hash<std::__1::basic_string<char,
std::__1::char_traits<char>, std::__1::allocator<char> > >'
: public integral_constant<bool, __is_empty(_Tp)> {};
^
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/../include/c++/v1/unordered_map:362:54: note:
in instantiation of template class 'std::__1::is_empty<std::__1::hash<std::__1::basic_string<char,
std::__1::char_traits<char>, std::__1::allocator<char> > > >' requested here
template <class _Key, class _Cp, class _Hash, bool = is_empty<_Hash>::value
^
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/../include/c++/v1/unordered_map:731:13: note:
in instantiation of default argument for '__unordered_map_hasher<std::__1::basic_string<char,
std::__1::char_traits<char>, std::__1::allocator<char> >, std::__1::__hash_value_type<std::__1::basic_string<char,
std::__1::char_traits<char>, std::__1::allocator<char> >, Distance_Sensor *>,
std::__1::hash<std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> > > >' required
here
typedef __unordered_map_hasher<key_type, __value_type, hasher> __hasher;
^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
include/simulator/robot.h:67:59: note: in instantiation of template class
'std::__1::unordered_map<std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> >,
Distance_Sensor *, std::__1::hash<std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char>
> >, std::__1::equal_to<std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> > >,
std::__1::allocator<std::__1::pair<const std::__1::basic_string<char, std::__1::char_traits<char>,
std::__1::allocator<char> >, Distance_Sensor *> > >' requested here
std::unordered_map<std::string, Distance_Sensor*> distance_sensors;
^
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/../include/c++/v1/memory:3065:29: note:
template is declared here
template <class _Tp> struct hash;
^
src/robot.cpp:46:52: warning: implicit conversion of NULL constant to 'int' [-Wnull-conversion]
rv.push_back(rotozoomSurface(sprite, i, 1, NULL));
~~~~~~~~~~~~~~~ ^~~~
0
src/robot.cpp:86:18: error: use of undeclared identifier 'distance_sensors'
sensor = distance_sensors.at(sensor_name);
^
src/robot.cpp:93:30: error: use of undeclared identifier 'world'
return sensor->sense(world->get_gardens());
^
1 warning and 3 errors generated.
make: *** [robot.o] Error 1
LIB := include/simulator
SIM_LIB := include/simulator
CLANG := clang++ -std=c++11 -stdlib=libc++ -c -g -I $(LIB)
LINK := clang++ `sdl-config --cflags --libs` -lSDL_ttf -lSDL_image -lSDL_gfx -std=c++11 -stdlib=libc++ -v -g -I $(LIB)
SRC := src
BIN := bin
TEST := tests
simulatormake: main.o sim.o
clang++ -o simulator main.o sim.o `sdl-config --cflags --libs` -lSDL_ttf -std=c++11 -stdlib=libc++
main.o: $(SRC)/main.cpp
$(CLANG) $(SRC)/main.cpp
sim.o: $(SRC)/sim.cpp $(SIM_LIB)/sim.h $(SIM_LIB)/drawable.h
$(CLANG) $(SRC)/sim.cpp
robot.o: $(SRC)/robot.cpp $(SIM_LIB)/robot.h $(SIM_LIB)/point.h $(SIM_LIB)/drawable.h $(SIM_LIB)/modulo.h $(SIM_LIB)/distance_sensor.h
$(CLANG) $(SRC)/robot.cpp
garden.o: $(SRC)/garden.cpp $(SIM_LIB)/garden.h $(SIM_LIB)/point.h $(SIM_LIB)/drawable.h
$(CLANG) $(SRC)/garden.cpp
world.o: $(SRC)/world.cpp $(SIM_LIB)/world.h $(SIM_LIB)/robot.h $(SIM_LIB)/garden.h $(SIM_LIB)/point.h $(SIM_LIB)/drawable.h
$(CLANG) $(SRC)/world.cpp
spiking_nnet.o: $(SRC)/spiking_nnet.cpp $(SIM_LIB)/spiking_nnet.h
$(CLANG) $(SRC)/spiking_nnet.cpp
distance_sensor.o: $(SRC)/distance_sensor.cpp $(SIM_LIB)/distance_sensor.h $(SIM_LIB)/robot.h $(SIM_LIB)/modulo.h $(SIM_LIB)/drawable.h
$(CLANG) $(SRC)/distance_sensor.cpp
drawable.h: point.h
robot.h: point.h drawable.h
point.h:
test_sim: sim.o test_sim.o
$(LINK) -o $(BIN)/test_sim sim.o test_sim.o
test_robot: robot.o sim.o world.o
$(LINK) -o $(BIN)/test_robot robot.o world.o $(TEST)/test_robot.cpp sim.o
test_garden: garden.o
$(LINK) -o $(BIN)/test_garden garden.o $(TEST)/test_garden.cpp
gdb bin/test_garden
test_world: robot.o world.o sim.o garden.o distance_sensor.o
$(LINK) -o $(BIN)/test_world world.o robot.o sim.o garden.o distance_sensor.o $(TEST)/test_world.cpp
gdb bin/test_world
test_spiking_nnet: spiking_nnet.o sim.o
$(LINK) -o $(BIN)/test_spiking_nnet $(TEST)/test_spiking_nnet.cpp spiking_nnet.o sim.o
gdb bin/test_spiking_nnet
test_distance_sensor: distance_sensor.o robot.o garden.o
$(LINK) -o $(BIN)/test_distance_sensor $(TEST)/test_distance_sensor.cpp distance_sensor.o robot.o garden.o
gdb bin/test_distance_sensor
clean:
rm *.o
#ifndef ROBOT_H
#define ROBOT_H
#include <SDL/SDL.h>
#include "point.h"
#include "drawable.h"
#include "world.h"
#include "distance_sensor.h"
#include <vector>
#include <unordered_map>
using namespace std;
class Robot: public Drawable{
public:
Robot();
void add_world(World* w){
world = w;
}
//Used when initializing robot
void set_position(int x, int y);
//Makes 360 sprites. 1 per degree of rotation
static vector<SDL_Surface*> make_sprites(const char* file);
//Abstract Drawable Implementation
SDL_Surface* get_image();
//Abstract Drawable Implementation
Point get_position();
//Obvious, int is amout to rotate in deg
void rotate(int);
int get_angle(){
return angle;
}
//Obvous, int is abount to move in distance
//NOTE: x, y components will be rounded
void move(int);
void add_distance_sensor(std::string name, double angle, double width);
double sense(std::string sensor_name);
private:
//Internal direction
int angle;
//360 deg of sprites
static vector<SDL_Surface*> sprites;
//Obvious
Point position;
//Flag to posibly complain on get_position()
bool position_exists;
//Obvious. Is set on movements and rotates
SDL_Surface* current_image;
//Used to store garden and customer sensors
std::unordered_map<std::string, Distance_Sensor*> distance_sensors;
//Used for sensing objects in the environment
World* world;
};
#endif
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment