Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 22 additions & 2 deletions src/fsae_electric_vehicle/src/cpp/gps/main.cpp
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
#include <ctime>
#include <stdlib.h>

#include "ros/ros.h"
#include "fsae_electric_vehicle/gps.h"

Expand All @@ -17,11 +20,28 @@ int main(int argc, char **argv) {
ros::Rate loop_rate{5};
std::cout << "Init!" << std::endl;

int x = 0;
// Coordinates for JD Hall
float x = 34.241993;
float y = -118.528624;

float alt = 0;
float sats = 0;
float t_start = time(0); // current time

srand(static_cast <unsigned> (time(0)));

while (ros::ok()) {
gps.latitude = x;
x++;
gps.longitude = y;
gps.alt = alt;
gps.sats = sats;
gps.time = time(0) - t_start;

x += (-0.00001) + static_cast<float>(rand()) /
(static_cast<float> (RAND_MAX/(0.00001+0.00001))); // set values for simulation ONLY
y += (-0.00001) + static_cast<float>(rand()) /
(static_cast<float> (RAND_MAX/(0.00001+0.00001)));

gps_topic.publish(gps);
ros::spinOnce();
loop_rate.sleep();
Expand Down
37 changes: 30 additions & 7 deletions src/fsae_electric_vehicle/src/cpp/socketsender/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,23 @@
#include "fsae_electric_vehicle/speedometer.h"
#include "std_msgs/String.h"

#include <WinSock2.h>
#include <Ws2tcpip.h>
#pragma comment(lib, "Ws2_32.lib")
#ifdef _WIN32
#include <WinSock2.h>
#include <Ws2tcpip.h>
#pragma comment(lib, "Ws2_32.lib")
#else
#include <sys/types.h>
#include <sys/socket.h>
#include <arpa/inet.h>
#include <netdb.h>
#include <unistd.h>
#endif

SOCKET sd;
#ifdef _WIN32
SOCKET sd;
#else
int sd;
#endif

void speedCallback(const fsae_electric_vehicle::speedometer::ConstPtr& msg) {
ROS_INFO("I heard: [%f]", msg->speed);
Expand All @@ -18,7 +30,7 @@ void speedCallback(const fsae_electric_vehicle::speedometer::ConstPtr& msg) {
struct sockaddr_in addr;
std::memset(&addr, 0, sizeof(addr));
addr.sin_family = AF_INET;
InetPton(AF_INET, "127.0.0.1", &(addr.sin_addr));
inet_pton(AF_INET, "127.0.0.1", &(addr.sin_addr)); //changed to ANSI version for compatibility
addr.sin_port = htons(1234);
sendto(sd, buf, 4, 0, reinterpret_cast<sockaddr*>(&addr), sizeof(addr));
}
Expand All @@ -35,8 +47,14 @@ int main(int argc, char **argv) {
addr.sin_port = htons(1232);

int optval = 1;

#ifdef _WIN32
setsockopt(sd, SOL_SOCKET, SO_EXCLUSIVEADDRUSE,
reinterpret_cast<char*>(&optval), sizeof optval);
#else
setsockopt(sd, SOL_SOCKET, SO_REUSEADDR,
reinterpret_cast<char*>(&optval), sizeof optval);
#endif

bind(sd, reinterpret_cast<sockaddr*>(&addr), sizeof(addr));

Expand All @@ -49,6 +67,11 @@ int main(int argc, char **argv) {

ros::spin();

::shutdown(sd, SD_BOTH);
closesocket(sd);
#ifdef _WIN32
shutdown(sd, SD_BOTH);
closesocket(sd);
#else
shutdown(sd, SHUT_RDWR);
close(sd);
#endif
}
53 changes: 53 additions & 0 deletions src/fsae_electric_vehicle/src/cpp/steering/main.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
#include <cmath>

#include "ros/ros.h"
#include "fsae_electric_vehicle/steering.h"

#define PI 3.1415926535

int main(int argc, char **argv) {
std::cout << "WHAT!?" << std::endl;
ros::init(argc, argv, "Steering");
std::cout << "After init" << std::endl;
ros::NodeHandle n;
std::cout << "After node handle" << std::endl;

ros::Publisher steering_topic = n.advertise<fsae_electric_vehicle::steering>
("steering", 1000);

std::cout << "After publisher" << std::endl;

fsae_electric_vehicle::steering steering;

ros::Rate loop_rate{5};
std::cout << "Init!" << std::endl;

//assumes data output includes values from 0-1023
//int x = 0.0;
int pos = 1023/2; //middle forward position
bool right == true;


while (ros::ok()) {
steering.value = pos;

// check if this produces sinusoidal output
//x += 0.01;
//pos = static_cast<int>(1023/2 * sin(x) + 1023/2);

//should produce pure linear output (like a triangle wave)
if(right == true)
pos++;
else if(right == false)
pos--;

if(pos == 1023)
right = false;
else if(pos == 0)
right = true;

steering_topic.publish(steering);
ros::spinOnce();
loop_rate.sleep();
}
}